File tree 3 files changed +47
-2
lines changed
3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -80,9 +80,13 @@ def visit_type_info(self, info: TypeInfo) -> None:
80
80
if info .tuple_type :
81
81
info .tuple_type .accept (self .type_fixer )
82
82
info .update_tuple_type (info .tuple_type )
83
+ if info .special_alias :
84
+ info .special_alias .alias_tvars = list (info .defn .type_vars )
83
85
if info .typeddict_type :
84
86
info .typeddict_type .accept (self .type_fixer )
85
87
info .update_typeddict_type (info .typeddict_type )
88
+ if info .special_alias :
89
+ info .special_alias .alias_tvars = list (info .defn .type_vars )
86
90
if info .declared_metaclass :
87
91
info .declared_metaclass .accept (self .type_fixer )
88
92
if info .metaclass_type :
Original file line number Diff line number Diff line change @@ -3473,8 +3473,13 @@ def __init__(
3473
3473
3474
3474
@classmethod
3475
3475
def from_tuple_type (cls , info : TypeInfo ) -> TypeAlias :
3476
- """Generate an alias to the tuple type described by a given TypeInfo."""
3476
+ """Generate an alias to the tuple type described by a given TypeInfo.
3477
+
3478
+ NOTE: this doesn't set type alias type variables (for generic tuple types),
3479
+ they must be set by the caller (when fully analyzed).
3480
+ """
3477
3481
assert info .tuple_type
3482
+ # TODO: is it possible to refactor this to set the correct type vars here?
3478
3483
return TypeAlias (
3479
3484
info .tuple_type .copy_modified (fallback = mypy .types .Instance (info , info .defn .type_vars )),
3480
3485
info .fullname ,
@@ -3484,8 +3489,13 @@ def from_tuple_type(cls, info: TypeInfo) -> TypeAlias:
3484
3489
3485
3490
@classmethod
3486
3491
def from_typeddict_type (cls , info : TypeInfo ) -> TypeAlias :
3487
- """Generate an alias to the TypedDict type described by a given TypeInfo."""
3492
+ """Generate an alias to the TypedDict type described by a given TypeInfo.
3493
+
3494
+ NOTE: this doesn't set type alias type variables (for generic TypedDicts),
3495
+ they must be set by the caller (when fully analyzed).
3496
+ """
3488
3497
assert info .typeddict_type
3498
+ # TODO: is it possible to refactor this to set the correct type vars here?
3489
3499
return TypeAlias (
3490
3500
info .typeddict_type .copy_modified (
3491
3501
fallback = mypy .types .Instance (info , info .defn .type_vars )
Original file line number Diff line number Diff line change @@ -6359,3 +6359,34 @@ from m import Foo
6359
6359
[file m.py]
6360
6360
from missing_module import Meta # type: ignore[import]
6361
6361
class Foo(metaclass=Meta): ...
6362
+
6363
+ [case testGenericTypedDictWithError]
6364
+ import b
6365
+ [file a.py]
6366
+ from typing import Generic, TypeVar
6367
+ from typing_extensions import TypedDict
6368
+
6369
+ TValue = TypeVar("TValue")
6370
+ class Dict(TypedDict, Generic[TValue]):
6371
+ value: TValue
6372
+
6373
+ [file b.py]
6374
+ from a import Dict, TValue
6375
+
6376
+ def f(d: Dict[TValue]) -> TValue:
6377
+ return d["value"]
6378
+ def g(d: Dict[TValue]) -> TValue:
6379
+ return d["x"]
6380
+
6381
+ [file b.py.2]
6382
+ from a import Dict, TValue
6383
+
6384
+ def f(d: Dict[TValue]) -> TValue:
6385
+ return d["value"]
6386
+ def g(d: Dict[TValue]) -> TValue:
6387
+ return d["y"]
6388
+ [builtins fixtures/dict.pyi]
6389
+ [out]
6390
+ tmp/b.py:6: error: TypedDict "a.Dict[TValue]" has no key "x"
6391
+ [out2]
6392
+ tmp/b.py:6: error: TypedDict "a.Dict[TValue]" has no key "y"
You can’t perform that action at this time.
0 commit comments