From 101b4672801deda378e41efe99f4c94de4dca05b Mon Sep 17 00:00:00 2001 From: JTCRicardo Date: Fri, 3 Oct 2025 21:46:22 -0400 Subject: [PATCH 1/2] Make enums idempotent --- mypy/checkexpr.py | 9 ++++++++- test-data/unit/check-expressions.test | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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..7f3696e5eda0 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] \ No newline at end of file From 2524a0e49adc86b8c1e0fdb44548d9ff0a88ce23 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 01:53:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test-data/unit/check-expressions.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 7f3696e5eda0..650b8f5fb0de 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -2504,4 +2504,4 @@ class Color(Enum): def f() -> Color: return Color(Color.RED) -[out] \ No newline at end of file +[out]