Skip to content

Commit

Permalink
Fixes TypedDict crash with function definition (python#11126)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Sep 17, 2021
1 parent 130ba46 commit c6e8a0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from typing import List, Optional, Set

from mypy.nodes import TypeInfo, Context, MypyFile, FuncItem, ClassDef, Block
from mypy.nodes import TypeInfo, Context, MypyFile, FuncItem, ClassDef, Block, FakeInfo
from mypy.types import (
Type, Instance, TypeVarType, AnyType, get_proper_types, TypeAliasType, get_proper_type
)
Expand Down Expand Up @@ -66,6 +66,8 @@ def visit_instance(self, t: Instance) -> None:
# Type argument counts were checked in the main semantic analyzer pass. We assume
# that the counts are correct here.
info = t.type
if isinstance(info, FakeInfo):
return # https://github.com/python/mypy/issues/11079
for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
if tvar.values:
if isinstance(arg, TypeVarType):
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ p = Point(x=42, y=1337, z='whatever')
reveal_type(p) # N: Revealed type is "TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int, 'z': Any})"
[builtins fixtures/dict.pyi]

[case testCannotCreateTypedDictWithClassWithFunctionUsedToCrash]
# https://github.com/python/mypy/issues/11079
from typing import TypedDict
class D(TypedDict):
y: int
def x(self, key: int): # E: Invalid statement in TypedDict definition; expected "field_name: field_type"
pass

d = D(y=1)
reveal_type(d) # N: Revealed type is "TypedDict('__main__.D', {'y': builtins.int})"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testCanCreateTypedDictTypeWithUnderscoreItemName]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int, '_fallback': object})
Expand Down

0 comments on commit c6e8a0b

Please sign in to comment.