-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
PEP 604 Union (int | str) doesn't have __parameters__ #88656
Comments
Recently I noticed that the new PEP-604 Union type doesn't collect type variables: from typing import TypeVar
T = TypeVar('T') (int | list[T]).__parameters__ Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'types.Union' object has no attribute '__parameters__' Whereas the typing.Union version has __parameters__. Is this behavior intentional? The downside to this is that things like this don't work: alias: TypeAlias = int | list[T] |
I agree that this is a bug. And >>> pickle.loads(pickle.dumps(int | str))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot pickle 'types.Union' object
>>> pickle.loads(pickle.dumps(Union[int, str]))
typing.Union[int, str] I don't have a use case for pickling types but someone might. |
Let's first see whether the type (int | list[T]) is accepted by static checkers. IMO introspecting unions should be done by looking at __args__, nothing more. |
Should P.S. mypy currently does not support it:
|
Yurii, thanks for the offer. We only need to implement __getitem__ if union supports TypeVars. Which I interpreted Guido's message above as to wait and see if static type Anyways, there's no rush. We probably can't backport this so we have until |
I intended for someone to write some test programs and report back here what mypy actually supports and what it doesn't. |
mypy does not support __parameters__:
mypy also does not support __getitem__
|
Mypy is definitely not going to support direct access to For example, this: from typing import TypeVar
T = TypeVar("T")
Alias = int | list[T]
def f(x: Alias[str]) -> None:
pass But this produces I'd rather not focus too much though on what mypy does; it is one of many type checkers by now and does not tend to be the quickest in adding support for new features. Pyright does handle the file above as I'd expect, for what it's worth. |
So I guess to expand on Jelle's example, Alias[str] should return (int | |
I'd also be OK with returning a |
I don't think we need the types.GenericAlias(int | list[T], str) @yurii do you still plan to take this? If not, I'll start working on |
@ken I will pick up this issue, thanks for asking. |
It also lacks the __module__ attribute, causing it to be unusable in PEP-593 typing.Annotated types: from typing import Annotated
x: Annotated[int | str, 'test'] Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ruben/.pyenv/versions/3.10.0b3/lib/python3.10/typing.py", line 298, in inner
return cached(*args, **kwds)
File "/Users/ruben/.pyenv/versions/3.10.0b3/lib/python3.10/typing.py", line 1594, in __class_getitem__
return _AnnotatedAlias(origin, metadata)
File "/Users/ruben/.pyenv/versions/3.10.0b3/lib/python3.10/typing.py", line 1520, in __init__
super().__init__(origin, origin)
File "/Users/ruben/.pyenv/versions/3.10.0b3/lib/python3.10/typing.py", line 976, in __init__
self.__module__ = origin.__module__
AttributeError: 'types.Union' object has no attribute '__module__'. Did you mean: '__reduce__'? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: