Skip to content

Commit c123dc0

Browse files
committed
Run enum.Enum analysis on non-assigned calls
1 parent 35e843c commit c123dc0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

mypy/semanal.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
infer_reachability_of_match_statement,
222222
)
223223
from mypy.scope import Scope
224-
from mypy.semanal_enum import EnumCallAnalyzer
224+
from mypy.semanal_enum import ENUM_BASES, EnumCallAnalyzer
225225
from mypy.semanal_namedtuple import NamedTupleAnalyzer
226226
from mypy.semanal_newtype import NewTypeAnalyzer
227227
from mypy.semanal_shared import (
@@ -5986,6 +5986,13 @@ def visit_call_expr(self, expr: CallExpr) -> None:
59865986
expr.analyzed.line = expr.line
59875987
expr.analyzed.column = expr.column
59885988
expr.analyzed.accept(self)
5989+
elif refers_to_fullname(expr.callee, ENUM_BASES):
5990+
assert isinstance(expr.callee, RefExpr)
5991+
for a in expr.args:
5992+
a.accept(self)
5993+
5994+
# ensure we get analytics about enum.Enum, even if we don't assign it
5995+
self.enum_call_analyzer.parse_enum_call_args(expr, expr.callee.fullname.split(".")[-1])
59895996
else:
59905997
# Normal call expression.
59915998
calculate_type_forms = TYPE_FORM in self.options.enable_incomplete_feature

mypy/semanal_enum.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535

3636
# Note: 'enum.EnumMeta' is deliberately excluded from this list. Classes that directly use
3737
# enum.EnumMeta do not necessarily automatically have the 'name' and 'value' attributes.
38-
ENUM_BASES: Final = frozenset(
39-
("enum.Enum", "enum.IntEnum", "enum.Flag", "enum.IntFlag", "enum.StrEnum")
40-
)
38+
ENUM_BASES: Final = ("enum.Enum", "enum.IntEnum", "enum.Flag", "enum.IntFlag", "enum.StrEnum")
4139
ENUM_SPECIAL_PROPS: Final = frozenset(
4240
(
4341
"name",

test-data/unit/check-enum.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,3 +2681,9 @@ reveal_type(Wrapper.Nested.FOO) # N: Revealed type is "Literal[__main__.Wrapper
26812681
reveal_type(Wrapper.Nested.FOO.value) # N: Revealed type is "builtins.ellipsis"
26822682
reveal_type(Wrapper.Nested.FOO._value_) # N: Revealed type is "builtins.ellipsis"
26832683
[builtins fixtures/enum.pyi]
2684+
2685+
[case testCatchesEnumCallWithoutAssignment]
2686+
import enum
2687+
2688+
enum.Enum() # E: Too few arguments for Enum()
2689+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)