Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zarch
Copy link

@zarch zarch commented Feb 23, 2025

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

  • Query for multiple widget types using the | operator
  • Support for nested unions (e.g., (Input | Select) | Static)
  • Type validation ensuring all union members are Widget subclasses
  • Deduplication of results when using nested unions

Usage Examples

# Find all Input or Select widgets
app.query(Input | Select)

# Combining multiple widget types
app.query(Input | Static | Select)

# Using nested unions
TextOrInput = TextArea | Input
app.query(TextOrInput | Button)

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:

  • Basic union type queries
  • Nested unions
  • Type validation
  • Widget uniqueness
  • Edge cases with additional widgets

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.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

Allow querying for multiple widget types using | operator, such as
app.query(Input | Select). Supports nested unions and validates widget types.
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.

1 participant