mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 12:44:28 +08:00
Improve enum stubs (#7362)
- Improve TypeVar names - `object` -> `Any` in `Enum.__new__` (there _are_ restrictions on the kinds of objects that can be passed in, they're just not expressable in the stubs. - Delete pointless `Self | int` unions, since `Self` is a subtype of `int` for these methods.
This commit is contained in:
@@ -38,8 +38,8 @@ if sys.version_info >= (3, 11):
|
||||
else:
|
||||
__all__ = ["EnumMeta", "Enum", "IntEnum", "Flag", "IntFlag", "auto", "unique"]
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_S = TypeVar("_S", bound=type[Enum])
|
||||
_EnumMemberT = TypeVar("_EnumMemberT")
|
||||
_EnumerationT = TypeVar("_EnumerationT", bound=type[Enum])
|
||||
|
||||
# The following all work:
|
||||
# >>> from enum import Enum
|
||||
@@ -87,12 +87,12 @@ class EnumMeta(ABCMeta):
|
||||
@classmethod
|
||||
def __prepare__(metacls, cls: str, bases: tuple[type, ...]) -> _EnumDict: ... # type: ignore[override]
|
||||
|
||||
def __iter__(self: type[_T]) -> Iterator[_T]: ...
|
||||
def __reversed__(self: type[_T]) -> Iterator[_T]: ...
|
||||
def __iter__(self: type[_EnumMemberT]) -> Iterator[_EnumMemberT]: ...
|
||||
def __reversed__(self: type[_EnumMemberT]) -> Iterator[_EnumMemberT]: ...
|
||||
def __contains__(self: type[Any], member: object) -> bool: ...
|
||||
def __getitem__(self: type[_T], name: str) -> _T: ...
|
||||
def __getitem__(self: type[_EnumMemberT], name: str) -> _EnumMemberT: ...
|
||||
@_builtins_property
|
||||
def __members__(self: type[_T]) -> types.MappingProxyType[str, _T]: ...
|
||||
def __members__(self: type[_EnumMemberT]) -> types.MappingProxyType[str, _EnumMemberT]: ...
|
||||
def __len__(self) -> int: ...
|
||||
def __bool__(self) -> Literal[True]: ...
|
||||
def __setattr__(self, name: str, value: Any) -> None: ...
|
||||
@@ -100,7 +100,7 @@ class EnumMeta(ABCMeta):
|
||||
if sys.version_info >= (3, 11):
|
||||
# Simple value lookup
|
||||
@overload # type: ignore[override]
|
||||
def __call__(cls: type[_T], value: Any, names: None = ...) -> _T: ...
|
||||
def __call__(cls: type[_EnumMemberT], value: Any, names: None = ...) -> _EnumMemberT: ...
|
||||
# Functional Enum API
|
||||
@overload
|
||||
def __call__(
|
||||
@@ -116,7 +116,7 @@ class EnumMeta(ABCMeta):
|
||||
) -> type[Enum]: ...
|
||||
else:
|
||||
@overload # type: ignore[override]
|
||||
def __call__(cls: type[_T], value: Any, names: None = ...) -> _T: ...
|
||||
def __call__(cls: type[_EnumMemberT], value: Any, names: None = ...) -> _EnumMemberT: ...
|
||||
@overload
|
||||
def __call__(
|
||||
cls,
|
||||
@@ -157,7 +157,7 @@ class Enum(metaclass=EnumMeta):
|
||||
def _missing_(cls, value: object) -> Any: ...
|
||||
@staticmethod
|
||||
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any: ...
|
||||
def __new__(cls: type[Self], value: object) -> Self: ...
|
||||
def __new__(cls: type[Self], value: Any) -> Self: ...
|
||||
def __dir__(self) -> list[str]: ...
|
||||
def __format__(self, format_spec: str) -> str: ...
|
||||
def __hash__(self) -> Any: ...
|
||||
@@ -171,16 +171,16 @@ if sys.version_info >= (3, 11):
|
||||
_value_: int
|
||||
@property
|
||||
def value(self) -> int: ...
|
||||
def __new__(cls: type[Self], value: int | Self) -> Self: ...
|
||||
def __new__(cls: type[Self], value: int) -> Self: ...
|
||||
|
||||
else:
|
||||
class IntEnum(int, Enum):
|
||||
_value_: int
|
||||
@types.DynamicClassAttribute
|
||||
def value(self) -> int: ...
|
||||
def __new__(cls: type[Self], value: int | Self) -> Self: ...
|
||||
def __new__(cls: type[Self], value: int) -> Self: ...
|
||||
|
||||
def unique(enumeration: _S) -> _S: ...
|
||||
def unique(enumeration: _EnumerationT) -> _EnumerationT: ...
|
||||
|
||||
_auto_null: Any
|
||||
|
||||
@@ -210,7 +210,7 @@ class Flag(Enum):
|
||||
@types.DynamicClassAttribute
|
||||
def value(self) -> int: ...
|
||||
|
||||
def __contains__(self: _T, other: _T) -> bool: ...
|
||||
def __contains__(self: Self, other: Self) -> bool: ...
|
||||
def __bool__(self) -> bool: ...
|
||||
def __or__(self: Self, other: Self) -> Self: ...
|
||||
def __and__(self: Self, other: Self) -> Self: ...
|
||||
@@ -218,17 +218,17 @@ class Flag(Enum):
|
||||
def __invert__(self: Self) -> Self: ...
|
||||
|
||||
class IntFlag(int, Flag):
|
||||
def __new__(cls: type[Self], value: int | Self) -> Self: ...
|
||||
def __or__(self: Self, other: int | Self) -> Self: ...
|
||||
def __and__(self: Self, other: int | Self) -> Self: ...
|
||||
def __xor__(self: Self, other: int | Self) -> Self: ...
|
||||
def __ror__(self: Self, n: int | Self) -> Self: ...
|
||||
def __rand__(self: Self, n: int | Self) -> Self: ...
|
||||
def __rxor__(self: Self, n: int | Self) -> Self: ...
|
||||
def __new__(cls: type[Self], value: int) -> Self: ...
|
||||
def __or__(self: Self, other: int) -> Self: ...
|
||||
def __and__(self: Self, other: int) -> Self: ...
|
||||
def __xor__(self: Self, other: int) -> Self: ...
|
||||
def __ror__(self: Self, n: int) -> Self: ...
|
||||
def __rand__(self: Self, n: int) -> Self: ...
|
||||
def __rxor__(self: Self, n: int) -> Self: ...
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
class StrEnum(str, ReprEnum):
|
||||
def __new__(cls: type[Self], value: str | Self) -> Self: ...
|
||||
def __new__(cls: type[Self], value: str) -> Self: ...
|
||||
_value_: str
|
||||
@property
|
||||
def value(self) -> str: ...
|
||||
@@ -243,7 +243,7 @@ if sys.version_info >= (3, 11):
|
||||
|
||||
class verify:
|
||||
def __init__(self, *checks: EnumCheck) -> None: ...
|
||||
def __call__(self, enumeration: _S) -> _S: ...
|
||||
def __call__(self, enumeration: _EnumerationT) -> _EnumerationT: ...
|
||||
|
||||
class FlagBoundary(StrEnum):
|
||||
STRICT: str
|
||||
@@ -260,6 +260,6 @@ if sys.version_info >= (3, 11):
|
||||
name: str
|
||||
clsname: str
|
||||
def global_str(self: Enum) -> str: ...
|
||||
def global_enum(cls: _S, update_str: bool = ...) -> _S: ...
|
||||
def global_enum(cls: _EnumerationT, update_str: bool = ...) -> _EnumerationT: ...
|
||||
def global_enum_repr(self: Enum) -> str: ...
|
||||
def global_flag_repr(self: Flag) -> str: ...
|
||||
|
||||
Reference in New Issue
Block a user