Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions stdlib/3.4/enum.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# FIXME: Stub incomplete, ommissions include:
# * the metaclass
# * _sunder_ methods with their transformations

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(type, 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]: ...
Expand All @@ -20,8 +21,6 @@ class Enum:
class IntEnum(int, Enum):
value = ... # type: int

_T = TypeVar('_T')

def unique(enumeration: _T) -> _T: ...

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.

I thought this might break because of the new bound on _T, since this decorator applies to Enum classes, not Enum instances. But I tried it and it seems to work with 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.

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.

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.

$ cat ../bin/enumu.py 
import enum

@enum.unique
class X(enum.Enum):
    a = 1
    b = 2
reveal_type(X)

$ mypy --custom-typeshed-dir . ../bin/enumu.py 
../bin/enumu.py:7: error: Revealed type is 'def (value: Any) -> enumu.X'

This looks right to me.

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.

Hm...

import enum
class Colors(enum.Enum):
  RED = 1
  GREEN = 2
  BLUE= 3
X = unique(Colors)
reveal_type(X)

Gives

__tmp__.py:12: error: Type argument 1 of "unique" has incompatible value "Colors"
__tmp__.py:13: error: Revealed type is 'def (value: Any) -> __tmp__.Colors'

(Sorry the line numbers are offset, it's about the last two lines.)

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.

Interesting, so it works differently when used as a decorator. That seems like a mypy bug.

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.

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.

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.

Maybe to be safe we should give unique its own TypeVar for now.


if sys.version_info >= (3, 6):
Expand Down