-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Support enum iteration #1136
Support enum iteration #1136
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
# FIXME: Stub incomplete, ommissions include: | ||
# * the metaclass | ||
# * _sunder_ methods with their transformations | ||
|
||
import abc | ||
import sys | ||
from typing import List, Any, TypeVar, Union | ||
from typing import List, Any, TypeVar, Union, Iterable, Iterator, TypeVar, Generic, Type | ||
|
||
_T = TypeVar('_T', bound=Enum) | ||
|
||
class Enum: | ||
def __new__(cls, value: Any) -> None: ... | ||
class EnumMeta(abc.ABCMeta, Iterable[Enum]): | ||
def __iter__(self: Type[_T]) -> Iterator[_T]: ... # type: ignore | ||
|
||
class Enum(metaclass=EnumMeta): | ||
def __new__(cls: Type[_T], value: Any) -> _T: ... | ||
def __repr__(self) -> str: ... | ||
def __str__(self) -> str: ... | ||
def __dir__(self) -> List[str]: ... | ||
|
@@ -20,8 +22,6 @@ class Enum: | |
class IntEnum(int, Enum): | ||
value = ... # type: int | ||
|
||
_T = TypeVar('_T') | ||
|
||
def unique(enumeration: _T) -> _T: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this might break because of the new bound on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, hm, that might be by accident, or because something defaults to Any when mypy doesn't understand it. Would be nice to try this and see what the reveal_type(unique(Color)) actually says. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This looks right to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm...
Gives
(Sorry the line numbers are offset, it's about the last two lines.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, so it works differently when used as a decorator. That seems like a mypy bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's not how it's supposed to be used. As a class decorator it does indeed work as expected. Not sure why or how. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe to be safe we should give |
||
|
||
if sys.version_info >= (3, 6): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the enum.py code it inherits from
type
, notABCMeta
. Is there a reason to use ABCMeta here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, let me see if that works.