From 0c4f48e5bf5ffa7ce5508244c5efc46830b11ab5 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 30 Jun 2023 16:45:42 +0300 Subject: [PATCH 1/6] Add `_generate_next_value` to `StrEnum` This change is enough to (at least partially) fix https://github.com/python/typeshed/issues/10384 Why? Because `mypy` already had this plugin in place: https://github.com/python/mypy/blob/b995e1620789a3ab2fc5dbcf0698a9077b0f5731/mypy/plugins/enums.py#L85-L90 So, adding it locally produces: ```python from enum import StrEnum, auto class A(StrEnum): a = auto() reveal_type(type(A.a)) # N: Revealed type is "Type[Literal[ex.A.a]?]" reveal_type(type(A.a.value)) # N: Revealed type is "Type[builtins.str]" ``` Closes https://github.com/python/typeshed/issues/10384 --- stdlib/enum.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index 0ca118a0e134..4ca1341507a1 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -237,6 +237,8 @@ if sys.version_info >= (3, 11): _value_: str @_magic_enum_attr def value(self) -> str: ... + @staticmethod + def _generate_next_value_(name: str, start: Unused, count: Unused, last_values: Unused) -> str: ... class EnumCheck(StrEnum): CONTINUOUS: str From b5ada058fc72a6f22aea64c3baf30d262c4e590a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Jun 2023 16:52:32 +0300 Subject: [PATCH 2/6] Fix stubtest on 3.11 --- tests/stubtest_allowlists/py311.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_allowlists/py311.txt b/tests/stubtest_allowlists/py311.txt index d6e1754b1c4e..b2a0f6444887 100644 --- a/tests/stubtest_allowlists/py311.txt +++ b/tests/stubtest_allowlists/py311.txt @@ -17,6 +17,7 @@ configparser.LegacyInterpolation.__init__ configparser.ParsingError.filename enum.Enum.__init__ enum.Enum._generate_next_value_ +enum.StrEnum._generate_next_value_ importlib.abc.Finder.find_module ipaddress._BaseNetwork.broadcast_address ipaddress._BaseNetwork.hostmask From b281063b55d5f4551b558de9c2a8da28dfc9d488 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 30 Jun 2023 16:58:42 +0300 Subject: [PATCH 3/6] Update tests/stubtest_allowlists/py311.txt --- tests/stubtest_allowlists/py311.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_allowlists/py311.txt b/tests/stubtest_allowlists/py311.txt index b2a0f6444887..38eef893cbf7 100644 --- a/tests/stubtest_allowlists/py311.txt +++ b/tests/stubtest_allowlists/py311.txt @@ -17,6 +17,7 @@ configparser.LegacyInterpolation.__init__ configparser.ParsingError.filename enum.Enum.__init__ enum.Enum._generate_next_value_ +# Python3.11 used to dynamically add this method: enum.StrEnum._generate_next_value_ importlib.abc.Finder.find_module ipaddress._BaseNetwork.broadcast_address From fe12e0abcd4dbdd0fd78ecb4037a5dd9d45198ff Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 30 Jun 2023 17:14:15 +0300 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Alex Waygood --- stdlib/enum.pyi | 2 +- tests/stubtest_allowlists/py311.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index 4ca1341507a1..96a96dbce10e 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -238,7 +238,7 @@ if sys.version_info >= (3, 11): @_magic_enum_attr def value(self) -> str: ... @staticmethod - def _generate_next_value_(name: str, start: Unused, count: Unused, last_values: Unused) -> str: ... + def _generate_next_value_(name: str, start: int, count: int, last_values: list[str]) -> str: ... class EnumCheck(StrEnum): CONTINUOUS: str diff --git a/tests/stubtest_allowlists/py311.txt b/tests/stubtest_allowlists/py311.txt index 38eef893cbf7..3b7809429f39 100644 --- a/tests/stubtest_allowlists/py311.txt +++ b/tests/stubtest_allowlists/py311.txt @@ -17,7 +17,7 @@ configparser.LegacyInterpolation.__init__ configparser.ParsingError.filename enum.Enum.__init__ enum.Enum._generate_next_value_ -# Python3.11 used to dynamically add this method: +# Not strictly speaking a staticmethod on 3.11, but it acts like one: enum.StrEnum._generate_next_value_ importlib.abc.Finder.find_module ipaddress._BaseNetwork.broadcast_address From 1ce923f6739c8a96df084965864e767c647cce96 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 1 Jul 2023 18:15:38 +0100 Subject: [PATCH 5/6] Add a test case --- test_cases/stdlib/check_enum.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test_cases/stdlib/check_enum.py diff --git a/test_cases/stdlib/check_enum.py b/test_cases/stdlib/check_enum.py new file mode 100644 index 000000000000..c59dfb2dc92b --- /dev/null +++ b/test_cases/stdlib/check_enum.py @@ -0,0 +1,12 @@ +from __future__ import annotations + +import enum +import sys +from typing_extensions import Literal, assert_type + +if sys.version_info >= (3, 11): + class Foo(enum.StrEnum): + X = enum.auto() + + assert_type(Foo.X, Literal[Foo.X]) + assert_type(Foo.X.value, str) From e46f82aff1b1b46739be6eb27d9d51d9cdc4153c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 1 Jul 2023 17:16:55 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- test_cases/stdlib/check_enum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_cases/stdlib/check_enum.py b/test_cases/stdlib/check_enum.py index c59dfb2dc92b..a93e1fae1182 100644 --- a/test_cases/stdlib/check_enum.py +++ b/test_cases/stdlib/check_enum.py @@ -5,6 +5,7 @@ from typing_extensions import Literal, assert_type if sys.version_info >= (3, 11): + class Foo(enum.StrEnum): X = enum.auto()