Skip to content

Commit

Permalink
Fix crash on invalid enum in method (#16511)
Browse files Browse the repository at this point in the history
Fixes #16163

Fix is straightforward: I simply copy the logic we have for invalid
TypedDicts/NamedTuples.
  • Loading branch information
ilevkivskyi committed Nov 17, 2023
1 parent 8c8aa10 commit 5489fd3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mypy/semanal_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,19 @@ class A(enum.Enum):
items, values, ok = self.parse_enum_call_args(call, fullname.split(".")[-1])
if not ok:
# Error. Construct dummy return value.
info = self.build_enum_call_typeinfo(var_name, [], fullname, node.line)
name = var_name
if is_func_scope:
name += "@" + str(call.line)
info = self.build_enum_call_typeinfo(name, [], fullname, node.line)
else:
name = cast(StrExpr, call.args[0]).value
if name != var_name or is_func_scope:
# Give it a unique name derived from the line number.
name += "@" + str(call.line)
info = self.build_enum_call_typeinfo(name, items, fullname, call.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)
call.analyzed = EnumCallExpr(info, items, values)
call.analyzed.set_line(call)
info.line = node.line
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -6560,3 +6560,26 @@ class C:
[out]
[out2]
tmp/a.py:3: note: Revealed type is "TypedDict('b.C.Hidden@5', {'x': builtins.int})"

[case testNoIncrementalCrashOnInvalidEnumMethod]
import a
[file a.py]
from lib import TheClass
[file a.py.2]
from lib import TheClass
x: TheClass
reveal_type(x.enum_type)
[file lib.py]
import enum

class TheClass:
def __init__(self) -> None:
names = ["foo"]
pyenum = enum.Enum('Blah', { # type: ignore[misc]
x.upper(): x
for x in names
})
self.enum_type = pyenum
[out]
[out2]
tmp/a.py:3: note: Revealed type is "def (value: builtins.object) -> lib.TheClass.pyenum@6"

0 comments on commit 5489fd3

Please sign in to comment.