Skip to content

Commit

Permalink
Fix crash on malformed TypedDict in incremental mode (#16115)
Browse files Browse the repository at this point in the history
Fixes #15557

FWIW I simply copy the logic for handling malformed definitions from
named tuples, that seems to be much more robust.
  • Loading branch information
ilevkivskyi committed Sep 15, 2023
1 parent d77310a commit 402c8ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,13 @@ def check_typeddict(
name, items, types, total, tvar_defs, ok = res
if not ok:
# Error. Construct dummy return value.
info = self.build_typeddict_typeinfo("TypedDict", [], [], set(), call.line, None)
if var_name:
name = var_name
if is_func_scope:
name += "@" + str(call.line)
else:
name = var_name = "TypedDict@" + str(call.line)
info = self.build_typeddict_typeinfo(name, [], [], set(), call.line, None)
else:
if var_name is not None and name != var_name:
self.fail(
Expand Down Expand Up @@ -395,9 +401,9 @@ def check_typeddict(
name, items, types, required_keys, call.line, existing_info
)
info.line = node.line
# Store generated TypeInfo under both names, see semanal_namedtuple for more details.
if name != var_name or is_func_scope:
self.api.add_symbol_skip_local(name, info)
# Store generated TypeInfo under both names, see semanal_namedtuple for more details.
if name != var_name or is_func_scope:
self.api.add_symbol_skip_local(name, info)
if var_name:
self.api.add_symbol(var_name, info, node)
call.analyzed = TypedDictExpr(info)
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -6479,3 +6479,28 @@ from typing_extensions import TypeVarTuple, Unpack
Ts = TypeVarTuple("Ts")
class C(Tuple[Unpack[Ts]]): ...
[builtins fixtures/tuple.pyi]

[case testNoIncrementalCrashOnInvalidTypedDict]
import m
[file m.py]
import counts
[file m.py.2]
import counts
# touch
[file counts.py]
from typing_extensions import TypedDict
Counts = TypedDict("Counts", {k: int for k in "abc"}) # type: ignore
[builtins fixtures/dict.pyi]

[case testNoIncrementalCrashOnInvalidTypedDictFunc]
import m
[file m.py]
import counts
[file m.py.2]
import counts
# touch
[file counts.py]
from typing_extensions import TypedDict
def test() -> None:
Counts = TypedDict("Counts", {k: int for k in "abc"}) # type: ignore
[builtins fixtures/dict.pyi]

0 comments on commit 402c8ff

Please sign in to comment.