Skip to content

Commit cee41ae

Browse files
committed
Improve Enum comparison methods and update lint config
Refactored Enum comparison methods to return NotImplemented for invalid types, improving type safety. Updated pyproject.toml to ignore N807 linting rule for dunder methods.
1 parent 36f815f commit cee41ae

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

discord/enums.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,32 @@ class Enum(EnumBase):
9393
def __init_subclass__(cls, *, comparable: bool = False) -> None:
9494
super().__init_subclass__()
9595

96-
if comparable is True:
97-
cls.__lt__ = lambda self, other: isinstance(other, self.__class__) and self.value < other.value
98-
cls.__gt__ = lambda self, other: isinstance(other, self.__class__) and self.value > other.value
99-
cls.__le__ = lambda self, other: isinstance(other, self.__class__) and self.value <= other.value
100-
cls.__ge__ = lambda self, other: isinstance(other, self.__class__) and self.value >= other.value
96+
if comparable:
97+
98+
def __lt__(self: Enum, other: object) -> bool:
99+
if not isinstance(other, cls):
100+
return NotImplemented
101+
return self.value < other.value
102+
103+
def __gt__(self: Enum, other: object) -> bool:
104+
if not isinstance(other, cls):
105+
return NotImplemented
106+
return self.value > other.value
107+
108+
def __le__(self: Enum, other: object) -> bool:
109+
if not isinstance(other, cls):
110+
return NotImplemented
111+
return self.value <= other.value
112+
113+
def __ge__(self: Enum, other: object) -> bool:
114+
if not isinstance(other, cls):
115+
return NotImplemented
116+
return self.value >= other.value
117+
118+
cls.__lt__ = __lt__
119+
cls.__gt__ = __gt__
120+
cls.__le__ = __le__
121+
cls.__ge__ = __ge__
101122

102123
@classmethod
103124
def _missing_(cls, value: Any) -> Self:

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ extend-ignore = [
192192
"N803",
193193
"N804",
194194
"N806",
195+
"N807", # Function name should not start and end with `__` (dunder)
195196
"N811",
196197
"N818",
197198
"PERF102",
@@ -302,4 +303,4 @@ show_error_codes = true
302303
ignore_errors = true
303304

304305
[tool.pytest.ini_options]
305-
asyncio_mode = "auto"
306+
asyncio_mode = "auto"

0 commit comments

Comments
 (0)