Inheriting from a generic TypedDict
with a T | None
key inexplicably narrows the that key type to T
#16395
Labels
TypedDict
with a T | None
key inexplicably narrows the that key type to T
#16395
Bug Report
Inheriting from a generic
TypedDict
with aT | None
key inexplicably narrows the that key type toT
.To Reproduce
Expected Behavior
No errors. For all three variables
foo
,bar
,baz
the revealed type of thea
-key of theTypedDict
should beUnion[builtins.str, None]
.Actual Behavior
The type of the
a
-key ofbar
andbaz
is revealed to bebuiltins.str
instead ofUnion[builtins.str, None]
.Thus, the last two assignments cause the errors:
Notable obsevations
Foo
is actually declared to be of typeT
.a: T | None
and adjusting thea_value
type accordingly makes no difference.Foo
non-generic, i.e. removeGeneric[T]
from the bases (and adjustBar
andBaz
declarations accordingly), the error disappears.a
-key to be of any other union type e.g.str | int
(and adjust thea_value
type accordingly), the error seems to disappear. (Obviously I have only tested a few combinations.)Your Environment
1.6.1
mypy.ini
(and other config files):strict = True
3.11.4
(also reproduced with3.9.17
)The text was updated successfully, but these errors were encountered: