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

Union with TypeVar becomes generic #14258

Closed
ikonst opened this issue Dec 6, 2022 · 2 comments
Closed

Union with TypeVar becomes generic #14258

ikonst opened this issue Dec 6, 2022 · 2 comments
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues

Comments

@ikonst
Copy link
Contributor

ikonst commented Dec 6, 2022

With disallow_any_generics, this code:

from typing import TypeVar, Union

T1 = TypeVar('T1')
U1 = Union[T1, int]

x: U1 = 5

results in

main.py:6: error: Missing type parameters for generic type "U1"  [type-arg]

The U1 type alias becomes generic and has a single argument. Curiously, it doesn't matter what you put there. For example, this passes:

x: U1[str] = 5
#     ^^^

One might think it passes because T1 is unbound, but even if we bind it (e.g. to float), it still passes:

T1 = TypeVar('T1', bound=float)
#                        ^^^^^
U1 = Union[T1, int]

x: U1[str] = 5
#     ^^^

Playground:
https://mypy-play.net/?mypy=latest&python=3.11&flags=disallow-any-generics&gist=68115745200d34208786d8267d2d324a

@ikonst ikonst added the bug mypy got something wrong label Dec 6, 2022
@AlexWaygood AlexWaygood added the topic-type-alias TypeAlias and other type alias issues label Dec 6, 2022
@AlexWaygood
Copy link
Member

With disallow_any_generics, this code:

from typing import TypeVar, Union

T1 = TypeVar('T1')
U1 = Union[T1, int]

x: U1 = 5

results in

main.py:6: error: Missing type parameters for generic type "U1"  [type-arg]

The U1 type alias becomes generic and has a single argument.

This is a feature, not a bug. You've created a generic type alias. The feature is documented here: https://mypy.readthedocs.io/en/stable/generics.html#generic-type-aliases

Curiously, it doesn't matter what you put there. For example, this passes:

x: U1[str] = 5
#     ^^^

This doesn't look like a bug to me. U1[str] evaluates to Union[str, int], and it's okay to assign the value 5 to an object that has the type Union[str, int], since 5 is an int.

One might think it passes because T1 is unbound, but even if we bind it (e.g. to float), it still passes:

T1 = TypeVar('T1', bound=float)
#                        ^^^^^
U1 = Union[T1, int]

x: U1[str] = 5
#     ^^^

This one is a bug. However, it's a duplicate of #10445 🙂. It's already been fixed on mypy master; the fix will be included as part of mypy 1.0 (the next release).

If you try the following snippet on mypy master, here is what mypy reports:

from typing import TypeVar, Union

T1 = TypeVar('T1', bound=float)
U1 = Union[T1, int]

x: U1[str] = 5  # error: Type argument "str" of "U1" must be a subtype of "float" [type-var]

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Dec 6, 2022
@ikonst
Copy link
Contributor Author

ikonst commented Dec 6, 2022

Thanks for the thorough answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

No branches or pull requests

2 participants