Skip to content

Commit c961e36

Browse files
committed
♻️ replace custom cached_property implementation with functools.cached_property
1 parent 44f753e commit c961e36

File tree

7 files changed

+26
-79
lines changed

7 files changed

+26
-79
lines changed

discord/audit_logs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from __future__ import annotations
2727

2828
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Generator, TypeVar
29+
from functools import cached_property
2930

3031
from .utils.private import get_as_snowflake
3132
from . import enums, utils
@@ -569,12 +570,12 @@ def _get_member(self, user_id: int) -> Member | User | None:
569570
def __repr__(self) -> str:
570571
return f"<AuditLogEntry id={self.id} action={self.action} user={self.user!r}>"
571572

572-
@utils.cached_property
573+
@cached_property
573574
def created_at(self) -> datetime.datetime:
574575
"""Returns the entry's creation time in UTC."""
575576
return utils.snowflake_time(self.id)
576577

577-
@utils.cached_property
578+
@cached_property
578579
def target(
579580
self,
580581
) -> (
@@ -598,24 +599,24 @@ def target(
598599
else:
599600
return converter(self._target_id)
600601

601-
@utils.cached_property
602+
@property
602603
def category(self) -> enums.AuditLogActionCategory:
603604
"""The category of the action, if applicable."""
604605
return self.action.category
605606

606-
@utils.cached_property
607+
@cached_property
607608
def changes(self) -> AuditLogChanges:
608609
"""The list of changes this entry has."""
609610
obj = AuditLogChanges(self, self._changes, state=self._state)
610611
del self._changes
611612
return obj
612613

613-
@utils.cached_property
614+
@property
614615
def before(self) -> AuditLogDiff:
615616
"""The target's prior state."""
616617
return self.changes.before
617618

618-
@utils.cached_property
619+
@property
619620
def after(self) -> AuditLogDiff:
620621
"""The target's subsequent state."""
621622
return self.changes.after

discord/commands/context.py

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

5454
from typing import Callable, Awaitable
5555

56-
from ..utils import cached_property
5756
from ..utils.private import copy_doc
5857

5958
T = TypeVar("T")
@@ -137,68 +136,68 @@ async def invoke(
137136
"""
138137
return await command(self, *args, **kwargs)
139138

140-
@cached_property
139+
@property
141140
def channel(self) -> InteractionChannel | None:
142141
"""Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]:
143142
Returns the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel`.
144143
"""
145144
return self.interaction.channel
146145

147-
@cached_property
146+
@property
148147
def channel_id(self) -> int | None:
149148
"""Returns the ID of the channel associated with this context's command.
150149
Shorthand for :attr:`.Interaction.channel_id`.
151150
"""
152151
return self.interaction.channel_id
153152

154-
@cached_property
153+
@property
155154
def guild(self) -> Guild | None:
156155
"""Returns the guild associated with this context's command.
157156
Shorthand for :attr:`.Interaction.guild`.
158157
"""
159158
return self.interaction.guild
160159

161-
@cached_property
160+
@property
162161
def guild_id(self) -> int | None:
163162
"""Returns the ID of the guild associated with this context's command.
164163
Shorthand for :attr:`.Interaction.guild_id`.
165164
"""
166165
return self.interaction.guild_id
167166

168-
@cached_property
167+
@property
169168
def locale(self) -> str | None:
170169
"""Returns the locale of the guild associated with this context's command.
171170
Shorthand for :attr:`.Interaction.locale`.
172171
"""
173172
return self.interaction.locale
174173

175-
@cached_property
174+
@property
176175
def guild_locale(self) -> str | None:
177176
"""Returns the locale of the guild associated with this context's command.
178177
Shorthand for :attr:`.Interaction.guild_locale`.
179178
"""
180179
return self.interaction.guild_locale
181180

182-
@cached_property
181+
@property
183182
def app_permissions(self) -> Permissions:
184183
return self.interaction.app_permissions
185184

186-
@cached_property
185+
@property
187186
def me(self) -> Member | ClientUser | None:
188187
"""Union[:class:`.Member`, :class:`.ClientUser`]:
189188
Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message
190189
message contexts, or when :meth:`Intents.guilds` is absent.
191190
"""
192191
return self.interaction.guild.me if self.interaction.guild is not None else self.bot.user
193192

194-
@cached_property
193+
@property
195194
def message(self) -> Message | None:
196195
"""Returns the message sent with this context's command.
197196
Shorthand for :attr:`.Interaction.message`, if applicable.
198197
"""
199198
return self.interaction.message
200199

201-
@cached_property
200+
@property
202201
def user(self) -> Member | User:
203202
"""Returns the user that sent this context's command.
204203
Shorthand for :attr:`.Interaction.user`.
@@ -217,7 +216,7 @@ def voice_client(self) -> VoiceClient | None:
217216

218217
return self.interaction.guild.voice_client
219218

220-
@cached_property
219+
@property
221220
def response(self) -> InteractionResponse:
222221
"""Returns the response object associated with this context's command.
223222
Shorthand for :attr:`.Interaction.response`.

discord/ext/commands/context.py

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

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

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

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

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

discord/onboarding.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
from __future__ import annotations
2626

2727
from typing import TYPE_CHECKING, Any
28+
from functools import cached_property
2829

2930
from discord import utils
3031

3132
from .enums import OnboardingMode, PromptType, try_enum
3233
from .partial_emoji import PartialEmoji
33-
from .utils import MISSING, cached_property, generate_snowflake, find
34+
from .utils import MISSING, generate_snowflake, find
3435

3536
if TYPE_CHECKING:
3637
from .abc import Snowflake

discord/poll.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import datetime
2828
from typing import TYPE_CHECKING, Any
29+
from functools import cached_property
2930

3031
from .utils.private import parse_time
3132
from . import utils
@@ -342,7 +343,7 @@ def __init__(
342343
self._expiry = None
343344
self._message = None
344345

345-
@utils.cached_property
346+
@cached_property
346347
def expiry(self) -> datetime.datetime | None:
347348
"""An aware datetime object that specifies the date and time in UTC when the poll will end."""
348349
return parse_time(self._expiry)

discord/utils/__init__.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from __future__ import annotations
2727

2828
from typing import (
29-
TYPE_CHECKING,
3029
Any,
3130
AsyncIterator,
3231
Iterator,
@@ -79,42 +78,6 @@
7978
)
8079

8180

82-
class _cached_property:
83-
def __init__(self, function):
84-
self.function = function
85-
self.__doc__ = getattr(function, "__doc__")
86-
87-
def __get__(self, instance, owner):
88-
if instance is None:
89-
return self
90-
91-
value = self.function(instance)
92-
setattr(instance, self.function.__name__, value)
93-
94-
return value
95-
96-
97-
if TYPE_CHECKING:
98-
from typing_extensions import ParamSpec
99-
100-
class _RequestLike(Protocol):
101-
headers: Mapping[str, Any]
102-
103-
cached_property = property
104-
105-
P = ParamSpec("P")
106-
107-
else:
108-
cached_property = _cached_property
109-
AutocompleteContext = Any
110-
OptionChoice = Any
111-
112-
113-
T = TypeVar("T")
114-
T_co = TypeVar("T_co", covariant=True)
115-
_Iter = Union[Iterator[T], AsyncIterator[T]]
116-
117-
11881
async def get_or_fetch(obj, attr: str, id: int, *, default: Any = MISSING) -> Any:
11982
"""|coro|
12083

tests/test_utils.py

Lines changed: 0 additions & 18 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
find,
3534
generate_snowflake,
3635
snowflake_time,
@@ -73,23 +72,6 @@ def test_temporary():
7372
# assert not MISSING
7473
# assert repr(MISSING) == '...'
7574
#
76-
#
77-
# def test_cached_property() -> None:
78-
# class Test:
79-
# def __init__(self, x: int):
80-
# self.x = x
81-
#
82-
# @_cached_property
83-
# def foo(self) -> int:
84-
# self.x += 1
85-
# return self.x
86-
#
87-
# t = Test(0)
88-
# assert isinstance(_cached_property.__get__(_cached_property(None), None, None), _cached_property)
89-
# assert t.foo == 1
90-
# assert t.foo == 1
91-
#
92-
#
9375
# def test_find_get() -> None:
9476
# class Obj:
9577
# def __init__(self, value: int):

0 commit comments

Comments
 (0)