Skip to content

Commit c8733e4

Browse files
feat: Replaced useless cached_property with property and moved to functools.cached_property
1 parent c0c0b7c commit c8733e4

File tree

8 files changed

+26
-64
lines changed

8 files changed

+26
-64
lines changed

discord/audit_logs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,12 @@ def _get_member(self, user_id: int) -> Member | User | None:
602602
def __repr__(self) -> str:
603603
return f"<AuditLogEntry id={self.id} action={self.action} user={self.user!r}>"
604604

605-
@utils.cached_property
605+
@property
606606
def created_at(self) -> datetime.datetime:
607607
"""Returns the entry's creation time in UTC."""
608608
return utils.snowflake_time(self.id)
609609

610-
@utils.cached_property
610+
@property
611611
def target(
612612
self,
613613
) -> (
@@ -631,24 +631,24 @@ def target(
631631
else:
632632
return converter(self._target_id)
633633

634-
@utils.cached_property
634+
@property
635635
def category(self) -> enums.AuditLogActionCategory:
636636
"""The category of the action, if applicable."""
637637
return self.action.category
638638

639-
@utils.cached_property
639+
@property
640640
def changes(self) -> AuditLogChanges:
641641
"""The list of changes this entry has."""
642642
obj = AuditLogChanges(self, self._changes, state=self._state)
643643
del self._changes
644644
return obj
645645

646-
@utils.cached_property
646+
@property
647647
def before(self) -> AuditLogDiff:
648648
"""The target's prior state."""
649649
return self.changes.before
650650

651-
@utils.cached_property
651+
@property
652652
def after(self) -> AuditLogDiff:
653653
"""The target's subsequent state."""
654654
return self.changes.after

discord/automod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ def __repr__(self) -> str:
416416
def __str__(self) -> str:
417417
return self.name
418418

419-
@cached_property
419+
@property
420420
def guild(self) -> Guild | None:
421421
"""The guild this rule belongs to."""
422422
return self._state._get_guild(self.guild_id)
423423

424-
@cached_property
424+
@property
425425
def creator(self) -> Member | None:
426426
"""The member who created this rule."""
427427
if self.guild is None:

discord/commands/context.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353

5454
from typing import Callable, Awaitable
5555

56-
from ..utils import cached_property
57-
5856
T = TypeVar("T")
5957
CogT = TypeVar("CogT", bound="Cog")
6058

@@ -136,53 +134,53 @@ async def invoke(
136134
"""
137135
return await command(self, *args, **kwargs)
138136

139-
@cached_property
137+
@property
140138
def channel(self) -> InteractionChannel | None:
141139
"""Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]:
142140
Returns the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel`.
143141
"""
144142
return self.interaction.channel
145143

146-
@cached_property
144+
@property
147145
def channel_id(self) -> int | None:
148146
"""Returns the ID of the channel associated with this context's command.
149147
Shorthand for :attr:`.Interaction.channel_id`.
150148
"""
151149
return self.interaction.channel_id
152150

153-
@cached_property
151+
@property
154152
def guild(self) -> Guild | None:
155153
"""Returns the guild associated with this context's command.
156154
Shorthand for :attr:`.Interaction.guild`.
157155
"""
158156
return self.interaction.guild
159157

160-
@cached_property
158+
@property
161159
def guild_id(self) -> int | None:
162160
"""Returns the ID of the guild associated with this context's command.
163161
Shorthand for :attr:`.Interaction.guild_id`.
164162
"""
165163
return self.interaction.guild_id
166164

167-
@cached_property
165+
@property
168166
def locale(self) -> str | None:
169167
"""Returns the locale of the guild associated with this context's command.
170168
Shorthand for :attr:`.Interaction.locale`.
171169
"""
172170
return self.interaction.locale
173171

174-
@cached_property
172+
@property
175173
def guild_locale(self) -> str | None:
176174
"""Returns the locale of the guild associated with this context's command.
177175
Shorthand for :attr:`.Interaction.guild_locale`.
178176
"""
179177
return self.interaction.guild_locale
180178

181-
@cached_property
179+
@property
182180
def app_permissions(self) -> Permissions:
183181
return self.interaction.app_permissions
184182

185-
@cached_property
183+
@property
186184
def me(self) -> Member | ClientUser | None:
187185
"""Union[:class:`.Member`, :class:`.ClientUser`]:
188186
Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message
@@ -194,14 +192,14 @@ def me(self) -> Member | ClientUser | None:
194192
else self.bot.user
195193
)
196194

197-
@cached_property
195+
@property
198196
def message(self) -> Message | None:
199197
"""Returns the message sent with this context's command.
200198
Shorthand for :attr:`.Interaction.message`, if applicable.
201199
"""
202200
return self.interaction.message
203201

204-
@cached_property
202+
@property
205203
def user(self) -> Member | User:
206204
"""Returns the user that sent this context's command.
207205
Shorthand for :attr:`.Interaction.user`.
@@ -220,7 +218,7 @@ def voice_client(self) -> VoiceClient | None:
220218

221219
return self.interaction.guild.voice_client
222220

223-
@cached_property
221+
@property
224222
def response(self) -> InteractionResponse:
225223
"""Returns the response object associated with this context's command.
226224
Shorthand for :attr:`.Interaction.response`.

discord/ext/commands/context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,28 +283,28 @@ def cog(self) -> Cog | None:
283283
return None
284284
return self.command.cog
285285

286-
@discord.utils.cached_property
286+
@property
287287
def guild(self) -> Guild | None:
288288
"""Returns the guild associated with this context's command.
289289
None if not available.
290290
"""
291291
return self.message.guild
292292

293-
@discord.utils.cached_property
293+
@property
294294
def channel(self) -> MessageableChannel:
295295
"""Returns the channel associated with this context's command.
296296
Shorthand for :attr:`.Message.channel`.
297297
"""
298298
return self.message.channel
299299

300-
@discord.utils.cached_property
300+
@property
301301
def author(self) -> User | Member:
302302
"""Union[:class:`~discord.User`, :class:`.Member`]:
303303
Returns the author associated with this context's command. Shorthand for :attr:`.Message.author`
304304
"""
305305
return self.message.author
306306

307-
@discord.utils.cached_property
307+
@property
308308
def me(self) -> Member | ClientUser:
309309
"""Union[:class:`.Member`, :class:`.ClientUser`]:
310310
Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message

discord/onboarding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from .enums import OnboardingMode, PromptType, try_enum
3030
from .partial_emoji import PartialEmoji
31-
from .utils import MISSING, cached_property, generate_snowflake, get
31+
from .utils import MISSING, generate_snowflake, get
3232

3333
if TYPE_CHECKING:
3434
from .abc import Snowflake
@@ -247,7 +247,7 @@ def _update(self, data: OnboardingPayload):
247247
self.enabled: bool = data["enabled"]
248248
self.mode: OnboardingMode = try_enum(OnboardingMode, data.get("mode"))
249249

250-
@cached_property
250+
@property
251251
def default_channels(
252252
self,
253253
) -> list[TextChannel | ForumChannel | VoiceChannel | Object]:

discord/poll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def __init__(
358358
self._expiry = None
359359
self._message = None
360360

361-
@utils.cached_property
361+
@property
362362
def expiry(self) -> datetime.datetime | None:
363363
"""An aware datetime object that specifies the date and time in UTC when the poll will end."""
364364
return utils.parse_time(self._expiry)

discord/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,6 @@ def __repr__(self) -> str:
121121
# error.
122122
MissingField = field(default_factory=lambda: MISSING)
123123

124-
125-
class _cached_property:
126-
def __init__(self, function):
127-
self.function = function
128-
self.__doc__ = getattr(function, "__doc__")
129-
130-
def __get__(self, instance, owner):
131-
if instance is None:
132-
return self
133-
134-
value = self.function(instance)
135-
setattr(instance, self.function.__name__, value)
136-
137-
return value
138-
139-
140124
if TYPE_CHECKING:
141125
from typing_extensions import ParamSpec
142126

@@ -150,12 +134,9 @@ def __get__(self, instance, owner):
150134
class _RequestLike(Protocol):
151135
headers: Mapping[str, Any]
152136

153-
cached_property = property
154-
155137
P = ParamSpec("P")
156138

157139
else:
158-
cached_property = _cached_property
159140
AutocompleteContext = Any
160141
OptionChoice = Any
161142

tests/test_utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
from discord.utils import (
3232
MISSING,
33-
_cached_property,
3433
_parse_ratelimit_header,
3534
_unique,
3635
async_all,
@@ -80,22 +79,6 @@ def test_temporary():
8079
# assert repr(MISSING) == '...'
8180
#
8281
#
83-
# def test_cached_property() -> None:
84-
# class Test:
85-
# def __init__(self, x: int):
86-
# self.x = x
87-
#
88-
# @_cached_property
89-
# def foo(self) -> int:
90-
# self.x += 1
91-
# return self.x
92-
#
93-
# t = Test(0)
94-
# assert isinstance(_cached_property.__get__(_cached_property(None), None, None), _cached_property)
95-
# assert t.foo == 1
96-
# assert t.foo == 1
97-
#
98-
#
9982
# def test_find_get() -> None:
10083
# class Obj:
10184
# def __init__(self, value: int):

0 commit comments

Comments
 (0)