Skip to content

refactor(enums)!: performance and functional improvements #556

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

Draft
wants to merge 54 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
eada261
ILOVEENUMSILOVEENUMSILOVEENUMSILOVEENUMSILOVEENUMS
Sharp-Eyes Feb 26, 2022
f0a4819
feat: merge, update, appease angry pyright gods
Sharp-Eyes Jun 9, 2022
17a83b4
fix: add missing types to enums where needed
Sharp-Eyes Jun 9, 2022
3e7ee1a
chore: fix CI issues
Sharp-Eyes Jun 9, 2022
b06526e
fix(tests): move properties, change error type
Sharp-Eyes Jun 11, 2022
94cf59a
fix: update stray enum to new syntax
Sharp-Eyes Jun 11, 2022
e60f8c9
chore: first pass at removing value for enum
onerandomusername Jun 12, 2022
e705a3d
docs: add docs to all `enums.py` enums
Sharp-Eyes Jun 12, 2022
34b9c86
fix: fix tests by fixing invalid escape sequence
Sharp-Eyes Jun 12, 2022
410cf00
docs: add docs to `commands.BucketType` enum
Sharp-Eyes Jun 12, 2022
b201d89
Merge branch 'master' into enums
Sharp-Eyes Jul 22, 2022
03a5507
feat: merge and fix failing checks
Sharp-Eyes Aug 14, 2022
fce05ef
feat: unit tests, minor fixes
Sharp-Eyes Aug 16, 2022
af8b09c
Merge branch 'master' into enums
Sharp-Eyes Aug 17, 2022
551886b
fix: tests
Sharp-Eyes Aug 17, 2022
bb8d001
fix: funny pyright funny fun fun i love pyright
Sharp-Eyes Aug 17, 2022
7c0fd82
Revert "chore: first pass at removing value for enum"
Sharp-Eyes Aug 23, 2022
7959bfa
chore: re-add missing param types and ignores
Sharp-Eyes Aug 23, 2022
dd147b0
chore: pyright bamboozle
Sharp-Eyes Aug 23, 2022
4124bbd
Merge branch 'master' into enums
Sharp-Eyes Aug 23, 2022
09c5a98
Merge branch 'master' into enums
Sharp-Eyes Aug 23, 2022
a4185f7
fix: readd missing __str__ dunder for Locales
onerandomusername Aug 24, 2022
8120f81
Merge branch 'enums' of https://github.com/Chromosomologist/disnake i…
Sharp-Eyes Aug 27, 2022
662e0bd
feat: optimize code and improve documentation
Sharp-Eyes Aug 29, 2022
f7564c2
chore: actually run pre-commit, whoops
Sharp-Eyes Aug 29, 2022
d46594a
Merge branch 'master' into enums
Sharp-Eyes Aug 29, 2022
e8f719e
feat: name/value speedups using setattr
Sharp-Eyes Aug 29, 2022
057f09b
Merge branch 'enums' of https://github.com/Chromosomologist/disnake i…
Sharp-Eyes Aug 29, 2022
0037617
fix: changelog filename I somehow managed to bean
Sharp-Eyes Aug 29, 2022
3175778
fix: revert now unnecessary check
Sharp-Eyes Aug 29, 2022
aa1ffb0
Merge branch 'master' into enums
onerandomusername Sep 2, 2022
2272fbc
feat: update enum inheritance
Sharp-Eyes Sep 3, 2022
b688696
Merge branch 'enums' of https://github.com/Chromosomologist/disnake i…
Sharp-Eyes Sep 3, 2022
ecabbec
fix: unfuck enums on python 3.8
Sharp-Eyes Sep 4, 2022
f8c0c07
feat: alias to existing member on value dupe
Sharp-Eyes Sep 4, 2022
0bb04d1
fix(tests): unfuckywucky the dupe test
Sharp-Eyes Sep 4, 2022
e85694c
Merge branch 'master' into enums
Sharp-Eyes Sep 4, 2022
726c826
chore(tests): use types.new_class instead of exec
Sharp-Eyes Sep 5, 2022
d192412
Merge branch 'master' into enums
onerandomusername Sep 6, 2022
5929b46
fix: uncomment `__str__` dunder for locales
Sharp-Eyes Sep 6, 2022
9fff055
feat: add handling for unknown values, add len
Sharp-Eyes Sep 9, 2022
313bf25
Merge branch 'master' into enums
onerandomusername Sep 9, 2022
0824971
fix: count enums like previous implementation
onerandomusername Sep 9, 2022
abfafc4
chore: undo docs changes for a separate PR
Sharp-Eyes Sep 16, 2022
d89c68d
Merge branch 'master' into enums
Sharp-Eyes Sep 16, 2022
d9930b1
feat: display value of unknown enum members
Sharp-Eyes Sep 20, 2022
b2e1e95
Merge branch 'master' into enums
Sharp-Eyes Sep 26, 2022
d8e927c
chore: re-add missing import, add license header
Sharp-Eyes Sep 26, 2022
2cfc928
feat: actually use brain for once
Sharp-Eyes Sep 26, 2022
4a3bd4a
Merge branch 'master' into enums
onerandomusername Sep 29, 2022
f818a76
fix(changelog): remove mention of reverted docs
Sharp-Eyes Sep 29, 2022
606392a
docs: re-add certain docstrings
Sharp-Eyes Sep 30, 2022
8ab78ef
Merge branch 'master' into enums
Sharp-Eyes Sep 30, 2022
797f338
feat: initial test updates
Sharp-Eyes Oct 19, 2022
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
5 changes: 5 additions & 0 deletions changelog/248.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rework :class:`disnake.enums.Enum` internals.
- Adds support for typed enums, removes support for untyped enums.
- Small performance increase by skipping removing the need for ``.value``.
- Closer to the vanilla python Enum api.
- Adds in-code documentation for all existin enums.
12 changes: 4 additions & 8 deletions disnake/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,7 @@ def to_dict(self) -> Dict[str, Any]:
if self._end:
timestamps["end"] = self._end

