Skip to content

Commit

Permalink
Merge pull request #5191 from Textualize/query-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan authored Oct 29, 2024
2 parents 440ff28 + 1415942 commit 4501ff2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
from textual.css.errors import DeclarationError, StyleValueError
from textual.css.match import match
from textual.css.parse import parse_declarations, parse_selectors
from textual.css.query import NoMatches, TooManyMatches
from textual.css.query import InvalidQueryFormat, NoMatches, TooManyMatches
from textual.css.styles import RenderStyles, Styles
from textual.css.tokenize import IDENTIFIER
from textual.css.tokenizer import TokenError
from textual.message_pump import MessagePump
from textual.reactive import Reactive, ReactiveError, _Mutated, _watch
from textual.timer import Timer
Expand Down Expand Up @@ -1441,7 +1442,12 @@ def query_one(
else:
query_selector = selector.__name__

selector_set = parse_selectors(query_selector)
try:
selector_set = parse_selectors(query_selector)
except TokenError:
raise InvalidQueryFormat(
f"Unable to parse {query_selector!r} as a query; check for syntax errors"
) from None

if all(selectors.is_simple for selectors in selector_set):
cache_key = (self._nodes._updates, query_selector, expect_type)
Expand Down Expand Up @@ -1505,7 +1511,12 @@ def query_exactly_one(
else:
query_selector = selector.__name__

selector_set = parse_selectors(query_selector)
try:
selector_set = parse_selectors(query_selector)
except TokenError:
raise InvalidQueryFormat(
f"Unable to parse {query_selector!r} as a query; check for syntax errors"
) from None

if all(selectors.is_simple for selectors in selector_set):
cache_key = (self._nodes._updates, query_selector, expect_type)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
from textual.widgets import Input, Label


def test_query_errors():
app = App()

with pytest.raises(InvalidQueryFormat):
app.query_one("foo_bar")

with pytest.raises(InvalidQueryFormat):
app.query("foo_bar")

with pytest.raises(InvalidQueryFormat):
app.query("1")

with pytest.raises(InvalidQueryFormat):
app.query_one("1")


def test_query():
class View(Widget):
pass
Expand Down

0 comments on commit 4501ff2

Please sign in to comment.