Skip to content

Commit

Permalink
Fix incremental crash on TypedDict in method (#16364)
Browse files Browse the repository at this point in the history
Fixes #16336

All the story with `@`-names is a mess. FWIW I just copied the logic
from named tuples, where it works. So although it is a mess, it will be
now be a consistent mess, with full parity between `NamedTuple` and
`TypedDict`.
  • Loading branch information
ilevkivskyi authored Oct 30, 2023
1 parent cf045d9 commit b8c748a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
if info is None:
self.mark_incomplete(defn.name, defn)
else:
self.prepare_class_def(defn, info)
self.prepare_class_def(defn, info, custom_names=True)
return True
return False

Expand Down Expand Up @@ -2099,8 +2099,9 @@ def prepare_class_def(
# Preserve name from previous fine-grained incremental run.
global_name = defn.info.name
defn.fullname = defn.info._fullname
if defn.info.is_named_tuple:
# Named tuple nested within a class is stored in the class symbol table.
if defn.info.is_named_tuple or defn.info.typeddict_type:
# Named tuples and Typed dicts nested within a class are stored
# in the class symbol table.
self.add_symbol_skip_local(global_name, defn.info)
else:
self.globals[global_name] = SymbolTableNode(GDEF, defn.info)
Expand Down
2 changes: 2 additions & 0 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> tuple[bool, TypeInfo | N
fields, types, statements, required_keys = self.analyze_typeddict_classdef_fields(defn)
if fields is None:
return True, None # Defer
if self.api.is_func_scope() and "@" not in defn.name:
defn.name += "@" + str(defn.line)
info = self.build_typeddict_typeinfo(
defn.name, fields, types, required_keys, defn.line, existing_info
)
Expand Down
22 changes: 21 additions & 1 deletion test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -5135,7 +5135,6 @@ tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"
tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"

[case testNewAnalyzerIncrementalMethodNamedTuple]

import a
[file a.py]
from b import C
Expand Down Expand Up @@ -6540,3 +6539,24 @@ from typing_extensions import TypedDict
def test() -> None:
Counts = TypedDict("Counts", {k: int for k in "abc"}) # type: ignore
[builtins fixtures/dict.pyi]

[case testNoIncrementalCrashOnTypedDictMethod]
import a
[file a.py]
from b import C
x: C
[file a.py.2]
from b import C
x: C
reveal_type(x.h)
[file b.py]
from typing_extensions import TypedDict
class C:
def __init__(self) -> None:
self.h: Hidden
class Hidden(TypedDict):
x: int
[builtins fixtures/dict.pyi]
[out]
[out2]
tmp/a.py:3: note: Revealed type is "TypedDict('b.C.Hidden@5', {'x': builtins.int})"

0 comments on commit b8c748a

Please sign in to comment.