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

Generic type aliases with TypeVar defaults infer to an incorrect type #18188

Closed
alexdrydew opened this issue Nov 25, 2024 · 1 comment · Fixed by #18197
Closed

Generic type aliases with TypeVar defaults infer to an incorrect type #18188

alexdrydew opened this issue Nov 25, 2024 · 1 comment · Fixed by #18197
Labels
bug mypy got something wrong

Comments

@alexdrydew
Copy link
Contributor

Bug Report

When using type alias of a generic type which uses type variables with defaults unbound type variables may infer to an incorrect default type.

To Reproduce

from typing import Generic, TypeVar, TypeAlias

T1 = TypeVar('T1', default=int | None)
T2 = TypeVar('T2', default=int | None)

class A(Generic[T1, T2]):
    def __init__(self, a: T1, b: T2) -> None:
        self.a = a
        self.b = b


MyA: TypeAlias = A[T1, int]
a: MyA = A(None, 10)

https://mypy-play.net/?mypy=latest&python=3.12&gist=abece06adfb80e28c6d23bd99997964e

Expected Behavior

MyA should preserve T1 default type, which is int | None

Actual Behavior

T1 is inferred to int default in MyA, so assignment to a fails with:

Argument 1 to "A" has incompatible type "None"; expected "int" [arg-type]

Your Environment

  • Mypy version used: 1.13
  • Python version used: 3.12
@alexdrydew alexdrydew added the bug mypy got something wrong label Nov 25, 2024
@alexdrydew
Copy link
Contributor Author

alexdrydew commented Nov 26, 2024

After a closer look it seems that only None type disappears from type variable default type. It seems that the bug is simply an overlook in #16859 and can be easily fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant