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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,8 @@ def set_any_tvars(
env[tv.id] = arg
t = TypeAliasType(node, args, newline, newcolumn)
if not has_type_var_tuple_type:
fixed = expand_type(t, env)
with state.strict_optional_set(options.strict_optional):
fixed = expand_type(t, env)
assert isinstance(fixed, TypeAliasType)
t.args = fixed.args

Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,24 @@ class C(Generic[_I]): pass

t: type[C] | int = C
[builtins fixtures/tuple.pyi]



[case testGenericTypeAliasWithDefaultTypeVarPreservesNoneInDefault]
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)
reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]"
[builtins fixtures/tuple.pyi]
Loading