FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Update `enum` for py312 by AlexWaygood · Pull Request #10670 · python/typeshed · GitHub

Repository navigation

Update enum for py312 - #10670

Merged
AlexWaygood merged 7 commits into
python:mainfrom
AlexWaygood:py32-enum
Sep 11, 2023
Merged

AlexWaygood merged 7 commits into
python:mainfrom
AlexWaygood:py32-enum

Conversation

Copy link
Copy Markdown
Member

No description provided.

This comment has been minimized.

Comment thread stdlib/enum.pyi
Comment on lines +154 to +163
# Overload 3 (py312+ only): Value lookup on an already existing enum class (complex case)
#
# >>> class Foo(enum.Enum):
# ... X = 1, 2, 3
# >>> Foo(1, 2, 3)
# <Foo.X: (1, 2, 3)>
#
if sys.version_info >= (3, 12):
@overload
def __call__(cls: type[_EnumMemberT], value: Any, *values: Any) -> _EnumMemberT: ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I added a third overload here rather than modifying the first overload, as if this were the first overload, there'd likely be cases where type checkers should be selecting the functional-API overload but would go for this one instead. It's a very lenient signature.

This comment has been minimized.

sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Consider adding tests for this new overload.

Y = 4, 5, 6


assert_type(EnumOfTuples((1, 2, 3)), EnumOfTuples)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@sobolevn, I tried adding this test:

Suggested change
assert_type(EnumOfTuples((1, 2, 3)), EnumOfTuples)
assert_type(EnumOfTuples((1, 2, 3)), EnumOfTuples)
if sys.version_info >= (3, 12):
assert_type(EnumOfTuples(1, 2, 3), EnumOfTuples)

Unfortunately, the test failed :(

test_cases\stdlib\check_enum.py:28: error:
Too many arguments for "EnumOfTuples"  [call-arg]
        assert_type(EnumOfTuples(1, 2, 3), EnumOfTuples)
                    ^~~~~~~~~~~~~~~~~~~~~


Test completed with errors

Do you think there's some special-casing in mypy that will also need to be updated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The tests I've added show that there is no regression w.r.t. existing functionality, anyway

sobolevn Sep 11, 2023 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The test continues to fail if I apply this diff, combining overloads 1 and 3:

Diff
diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi
index 10ea19257..157d09c84 100644
--- a/stdlib/enum.pyi
+++ b/stdlib/enum.pyi
@@ -120,9 +120,13 @@ class EnumMeta(type):
     def __bool__(self) -> Literal[True]: ...
     def __dir__(self) -> list[str]: ...

-    # Overload 1: Value lookup on an already existing enum class (simple case)
-    @overload
-    def __call__(cls: type[_EnumMemberT], value: Any, names: None = None) -> _EnumMemberT: ...
+    # Overload 1: Value lookup on an already existing enum class
+    if sys.version_info >= (3, 12):
+        @overload
+        def __call__(cls: type[_EnumMemberT], value: Any, names: Any | None = None, *values: Any) -> _EnumMemberT: ...
+    else:
+        @overload
+        def __call__(cls: type[_EnumMemberT], value: Any, names: None = None) -> _EnumMemberT: ...

     # Overload 2: Functional API for constructing new enum classes.
     if sys.version_info >= (3, 11):
@@ -151,17 +155,6 @@ class EnumMeta(type):
             start: int = 1,
         ) -> type[Enum]: ...

-    # Overload 3 (py312+ only): Value lookup on an already existing enum class (complex case)
-    #
-    # >>> class Foo(enum.Enum):
-    # ...     X = 1, 2, 3
-    # >>> Foo(1, 2, 3)
-    # <Foo.X: (1, 2, 3)>
-    #
-    if sys.version_info >= (3, 12):
-        @overload
-        def __call__(cls: type[_EnumMemberT], value: Any, *values: Any) -> _EnumMemberT: ...

So I'm not sure this is fixable in typeshed :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's report it to mypy 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yup, for now I've added a TODO 👍

This comment has been minimized.

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

AlexWaygood merged commit 23249c0 into python:main Sep 11, 2023
AlexWaygood deleted the py32-enum branch September 11, 2023 10:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL