Make name and value read-only for Enums, part II (#6578)

This commit is contained in:
Alex Waygood
2021-12-13 16:00:28 +00:00
committed by GitHub
parent 8a5d91ca37
commit cc054efa79
2 changed files with 29 additions and 6 deletions

View File

@@ -121,7 +121,12 @@ class Enum(metaclass=EnumMeta):
def __reduce_ex__(self, proto: object) -> Any: ...
class IntEnum(int, Enum):
value: int
if sys.version_info >= (3, 11):
@property
def value(self) -> int: ...
else:
@types.DynamicClassAttribute
def value(self) -> int: ...
def __new__(cls: Type[_T], value: int | _T) -> _T: ...
def unique(enumeration: _S) -> _S: ...
@@ -130,12 +135,25 @@ _auto_null: Any
# subclassing IntFlag so it picks up all implemented base functions, best modeling behavior of enum.auto()
class auto(IntFlag):
value: Any
if sys.version_info >= (3, 11):
@property
def value(self) -> Any: ...
else:
@types.DynamicClassAttribute
def value(self) -> Any: ...
def __new__(cls: Type[_T]) -> _T: ...
class Flag(Enum):
name: str | None # type: ignore[assignment]
value: int
if sys.version_info >= (3, 11):
@property
def name(self) -> str | None: ... # type: ignore[override]
@property
def value(self) -> int: ...
else:
@types.DynamicClassAttribute
def name(self) -> str | None: ... # type: ignore[override]
@types.DynamicClassAttribute
def value(self) -> int: ...
def __contains__(self: _T, other: _T) -> bool: ...
def __repr__(self) -> str: ...
def __str__(self) -> str: ...

View File

@@ -83,8 +83,13 @@ distutils.command.bdist_packager # It exists in docs as package name but not in
distutils.version.Version._cmp # class should have declared this
distutils.version.Version.parse # class should have declared this
email.headerregistry.BaseHeader.max_count # docs say subclasses should have this property
enum.Enum.name # A special property that exists at runtime, but stubtest can't detect https://github.com/python/typeshed/pull/6576#issuecomment-992538677
enum.Enum.value # A special property that exists at runtime, but stubtest can't detect https://github.com/python/typeshed/pull/6576#issuecomment-992538677
# Enum `name` and `value` are special properties that exists at runtime, but stubtest can't detect
# https://github.com/python/typeshed/pull/6576#issuecomment-992538677
enum.Enum.name
enum.Enum.value
enum.Flag.name
enum.Flag.value
enum.IntEnum.value
http.HTTPStatus.description # set in __new__
http.HTTPStatus.phrase # set in __new__
http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta