Skip to content

Commit c6e8a0b

Browse files
authored
Fixes TypedDict crash with function definition (#11126)
Fixes #11079
1 parent 130ba46 commit c6e8a0b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mypy/semanal_typeargs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import List, Optional, Set
99

10-
from mypy.nodes import TypeInfo, Context, MypyFile, FuncItem, ClassDef, Block
10+
from mypy.nodes import TypeInfo, Context, MypyFile, FuncItem, ClassDef, Block, FakeInfo
1111
from mypy.types import (
1212
Type, Instance, TypeVarType, AnyType, get_proper_types, TypeAliasType, get_proper_type
1313
)
@@ -66,6 +66,8 @@ def visit_instance(self, t: Instance) -> None:
6666
# Type argument counts were checked in the main semantic analyzer pass. We assume
6767
# that the counts are correct here.
6868
info = t.type
69+
if isinstance(info, FakeInfo):
70+
return # https://github.com/python/mypy/issues/11079
6971
for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
7072
if tvar.values:
7173
if isinstance(arg, TypeVarType):

test-data/unit/check-typeddict.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ p = Point(x=42, y=1337, z='whatever')
185185
reveal_type(p) # N: Revealed type is "TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int, 'z': Any})"
186186
[builtins fixtures/dict.pyi]
187187

188+
[case testCannotCreateTypedDictWithClassWithFunctionUsedToCrash]
189+
# https://github.com/python/mypy/issues/11079
190+
from typing import TypedDict
191+
class D(TypedDict):
192+
y: int
193+
def x(self, key: int): # E: Invalid statement in TypedDict definition; expected "field_name: field_type"
194+
pass
195+
196+
d = D(y=1)
197+
reveal_type(d) # N: Revealed type is "TypedDict('__main__.D', {'y': builtins.int})"
198+
[builtins fixtures/dict.pyi]
199+
[typing fixtures/typing-typeddict.pyi]
200+
188201
[case testCanCreateTypedDictTypeWithUnderscoreItemName]
189202
from mypy_extensions import TypedDict
190203
Point = TypedDict('Point', {'x': int, 'y': int, '_fallback': object})

0 commit comments

Comments
 (0)