Skip to content

Commit 101b467

Browse files
committed
Make enums idempotent
1 parent 053c054 commit 101b467

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

mypy/checkexpr.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,14 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
627627
for p in self.chk.options.untyped_calls_exclude
628628
):
629629
self.msg.untyped_function_call(callee_type, e)
630-
630+
if isinstance(callee_type, Instance) and callee_type.type.has_base("enum.Enum"):
631+
if e.args:
632+
arg_type = get_proper_type(self.accept(e.args[0]))
633+
if (
634+
isinstance(arg_type, LiteralType)
635+
and arg_type.fallback.type == callee_type.type
636+
):
637+
return arg_type
631638
ret_type = self.check_call_expr_with_callee_type(
632639
callee_type, e, fullname, object_type, member
633640
)

test-data/unit/check-expressions.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,3 +2492,16 @@ x + T # E: Unsupported left operand type for + ("int")
24922492
T() # E: "TypeVar" not callable
24932493
[builtins fixtures/tuple.pyi]
24942494
[typing fixtures/typing-full.pyi]
2495+
2496+
[case testEnumIdempotency]
2497+
[builtins fixtures/tuple.pyi]
2498+
from enum import Enum
2499+
2500+
class Color(Enum):
2501+
RED = 1
2502+
BLUE = 2
2503+
2504+
def f() -> Color:
2505+
return Color(Color.RED)
2506+
2507+
[out]

0 commit comments

Comments
 (0)