diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 73282c94be4e..166456e5d35e 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -627,7 +627,14 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> for p in self.chk.options.untyped_calls_exclude ): self.msg.untyped_function_call(callee_type, e) - + if isinstance(callee_type, Instance) and callee_type.type.has_base("enum.Enum"): + if e.args: + arg_type = get_proper_type(self.accept(e.args[0])) + if ( + isinstance(arg_type, LiteralType) + and arg_type.fallback.type == callee_type.type + ): + return arg_type ret_type = self.check_call_expr_with_callee_type( callee_type, e, fullname, object_type, member ) diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index ea6eac9a39b3..650b8f5fb0de 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -2492,3 +2492,16 @@ x + T # E: Unsupported left operand type for + ("int") T() # E: "TypeVar" not callable [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] + +[case testEnumIdempotency] +[builtins fixtures/tuple.pyi] +from enum import Enum + +class Color(Enum): + RED = 1 + BLUE = 2 + +def f() -> Color: + return Color(Color.RED) + +[out]