Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add EnumMeta.__prepare__ #7243

Merged
merged 1 commit into from
Feb 20, 2022
Merged

Conversation

AlexWaygood
Copy link
Member

I don't really understand why this needs the # type: ignores, but mypy seems absolutely convinced that this method breaks LSP!

@github-actions
Copy link
Contributor

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

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

It breaks LSP on 3.8 by not accepting **kwds, not sure about on 3.9 (will check the PR out locally later and see)

@hauntsaninja
Copy link
Collaborator

Looking into the LSP complaint on 3.9:

stdlib/enum.pyi:85: error: Signature of "__prepare__" incompatible with supertype "type"  [override]
stdlib/enum.pyi:85: note:      Superclass:
stdlib/enum.pyi:85: note:          def __prepare__(metacls, str, Tuple[type, ...], **kwds: Any) -> Mapping[str, object]
stdlib/enum.pyi:85: note:      Subclass:
stdlib/enum.pyi:85: note:          @classmethod
stdlib/enum.pyi:85: note:          def __prepare__(metacls, cls: str, bases: Tuple[type, ...], **kwds: Any) -> _EnumDict

It goes away if you mark the args as positional-only, which seems incorrect:

diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi
index 8ca0a5f8..0c4a2943 100644
--- a/stdlib/enum.pyi
+++ b/stdlib/enum.pyi
@@ -82,7 +82,7 @@ class EnumMeta(ABCMeta):
 
     if sys.version_info >= (3, 9):
         @classmethod
-        def __prepare__(metacls, cls: str, bases: tuple[type, ...], **kwds: Any) -> _EnumDict: ...  # type: ignore[override]
+        def __prepare__(metacls, __cls: str, __bases: tuple[type, ...], **kwds: Any) -> _EnumDict: ...
     else:
         @classmethod
         def __prepare__(metacls, cls: str, bases: tuple[type, ...]) -> _EnumDict: ...  # type: ignore[override]

(Also the error message seems to drop the classmethod on the superclass, but based on tests I think that might just be a reporting issue). So I'm happy to chalk this up to mypy bug.

@JelleZijlstra
Copy link
Member

Thanks for checking! Wasn't there some bug where the @classmethod decorator in this message sometimes gets eaten by the cache? Anyway, this does look like a mypy bug.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 21, 2022

I mean I guess technically mypy has a point in that: x.__prepare__(metaclass, cls, bases, cls=100) will get you a TypeError if it turns out your x is an EnumMeta and not a type. But I'm guessing the complaint might not be intentional.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants