optional query#6325
Conversation
There was a problem hiding this comment.
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_optionalmethod 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.
| 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) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
|
@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. |
Adds a
query_one_optionalwhich returnsNonerather than raise NoMatches