Skip to content

Commit b8c748a

Browse files
authored
Fix incremental crash on TypedDict in method (#16364)
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`.
1 parent cf045d9 commit b8c748a

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

mypy/semanal.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
17451745
if info is None:
17461746
self.mark_incomplete(defn.name, defn)
17471747
else:
1748-
self.prepare_class_def(defn, info)
1748+
self.prepare_class_def(defn, info, custom_names=True)
17491749
return True
17501750
return False
17511751

@@ -2099,8 +2099,9 @@ def prepare_class_def(
20992099
# Preserve name from previous fine-grained incremental run.
21002100
global_name = defn.info.name
21012101
defn.fullname = defn.info._fullname
2102-
if defn.info.is_named_tuple:
2103-
# Named tuple nested within a class is stored in the class symbol table.
2102+
if defn.info.is_named_tuple or defn.info.typeddict_type:
2103+
# Named tuples and Typed dicts nested within a class are stored
2104+
# in the class symbol table.
21042105
self.add_symbol_skip_local(global_name, defn.info)
21052106
else:
21062107
self.globals[global_name] = SymbolTableNode(GDEF, defn.info)

mypy/semanal_typeddict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> tuple[bool, TypeInfo | N
101101
fields, types, statements, required_keys = self.analyze_typeddict_classdef_fields(defn)
102102
if fields is None:
103103
return True, None # Defer
104+
if self.api.is_func_scope() and "@" not in defn.name:
105+
defn.name += "@" + str(defn.line)
104106
info = self.build_typeddict_typeinfo(
105107
defn.name, fields, types, required_keys, defn.line, existing_info
106108
)

test-data/unit/check-incremental.test

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5135,7 +5135,6 @@ tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"
51355135
tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"
51365136

51375137
[case testNewAnalyzerIncrementalMethodNamedTuple]
5138-
51395138
import a
51405139
[file a.py]
51415140
from b import C
@@ -6540,3 +6539,24 @@ from typing_extensions import TypedDict
65406539
def test() -> None:
65416540
Counts = TypedDict("Counts", {k: int for k in "abc"}) # type: ignore
65426541
[builtins fixtures/dict.pyi]
6542+
6543+
[case testNoIncrementalCrashOnTypedDictMethod]
6544+
import a
6545+
[file a.py]
6546+
from b import C
6547+
x: C
6548+
[file a.py.2]
6549+
from b import C
6550+
x: C
6551+
reveal_type(x.h)
6552+
[file b.py]
6553+
from typing_extensions import TypedDict
6554+
class C:
6555+
def __init__(self) -> None:
6556+
self.h: Hidden
6557+
class Hidden(TypedDict):
6558+
x: int
6559+
[builtins fixtures/dict.pyi]
6560+
[out]
6561+
[out2]
6562+
tmp/a.py:3: note: Revealed type is "TypedDict('b.C.Hidden@5', {'x': builtins.int})"

0 commit comments

Comments
 (0)