Skip to content

Commit dd2a89d

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3d1a96d + 6769f80 commit dd2a89d

File tree

6 files changed

+54
-56
lines changed

6 files changed

+54
-56
lines changed

changelog/660.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove the internal ``fill_with_flags`` decorator for flags classes and use the built in :meth:`object.__init_subclass__` method.

changelog/671.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update fields listed in :func:`on_user_update` and :func:`on_member_update` docs.

disnake/flags.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,6 @@ def all_flags_value(flags: Dict[str, int]) -> int:
117117
return functools.reduce(operator.or_, flags.values())
118118

119119

120-
def fill_with_flags(*, inverted: bool = False):
121-
def decorator(cls: Type[BF]) -> Type[BF]:
122-
cls.VALID_FLAGS = {}
123-
for name, value in cls.__dict__.items():
124-
if isinstance(value, flag_value):
125-
value._parent = cls
126-
cls.VALID_FLAGS[name] = value.flag
127-
128-
if inverted:
129-
cls.DEFAULT_VALUE = all_flags_value(cls.VALID_FLAGS)
130-
else:
131-
cls.DEFAULT_VALUE = 0
132-
133-
return cls
134-
135-
return decorator
136-
137-
138-
# n.b. flags must inherit from this and use the decorator above
139120
class BaseFlags:
140121
VALID_FLAGS: ClassVar[Dict[str, int]]
141122
DEFAULT_VALUE: ClassVar[int]
@@ -151,6 +132,29 @@ def __init__(self, **kwargs: bool):
151132
raise TypeError(f"{key!r} is not a valid flag name.")
152133
setattr(self, key, value)
153134

135+
@classmethod
136+
def __init_subclass__(cls, inverted: bool = False, no_fill_flags: bool = False):
137+
# add a way to bypass filling flags, eg for ListBaseFlags.
138+
if no_fill_flags:
139+
return cls
140+
141+
# use the parent's current flags as a base if they exist
142+
cls.VALID_FLAGS = getattr(cls, "VALID_FLAGS", {}).copy()
143+
144+
for name, value in cls.__dict__.items():
145+
if isinstance(value, flag_value):
146+
value._parent = cls
147+
cls.VALID_FLAGS[name] = value.flag
148+
149+
if not cls.VALID_FLAGS:
150+
raise RuntimeError(
151+
"At least one flag must be defined in a BaseFlags subclass, or 'no_fill_flags' must be set to True"
152+
)
153+
154+
cls.DEFAULT_VALUE = all_flags_value(cls.VALID_FLAGS) if inverted else 0
155+
156+
return cls
157+
154158
@classmethod
155159
def _from_value(cls, value: int) -> Self:
156160
self = cls.__new__(cls)
@@ -295,7 +299,7 @@ def _set_flag(self, o: int, toggle: bool) -> None:
295299
raise TypeError(f"Value to set for {self.__class__.__name__} must be a bool.")
296300

297301

298-
class ListBaseFlags(BaseFlags):
302+
class ListBaseFlags(BaseFlags, no_fill_flags=True):
299303
"""
300304
A base class for flags that aren't powers of 2.
301305
Instead, values are used as exponents to map to powers of 2 to avoid collisions,
@@ -330,8 +334,7 @@ def __repr__(self) -> str:
330334
return f"<{self.__class__.__name__} values={self.values}>"
331335

332336

333-
@fill_with_flags(inverted=True)
334-
class SystemChannelFlags(BaseFlags):
337+
class SystemChannelFlags(BaseFlags, inverted=True):
335338
"""
336339
Wraps up a Discord system channel flag value.
337340
@@ -466,7 +469,6 @@ def join_notification_replies(self):
466469
return 8
467470

468471

469-
@fill_with_flags()
470472
class MessageFlags(BaseFlags):
471473
"""
472474
Wraps up a Discord Message flag value.
@@ -621,7 +623,6 @@ def failed_to_mention_roles_in_thread(self):
621623
return 1 << 8
622624

623625

624-
@fill_with_flags()
625626
class PublicUserFlags(BaseFlags):
626627
"""
627628
Wraps up the Discord User Public flags.
@@ -814,7 +815,6 @@ def all(self) -> List[UserFlags]:
814815
return [public_flag for public_flag in UserFlags if self._has_flag(public_flag.value)]
815816

816817

