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 @@ -3495,8 +3495,13 @@ def __init__(
3495
3495
3496
3496
@classmethod
3497
3497
def from_tuple_type (cls , info : TypeInfo ) -> TypeAlias :
3498
- """Generate an alias to the tuple type described by a given TypeInfo."""
3498
+ """Generate an alias to the tuple type described by a given TypeInfo.
3499
+
3500
+ NOTE: this doesn't set type alias type variables (for generic tuple types),
3501
+ they must be set by the caller (when fully analyzed).
3502
+ """
3499
3503
assert info .tuple_type
3504
+ # TODO: is it possible to refactor this to set the correct type vars here?
3500
3505
return TypeAlias (
3501
3506
info .tuple_type .copy_modified (fallback = mypy .types .Instance (info , info .defn .type_vars )),
3502
3507
info .fullname ,
@@ -3506,8 +3511,13 @@ def from_tuple_type(cls, info: TypeInfo) -> TypeAlias:
3506
3511
3507
3512
@classmethod
3508
3513
def from_typeddict_type (cls , info : TypeInfo ) -> TypeAlias :
3509
- """Generate an alias to the TypedDict type described by a given TypeInfo."""
3514
+ """Generate an alias to the TypedDict type described by a given TypeInfo.
3515
+
3516
+ NOTE: this doesn't set type alias type variables (for generic TypedDicts),
3517
+ they must be set by the caller (when fully analyzed).
3518
+ """
3510
3519
assert info .typeddict_type
3520
+ # TODO: is it possible to refactor this to set the correct type vars here?
3511
3521
return TypeAlias (
3512
3522
info .typeddict_type .copy_modified (
3513
3523
fallback = mypy .types .Instance (info , info .defn .type_vars )
Original file line number Diff line number Diff line change @@ -6372,3 +6372,34 @@ y: int = x
6372
6372
[builtins fixtures/tuple.pyi]
6373
6373
[out]
6374
6374
[out2]
6375
+
6376
+ [case testGenericTypedDictWithError]
6377
+ import b
6378
+ [file a.py]
6379
+ from typing import Generic, TypeVar
6380
+ from typing_extensions import TypedDict
6381
+
6382
+ TValue = TypeVar("TValue")
6383
+ class Dict(TypedDict, Generic[TValue]):
6384
+ value: TValue
6385
+
6386
+ [file b.py]
6387
+ from a import Dict, TValue
6388
+
6389
+ def f(d: Dict[TValue]) -> TValue:
6390
+ return d["value"]
6391
+ def g(d: Dict[TValue]) -> TValue:
6392
+ return d["x"]
6393
+
6394
+ [file b.py.2]
6395
+ from a import Dict, TValue
6396
+
6397
+ def f(d: Dict[TValue]) -> TValue:
6398
+ return d["value"]
6399
+ def g(d: Dict[TValue]) -> TValue:
6400
+ return d["y"]
6401
+ [builtins fixtures/dict.pyi]
6402
+ [out]
6403
+ tmp/b.py:6: error: TypedDict "a.Dict[TValue]" has no key "x"
6404
+ [out2]
6405
+ 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