Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incremental crash on TypedDict in method #16364

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})"