return {
"type": ActivityType.playing.value,
"name": str(self.name),
"timestamps": timestamps,
}
return {"type": ActivityType.playing, "name": str(self.name), "timestamps": timestamps}

def __eq__(self, other: Any) -> bool:
return isinstance(other, Game) and other.name == self.name
Expand Down Expand Up @@ -518,7 +514,7 @@ def twitch_name(self):

def to_dict(self) -> Dict[str, Any]:
ret: Dict[str, Any] = {
"type": ActivityType.streaming.value,
"type": ActivityType.streaming,
"name": str(self.name),
"url": str(self.url),
"assets": self.assets,
Expand Down Expand Up @@ -795,13 +791,13 @@ def to_dict(self) -> Dict[str, Any]:
o: Dict[str, Any]
if self.name == self.state:
o = {
"type": ActivityType.custom.value,
"type": ActivityType.custom,
"state": self.name,
"name": "Custom Status",
}
else:
o = {
"type": ActivityType.custom.value,
"type": ActivityType.custom,
"name": self.name,
}

Expand Down
6 changes: 3 additions & 3 deletions disnake/app_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ class APIUserCommand(UserCommand, _APIApplicationCommandMixin):
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.user.value:
if cmd_type != ApplicationCommandType.user:
raise ValueError(f"Invalid payload type for UserCommand: {cmd_type}")

self = cls(
Expand Down Expand Up @@ -775,7 +775,7 @@ class APIMessageCommand(MessageCommand, _APIApplicationCommandMixin):
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.message.value:
if cmd_type != ApplicationCommandType.message:
raise ValueError(f"Invalid payload type for MessageCommand: {cmd_type}")

self = cls(
Expand Down Expand Up @@ -968,7 +968,7 @@ class APISlashCommand(SlashCommand, _APIApplicationCommandMixin):
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.chat_input.value:
if cmd_type != ApplicationCommandType.chat_input:
raise ValueError(f"Invalid payload type for SlashCommand: {cmd_type}")

self = cls(
Expand Down
14 changes: 7 additions & 7 deletions disnake/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ def type(self) -> Literal[ChannelType.text, ChannelType.news]:

This always returns :attr:`ChannelType.text` or :attr:`ChannelType.news`.
"""
if self._type == ChannelType.text.value:
if self._type == ChannelType.text: # pyright: ignore[reportUnnecessaryComparison]
return ChannelType.text
return ChannelType.news

@property
def _sorting_bucket(self) -> int:
return ChannelType.text.value
return ChannelType.text

@utils.copy_doc(disnake.abc.GuildChannel.permissions_for)
def permissions_for(
Expand Down Expand Up @@ -288,7 +288,7 @@ def is_news(self) -> bool:

:return type: :class:`bool`
"""
return self._type == ChannelType.news.value
return self._type == ChannelType.news # pyright: ignore[reportUnnecessaryComparison]

@property
def last_message(self) -> Optional[Message]:
Expand Down Expand Up @@ -1035,7 +1035,7 @@ def _update(self, guild: Guild, data: Union[VoiceChannelPayload, StageChannelPay

@property
def _sorting_bucket(self) -> int:
return ChannelType.voice.value
return ChannelType.voice

@property
def members(self) -> List[Member]:
Expand Down Expand Up @@ -2127,7 +2127,7 @@ def _update(self, guild: Guild, data: CategoryChannelPayload) -> None:

@property
def _sorting_bucket(self) -> int:
return ChannelType.category.value
return ChannelType.category

@property
def type(self) -> Literal[ChannelType.category]:
Expand Down Expand Up @@ -2557,7 +2557,7 @@ def type(self) -> Literal[ChannelType.forum]:

@property
def _sorting_bucket(self) -> int:
return ChannelType.text.value
return ChannelType.text

@utils.copy_doc(disnake.abc.GuildChannel.permissions_for)
def permissions_for(
Expand Down Expand Up @@ -2996,7 +2996,7 @@ async def create_thread(
name=name,
auto_archive_duration=auto_archive_duration or self.default_auto_archive_duration,
rate_limit_per_user=slowmode_delay or 0,
type=ChannelType.public_thread.value,
type=ChannelType.public_thread,
files=params.files,
flags=flags,
reason=reason,
Expand Down
2 changes: 1 addition & 1 deletion disnake/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class TextInput(Component):
__repr_info__: ClassVar[Tuple[str, ...]] = __slots__

def __init__(self, data: TextInputPayload) -> None:
style = data.get("style", TextInputStyle.short.value)
style = data.get("style", TextInputStyle.short)

self.type: ComponentType = try_enum(ComponentType, data["type"])
self.custom_id: str = data["custom_id"]
Expand Down
Loading