Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add missing kwargs from Enum functional api signature #1710

Closed

Conversation

anentropic
Copy link
Contributor

@anentropic anentropic commented Nov 2, 2017

I'm making an attempt to address python/mypy#4184

from enum import Enum

# Error: Too many arguments for Enum()
PictureSize = Enum('PictureSize', 'P0 P1 P2 P3 P4 P5 P6 P7 P8', type=str, module=__name__)

to really support the type arg fully I think needs python/typing#213 ... then with the overloads decorator (since arg is optional) we could express that the return type is a subclass i.e. -> Type[Intersection[_T, _T2]]

but at the moment it's just kind of annotated and ignored

@@ -18,7 +19,7 @@ class EnumMeta(ABCMeta, Iterable[Enum], Sized, Reversible[Enum], Container[Enum]
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...

class Enum(metaclass=EnumMeta):
def __new__(cls: Type[_T], value: Any) -> _T: ...
def __new__(cls: Type[_T], value: str, names: Optional[str], module: Optional[str], qualname: Optional[str], type: Optional[Type[_T2]], start: Optional[int]) -> _T: ...
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is right—this method is for constructing enum instances, not enum classes. If anything, your change should be on EnumMeta.

Copy link
Member

Choose a reason for hiding this comment

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

So maybe we can't actually do much to fix this in typeshed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I must admit I find the Enum class rather baffling, I will look more into it

@anentropic
Copy link
Contributor Author

anentropic commented Nov 5, 2017

@JelleZijlstra

Looking into the Python source code for Enum I could see that these args actually belong on EnumMeta.__call__

I have added them there, however if I run mypy on my problem enum example with this modified typeshed I get:

.../typeshed/stdlib/3.4/enum.pyi:16: error: Signature of "__call__" incompatible with supertype "type"

but the signature of class type in typeshed builtins.py is

def __call__(self, *args: Any, **kwds: Any) -> Any: ...

so it seems like the signature of __call__ is not the cause of my original problem.

I guess as you say it's not fixable in typeshed and needs some magic in mypy to understand that Enum(...) does not instantiate an Enum, due to the wacky metaclass tricks being employed here.

@JelleZijlstra
Copy link
Member

Yes, so let's close this and attempt to fix the issue in mypy instead.

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.

None yet

2 participants