Skip to content

Commit

Permalink
5.0: Update django.db.models.enums (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHyuckSa authored May 12, 2024
1 parent f3dcf83 commit 85afae7
Showing 1 changed file with 14 additions and 6 deletions.
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: ...

0 comments on commit 85afae7

Please sign in to comment.