-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(typehints): add Python 3.12 TypeAliasType support #36709
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,14 @@ | |
| except ImportError: | ||
| from typing_extensions import is_typeddict | ||
|
|
||
| # Python 3.12 adds TypeAliasType for `type` statements; keep optional import. | ||
| # pylint: disable=ungrouped-imports | ||
| # isort: off | ||
| try: | ||
| from typing import TypeAliasType # type: ignore[attr-defined] | ||
| except Exception: # pragma: no cover - pre-3.12 | ||
| TypeAliasType = None # type: ignore[assignment] | ||
|
|
||
| T = TypeVar('T') | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
@@ -332,6 +340,14 @@ def convert_to_beam_type(typ): | |
| sys.version_info.minor >= 10) and (isinstance(typ, types.UnionType)): | ||
| typ = typing.Union[typ] | ||
|
|
||
| # Unwrap Python 3.12 `type` aliases (TypeAliasType) to their underlying value. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like one or two unit test cases for this just to cover us. Nothing fancy, just alias a simple built-in type and make sure that the type gets converted as expected
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. Added one unit test |
||
| # This ensures Beam sees the actual aliased type (e.g., tuple[int, ...]). | ||
| if sys.version_info >= (3, 12) and TypeAliasType is not None: | ||
| if isinstance(typ, TypeAliasType): # pylint: disable=isinstance-second-argument-not-valid-type | ||
| underlying = getattr(typ, '__value__', None) | ||
| if underlying is not None: | ||
| typ = underlying | ||
|
|
||
| if getattr(typ, '__module__', None) == 'typing': | ||
| typ = convert_typing_to_builtin(typ) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.