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 crash on malformed TypedDict in incremental mode #16115

Merged
merged 1 commit into from
Sep 15, 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
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]