Skip to content

Conversation

@willmcgugan
Copy link
Member

Adds a query_one_optional which returns None rather than raise NoMatches

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a new query_one_optional method to the DOM API that returns None instead of raising a NoMatches exception when no widget matches the given selector.

Changes:

  • Added DOM.query_one_optional method with proper type hints and overloads
  • Added test coverage for the new method
  • Updated CHANGELOG.md to document the new API

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/textual/dom.py Adds query_one_optional method with type overloads that returns None instead of raising NoMatches
tests/test_query.py Adds test cases verifying the method returns None for non-matches and returns widgets for matches
CHANGELOG.md Documents the addition of DOM.query_one_optional in the Unreleased section

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +395 to +399
assert app.query_one_optional("TextArea") is None
assert app.query_one_optional("Input#bar") is None

assert isinstance(app.query_one_optional("Input"), Input)
assert isinstance(app.query_one_optional(".bar"), Input)
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test does not verify that WrongType exceptions still propagate as documented in the docstring. Consider adding a test case similar to test_query_error that verifies query_one_optional raises WrongType when a widget is found but has the wrong type (e.g., app.query_one_optional('#foo', Label) should raise WrongType when #foo is an Input).

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Copy link
Contributor

Copilot AI commented Jan 14, 2026

@willmcgugan I've opened a new pull request, #6326, to work on those changes. Once the pull request is ready, I'll request review from you.

@willmcgugan willmcgugan merged commit 4aea70d into main Jan 14, 2026
29 checks passed
@willmcgugan willmcgugan deleted the query-one-optional branch January 14, 2026 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants