textual icon indicating copy to clipboard operation
textual copied to clipboard

A DOMQuery that matches nothing is treated as matching everything when chained with `exclude()` and/or `filter()`

Open cognifloyd opened this issue 8 months ago • 3 comments

  • [x] Have you checked closed issues? (https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed)

  • [x] Have you checked against the most recent version of Textual? (https://pypi.org/search/?q=textual)

The bug

When composing DOMQuery, filters/excludes of a parent query are discarded when that parent query does not match anything.

Example: Given a widget that does not have any Foo widget descendants, run this query:

query = self.query("Foo").exclude("#bar")

The parent query("Foo") does not match any nodes. But, that filter is lost for any additional exclude(...) or filter(...) calls. So, the above query is effectively:

query = self.query("*").exclude("#bar")

I expected a query query like this to match nothing, but it matches everything except any id="bar" widget(s).

This bug is caused by the __init__ and __bool__ methods of DOMQuery. Calling exclude (or filter) creates a new DOMQuery passing the self as parent:

https://github.com/Textualize/textual/blob/0f0849fd37fbd0d4d6f81889476c22340129df67/src/textual/css/query.py#L199-L213

The parent arg of DOMQuery.__init__ is typed as DOMQuery | None:

https://github.com/Textualize/textual/blob/0f0849fd37fbd0d4d6f81889476c22340129df67/src/textual/css/query.py#L69

Then, these DOMQuery.__init__ lines handle preserving data from the parent:

https://github.com/Textualize/textual/blob/0f0849fd37fbd0d4d6f81889476c22340129df67/src/textual/css/query.py#L90-L95

I suspect the if parent was meant to filter instances where parent is None. But, if parent is not None, the DOMQuery.__bool__ method gets called:

https://github.com/Textualize/textual/blob/0f0849fd37fbd0d4d6f81889476c22340129df67/src/textual/css/query.py#L140-L142

So, when chaining a widget.query(...) with exclude(...) and/or include(...), if any level of the chained calls matches nothing, any filters and excludes get discarded when constructing the next chained DOMQuery.

I don't have bandwidth to add tests and file a PR right now, but I suspect DOMQuery.__init__ could be changed to use if parent is not None instead of if parent like this to resolve the issue:

         self._filters: list[tuple[SelectorSet, ...]] = (
-            parent._filters.copy() if parent else []
+            parent._filters.copy() if parent is not None else []
         )
         self._excludes: list[tuple[SelectorSet, ...]] = (
-            parent._excludes.copy() if parent else []
+            parent._excludes.copy() if parent is not None else []
         )

Textual Diagnostics

Versions

Name Value
Textual 8.1.1
Rich 14.3.2

Python

Name Value
Version 3.13.7
Implementation CPython
Compiler Clang 20.1.4
Executable /home/jafloyd/g/github/copart-pe/ghe.git/.venv/bin/python3

Operating System

Name Value
System Linux
Release 6.8.0-90-generic
Version #91~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 20 15:20:45 UTC 2

Terminal

Name Value
Terminal Application Konsole 220403
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=236, height=55
legacy_windows False
min_width 1
max_width 236
is_terminal True
encoding utf-8
max_height 55
justify None
overflow None
no_wrap False
highlight None
markup None
height None

cognifloyd avatar Mar 20 '26 20:03 cognifloyd

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Mar 20 '26 20:03 github-actions[bot]

I've implemented the fix suggested in this issue. The changes are available in my fork:

Branch: owendevereaux:fix-domquery-filter-chain-empty-parent

Changes:

  • Fixed DOMQuery.__init__ to use if parent is not None instead of if parent (2 lines in src/textual/css/query.py)
  • Added regression test test_query_empty_parent_preserves_filters in tests/test_query.py

Note: I couldn't create a PR directly as the repository has restricted PR creation to collaborators. A maintainer would need to either:

  1. Open a PR from my branch
  2. Cherry-pick the commit: d88739bc0adb1d48285c0a5d5f2fddb9940e69c4

The fix is exactly as described in the issue - changing the condition to explicitly check for None prevents the __bool__ method from being invoked on empty queries.

owendevereaux avatar Mar 22 '26 21:03 owendevereaux

@owendevereaux Why link to your branch that simply implements the fix already suggested in this issue? My guess is yet more AI slop, which is why PRs are now restricted. Please go away and stop wasting people's time.

TomJGooding avatar Mar 23 '26 00:03 TomJGooding