Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 26 additions & 5 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,32 @@ class Enum(EnumBase):
def __init_subclass__(cls, *, comparable: bool = False) -> None:
super().__init_subclass__()

if comparable is True:
cls.__lt__ = lambda self, other: isinstance(other, self.__class__) and self.value < other.value
cls.__gt__ = lambda self, other: isinstance(other, self.__class__) and self.value > other.value
cls.__le__ = lambda self, other: isinstance(other, self.__class__) and self.value <= other.value
cls.__ge__ = lambda self, other: isinstance(other, self.__class__) and self.value >= other.value
if comparable:

def __lt__(self: Enum, other: object) -> bool:
if not isinstance(other, cls):
return NotImplemented
return self.value < other.value

def __gt__(self: Enum, other: object) -> bool:
if not isinstance(other, cls):
return NotImplemented
return self.value > other.value

def __le__(self: Enum, other: object) -> bool:
if not isinstance(other, cls):
return NotImplemented
return self.value <= other.value

def __ge__(self: Enum, other: object) -> bool:
if not isinstance(other, cls):
return NotImplemented
return self.value >= other.value

cls.__lt__ = __lt__
cls.__gt__ = __gt__
cls.__le__ = __le__
cls.__ge__ = __ge__

@classmethod
def _missing_(cls, value: Any) -> Self:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ extend-ignore = [
"N803",
"N804",
"N806",
"N807", # Function name should not start and end with `__` (dunder)
"N811",
"N818",
"PERF102",
Expand Down Expand Up @@ -302,4 +303,4 @@ show_error_codes = true
ignore_errors = true

[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_mode = "auto"