-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add UnionType support to query method #5578
Conversation
Allow querying for multiple widget types using | operator, such as app.query(Input | Select). Supports nested unions and validates widget types.
I do like this syntax! |
Failing to import UnionType. I wonder if that is in |
@willmcgugan I've investigated this and found that This means we would need to update our Python requirement from >=3.8 to >=3.10 to support this feature. Regarding Python version support:
Here's the current Python release timeline for reference:
Since this would be a breaking change in Python compatibility, how would you like to proceed?
What are your thoughts on this? |
That is disappointing! I'm afraid the jump from 3.8 to 3.10 is too large, and we like to support Python's for a while after their EOL. It's a nice feature, and I'd hate to have to wait. How about we make it conditional on Python 3.10 or above? |
- Use try/except to conditionally import UnionType and get_args - Make UnionType overload signature conditional - Add runtime checks before using UnionType functionality - Maintain backward compatibility with Python <3.10 This change allows users on Python 3.10+ to query with Union types while preserving compatibility with earlier Python versions.
Yes, definitely it make sense.
@willmcgugan Thanks for the suggestion, I didn't think about this option. The changes:
This approach allows Python 3.10+ users to benefit from Union type queries while maintaining full backward compatibility. Users on older Python versions simply won't have access to this specific feature, but all other functionality continues to work normally. Let me know if you'd like any adjustments to this implementation! |
I've thoroughly tested this change across multiple Python versions: 3.8 (representing pre-UnionType versions), 3.10 (first version with UnionType support), and 3.13 (latest version). Tests were executed using: All tests pass successfully on each Python version, confirming that the conditional implementation works as expected - with UnionType support on 3.10+ and graceful degradation on earlier versions. |
@willmcgugan Should I add in the PR also a change in the Changelog file and on the documentation? |
Could use an entry in the Changelog. I'll add that, unless you beat me too it. I'll also add something to the docs. |
Fixes the tests. That pattern with fixtures doesn't work for some reason. |
Add Union Type Support to Query Method
This PR adds support for using Python's Union type syntax (
|
) in the query method, allowing developers to search for multiple widget types in a single query. This makes the API more intuitive and concise when searching for different kinds of widgets.Features
|
operator(Input | Select) | Static
)Usage Examples
Technical Details
The implementation converts Union types into CSS selectors internally, maintaining compatibility with the existing query system. It handles nested unions by recursively extracting all unique widget types and validates that all types in the union are Widget subclasses.
Testing
Added comprehensive test coverage including:
Backwards Compatibility
This change is fully backwards compatible. Existing query usage with strings, single types, or None continues to work as before.
Please review the following checklist.