From cd02d9a2d4628b4b3f384061d3eb7d24f2e4cade Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Fri, 18 Nov 2022 18:11:02 +0000 Subject: [PATCH 1/3] Correctly handle Enum name on Python 3.11 --- mypy/semanal.py | 8 +++++++- test-data/unit/pythoneval.test | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 9b2b4ba44cce..538e37c030a9 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1448,7 +1448,13 @@ def visit_decorator(self, dec: Decorator) -> None: dec.var.is_classmethod = True self.check_decorated_function_is_method("classmethod", dec) elif refers_to_fullname( - d, ("builtins.property", "abc.abstractproperty", "functools.cached_property") + d, + ( + "builtins.property", + "abc.abstractproperty", + "functools.cached_property", + "enum.property", + ), ): removed.append(i) dec.func.is_property = True diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index f6336b48ee7b..649023add40d 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1712,3 +1712,20 @@ A = Type[int] | str B: TypeAlias = Type[int] | str [out] m.pyi:5: note: Revealed type is "typing._SpecialForm" + +[case testEnumNameWorkCorrectlyOn311] +# flags: --python-version 3.11 +from enum import Enum + +class E(Enum): + X = 1 + Y = 2 + +e: E +reveal_type(e.name) +reveal_type(e.value) +reveal_type(E.X.name) +[out] +_testEnumNameWorkCorrectlyOn311.py:9: note: Revealed type is "builtins.str" +_testEnumNameWorkCorrectlyOn311.py:10: note: Revealed type is "Union[Literal[1]?, Literal[2]?]" +_testEnumNameWorkCorrectlyOn311.py:11: note: Revealed type is "Literal['X']?" From 132d31a933cea7599965b5225a0526315ce53d60 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Fri, 18 Nov 2022 18:17:31 +0000 Subject: [PATCH 2/3] A better test --- test-data/unit/pythoneval.test | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 649023add40d..9bae8e1848e6 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1715,17 +1715,23 @@ m.pyi:5: note: Revealed type is "typing._SpecialForm" [case testEnumNameWorkCorrectlyOn311] # flags: --python-version 3.11 -from enum import Enum +import enum -class E(Enum): +class E(enum.Enum): X = 1 Y = 2 + @enum.property + def foo(self) -> int: ... e: E reveal_type(e.name) reveal_type(e.value) reveal_type(E.X.name) +reveal_type(e.foo) +reveal_type(E.Y.foo) [out] _testEnumNameWorkCorrectlyOn311.py:9: note: Revealed type is "builtins.str" _testEnumNameWorkCorrectlyOn311.py:10: note: Revealed type is "Union[Literal[1]?, Literal[2]?]" _testEnumNameWorkCorrectlyOn311.py:11: note: Revealed type is "Literal['X']?" +_testEnumNameWorkCorrectlyOn311.py:14: note: Revealed type is "builtins.int" +_testEnumNameWorkCorrectlyOn311.py:15: note: Revealed type is "builtins.int" From 4d717e39503393c34ebd865038027debfcb16a82 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Fri, 18 Nov 2022 18:48:58 +0000 Subject: [PATCH 3/3] Update line numbers --- test-data/unit/pythoneval.test | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 9bae8e1848e6..3f669246bb4e 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1730,8 +1730,8 @@ reveal_type(E.X.name) reveal_type(e.foo) reveal_type(E.Y.foo) [out] -_testEnumNameWorkCorrectlyOn311.py:9: note: Revealed type is "builtins.str" -_testEnumNameWorkCorrectlyOn311.py:10: note: Revealed type is "Union[Literal[1]?, Literal[2]?]" -_testEnumNameWorkCorrectlyOn311.py:11: note: Revealed type is "Literal['X']?" +_testEnumNameWorkCorrectlyOn311.py:11: note: Revealed type is "builtins.str" +_testEnumNameWorkCorrectlyOn311.py:12: note: Revealed type is "Union[Literal[1]?, Literal[2]?]" +_testEnumNameWorkCorrectlyOn311.py:13: note: Revealed type is "Literal['X']?" _testEnumNameWorkCorrectlyOn311.py:14: note: Revealed type is "builtins.int" _testEnumNameWorkCorrectlyOn311.py:15: note: Revealed type is "builtins.int"