Skip to content

Commit 50c82d8

Browse files
committed
preserve None in typevar default
1 parent 499adae commit 50c82d8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/typeanal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,8 @@ def set_any_tvars(
22772277
env[tv.id] = arg
22782278
t = TypeAliasType(node, args, newline, newcolumn)
22792279
if not has_type_var_tuple_type:
2280-
fixed = expand_type(t, env)
2280+
with state.strict_optional_set(options.strict_optional):
2281+
fixed = expand_type(t, env)
22812282
assert isinstance(fixed, TypeAliasType)
22822283
t.args = fixed.args
22832284

test-data/unit/check-typevar-defaults.test

+21
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,24 @@ class C(Generic[_I]): pass
728728

729729
t: type[C] | int = C
730730
[builtins fixtures/tuple.pyi]
731+
732+
733+
734+
[case testGenericTypeAliasWithDefaultTypeVarPreservesNoneInDefault]
735+
from typing_extensions import TypeVar
736+
from typing import Generic, Union
737+
738+
T1 = TypeVar("T1", default=Union[int, None])
739+
T2 = TypeVar("T2", default=Union[int, None])
740+
741+
742+
class A(Generic[T1, T2]):
743+
def __init__(self, a: T1, b: T2) -> None:
744+
self.a = a
745+
self.b = b
746+
747+
748+
MyA = A[T1, int]
749+
a: MyA = A(None, 10)
750+
reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]"
751+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)