|
41 | 41 | from textual.css.errors import DeclarationError, StyleValueError |
42 | 42 | from textual.css.match import match |
43 | 43 | from textual.css.parse import parse_declarations, parse_selectors |
44 | | -from textual.css.query import InvalidQueryFormat, NoMatches, TooManyMatches |
| 44 | +from textual.css.query import InvalidQueryFormat, NoMatches, TooManyMatches, WrongType |
45 | 45 | from textual.css.styles import RenderStyles, Styles |
46 | 46 | from textual.css.tokenize import IDENTIFIER |
47 | 47 | from textual.css.tokenizer import TokenError |
|
65 | 65 | from textual.widget import Widget |
66 | 66 | from textual.worker import Worker, WorkType, ResultType |
67 | 67 |
|
68 | | - # Unused & ignored imports are needed for the docs to link to these objects: |
69 | | - from textual.css.query import WrongType # type: ignore # noqa: F401 |
70 | | - |
71 | 68 | from typing_extensions import Literal |
72 | 69 |
|
73 | 70 | _re_identifier = re.compile(IDENTIFIER) |
@@ -1488,12 +1485,14 @@ def query_one( |
1488 | 1485 | if not match(selector_set, node): |
1489 | 1486 | continue |
1490 | 1487 | if expect_type is not None and not isinstance(node, expect_type): |
1491 | | - continue |
| 1488 | + raise WrongType( |
| 1489 | + f"Node matching {query_selector!r} is the wrong type; expected type {expect_type.__name__!r}, found {node}" |
| 1490 | + ) |
1492 | 1491 | if cache_key is not None: |
1493 | 1492 | base_node._query_one_cache[cache_key] = node |
1494 | 1493 | return node |
1495 | 1494 |
|
1496 | | - raise NoMatches(f"No nodes match {selector!r} on {self!r}") |
| 1495 | + raise NoMatches(f"No nodes match {selector!r} on {base_node!r}") |
1497 | 1496 |
|
1498 | 1497 | if TYPE_CHECKING: |
1499 | 1498 |
|
@@ -1561,19 +1560,19 @@ def query_exactly_one( |
1561 | 1560 | if not match(selector_set, node): |
1562 | 1561 | continue |
1563 | 1562 | if expect_type is not None and not isinstance(node, expect_type): |
1564 | | - continue |
| 1563 | + raise WrongType( |
| 1564 | + f"Node matching {query_selector!r} is the wrong type; expected type {expect_type.__name__!r}, found {node}" |
| 1565 | + ) |
1565 | 1566 | for later_node in iter_children: |
1566 | 1567 | if match(selector_set, later_node): |
1567 | | - if expect_type is not None and not isinstance(node, expect_type): |
1568 | | - continue |
1569 | 1568 | raise TooManyMatches( |
1570 | 1569 | "Call to query_one resulted in more than one matched node" |
1571 | 1570 | ) |
1572 | 1571 | if cache_key is not None: |
1573 | 1572 | base_node._query_one_cache[cache_key] = node |
1574 | 1573 | return node |
1575 | 1574 |
|
1576 | | - raise NoMatches(f"No nodes match {selector!r} on {self!r}") |
| 1575 | + raise NoMatches(f"No nodes match {selector!r} on {base_node!r}") |
1577 | 1576 |
|
1578 | 1577 | if TYPE_CHECKING: |
1579 | 1578 |
|
|
0 commit comments