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

5.0: Update django.db.models.enums #2148

Merged
merged 1 commit into from
May 12, 2024
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
20 changes: 14 additions & 6 deletions django-stubs/db/models/enums.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ _Self = TypeVar("_Self")

if sys.version_info >= (3, 11):
_enum_property = enum.property
EnumType = enum.EnumType
IntEnum = enum.IntEnum
StrEnum = enum.StrEnum
else:
_enum_property = property
EnumType = enum.EnumMeta

class ChoicesMeta(enum.EnumMeta):
class ReprEnum(enum.Enum): ...
class IntEnum(int, ReprEnum): ...
class StrEnum(str, ReprEnum): ...

class ChoicesMeta(EnumType):
# There's a contradiction between mypy and PYI019 regarding metaclasses. Where mypy
# disallows 'typing_extensions.Self' on metaclasses, while PYI019 try to enforce
# 'typing_extensions.Self' for '__new__' methods.. We've chosen to ignore the
Expand All @@ -31,7 +39,7 @@ class ChoicesMeta(enum.EnumMeta):

ChoicesType: TypeAlias = ChoicesMeta

class Choices(enum.Enum, metaclass=ChoicesMeta):
class Choices(enum.Enum, metaclass=ChoicesType):
@property
def label(self) -> str: ...
@_enum_property
Expand All @@ -41,26 +49,26 @@ class Choices(enum.Enum, metaclass=ChoicesMeta):

# fake, to keep simulate class properties
@type_check_only
class _IntegerChoicesMeta(ChoicesMeta):
class _IntegerChoicesMeta(ChoicesType):
@property
def choices(self) -> list[tuple[int, str]]: ...
@property
def values(self) -> list[int]: ...

class IntegerChoices(int, Choices, metaclass=_IntegerChoicesMeta):
class IntegerChoices(Choices, IntEnum, metaclass=_IntegerChoicesMeta):
def __new__(cls, value: int) -> Self: ...
@_enum_property
def value(self) -> int: ...

# fake, to keep simulate class properties
@type_check_only
class _TextChoicesMeta(ChoicesMeta):
class _TextChoicesMeta(ChoicesType):
@property
def choices(self) -> list[tuple[str, str]]: ...
@property
def values(self) -> list[str]: ...

class TextChoices(str, Choices, metaclass=_TextChoicesMeta):
class TextChoices(Choices, StrEnum, metaclass=_TextChoicesMeta):
def __new__(cls, value: str) -> Self: ...
@_enum_property
def value(self) -> str: ...
Loading