817-
@fill_with_flags()
818818
class Intents(BaseFlags):
819819
"""
820820
Wraps up a Discord gateway intent flag.
@@ -1449,7 +1449,6 @@ def automod(self):
14491449
return (1 << 20) | (1 << 21)
14501450

14511451

1452-
@fill_with_flags()
14531452
class MemberCacheFlags(BaseFlags):
14541453
"""Controls the library's cache policy when it comes to members.
14551454
@@ -1632,7 +1631,6 @@ def _voice_only(self):
16321631
return self.value == 1
16331632

16341633

1635-
@fill_with_flags()
16361634
class ApplicationFlags(BaseFlags):
16371635
"""
16381636
Wraps up the Discord Application flags.
@@ -1777,7 +1775,6 @@ def gateway_message_content_limited(self):
17771775
return 1 << 19
17781776

17791777

1780-
@fill_with_flags()
17811778
class ChannelFlags(BaseFlags):
17821779
"""Wraps up the Discord Channel flags.
17831780
@@ -1872,7 +1869,6 @@ def pinned(self):
18721869
return 1 << 1
18731870

18741871

1875-
@fill_with_flags()
18761872
class AutoModKeywordPresets(ListBaseFlags):
18771873
"""
18781874
Wraps up the pre-defined auto moderation keyword lists, provided by Discord.

disnake/permissions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from functools import wraps
2929
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Dict, Iterator, Optional, Set, Tuple
3030

31-
from .flags import BaseFlags, alias_flag_value, fill_with_flags, flag_value
31+
from .flags import BaseFlags, alias_flag_value, flag_value
3232

3333
if TYPE_CHECKING:
3434
from typing_extensions import Self
@@ -68,7 +68,6 @@ def wrapped(cls):
6868
return wrapped
6969

7070

71-
@fill_with_flags()
7271
class Permissions(BaseFlags):
7372
"""Wraps up the Discord permission value.
7473

docs/api.rst

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,6 @@ This section documents events related to :class:`Client` and its connectivity to
362362
WebSocket library. It can be :class:`bytes` to denote a binary
363363
message or :class:`str` to denote a regular text message.
364364

365-
.. function:: on_user_update(before, after)
366-
367-
Called when a :class:`User` updates their profile.
368-
369-
This is called when one or more of the following things change:
370-
371-
- avatar
372-
- username
373-
- discriminator
374-
375-
This requires :attr:`Intents.members` to be enabled.
376-
377-
:param before: The updated user's old info.
378-
:type before: :class:`User`
379-
:param after: The updated user's updated info.
380-
:type after: :class:`User`
381-
382365
Channels/Threads
383366
~~~~~~~~~~~~~~~~
384367

@@ -851,21 +834,22 @@ Members
851834

852835
.. function:: on_member_update(before, after)
853836

854-
Called when a :class:`Member` updates their profile.
837+
Called when a :class:`Member` is updated.
855838

856839
This is called when one or more of the following things change, but is not limited to:
857840

841+
- avatar (guild-specific)
842+
- current_timeout
858843
- nickname
859-
- roles
860844
- pending
861-
- timeout
862-
- guild specific avatar
845+
- premium_since
846+
- roles
863847

864848
This requires :attr:`Intents.members` to be enabled.
865849

866-
:param before: The updated member's old info.
850+
:param before: The member's old info.
867851
:type before: :class:`Member`
868-
:param after: The updated member's updated info.
852+
:param after: The member's updated info.
869853
:type after: :class:`Member`
870854

871855
.. function:: on_member_ban(guild, user)
@@ -910,6 +894,24 @@ Members
910894
:param after: The updated member's updated info.
911895
:type after: :class:`Member`
912896

897+
.. function:: on_user_update(before, after)
898+
899+
Called when a :class:`User` is updated.
900+
901+
This is called when one or more of the following things change, but is not limited to:
902+
903+
- avatar
904+
- discriminator
905+
- name
906+
- public_flags
907+
908+
This requires :attr:`Intents.members` to be enabled.
909+
910+
:param before: The user's old info.
911+
:type before: :class:`User`
912+
:param after: The user's updated info.
913+
:type after: :class:`User`
914+
913915

914916
Scheduled Events
915917
++++++++++++++++

tests/test_flags.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import pytest
22

3-
from disnake.flags import ListBaseFlags, fill_with_flags, flag_value
3+
from disnake.flags import ListBaseFlags, flag_value
44

55

6-
@fill_with_flags()
76
class _ListFlags(ListBaseFlags):
87
@flag_value
98
def flag1(self):

0 commit comments

Comments
 (0)