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

Preserve typevar default None in type alias #18197

Merged

Conversation

alexdrydew
Copy link
Contributor

@alexdrydew alexdrydew commented Nov 26, 2024

Fixes #18188

Currently it seems that None is dropped from type variable defaults when assigning generic class to a type alias.

Example
from typing_extensions import TypeVar
from typing import Generic, Union

T1 = TypeVar("T1", default=Union[int, None])
T2 = TypeVar("T2", default=Union[int, None])


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


MyA = A[T1, int]
a: MyA = A(None, 10)  # error: Argument 1 to "A" has incompatible type "None"; expected "int"  [arg-type]
reveal_type(a.a)

The change is similar to #16859

@alexdrydew alexdrydew force-pushed the fix-union-in-default-typealias branch from f9d9ba5 to 50c82d8 Compare November 26, 2024 23:13
@brianschubert
Copy link
Collaborator

mypyc suite failure appears to be due to python/cpython#127307, not related to this PR

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/cog.py:182: error: Argument 1 to "decorator" has incompatible type "Callable[[Any, VarArg(Any), KwArg(Any)], Coroutine[Any, Any, None]]"; expected "F"  [arg-type]
+ steam/ext/commands/cog.py:182: error: Argument 1 to "decorator" has incompatible type "Callable[[Any | None, VarArg(Any), KwArg(Any)], Coroutine[Any, Any, None]]"; expected "F"  [arg-type]

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The mypy logic here seems fragile, we should try setting the default value in the state to True

edit: let's see what #18198 does

@hauntsaninja hauntsaninja merged commit 30a2007 into python:master Nov 27, 2024
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic type aliases with TypeVar defaults infer to an incorrect type
4 participants