Skip to content

Commit 9e23f4b

Browse files
RiccardoVaccariPaillat-dev
authored andcommitted
feat: Replaced useless cached_property with property and moved to functools.cached_property (Pycord-Development#2769)
* feat: Replaced useless `cached_property` with `property` and moved to `functools.cached_property` * style(pre-commit): auto fixes from pre-commit.com hooks * chore: switch to `functools.cached_property` for intensive code * style(pre-commit): auto fixes from pre-commit.com hooks * Update context.py Signed-off-by: Paillat <[email protected]> --------- Signed-off-by: Paillat <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <[email protected]> (cherry picked from commit 7ab3af9)
1 parent d9eda95 commit 9e23f4b

File tree

8 files changed

+38
-75
lines changed

8 files changed

+38
-75
lines changed

discord/audit_logs.py

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

571-
@utils.cached_property
571+
@property
572572
def created_at(self) -> datetime.datetime:
573573
"""Returns the entry's creation time in UTC."""
574574
return utils.snowflake_time(self.id)
575575

576-
@utils.cached_property
576+
@property
577577
def target(
578578
self,
579579
) -> (
@@ -597,24 +597,24 @@ def target(
597597
else:
598598
return converter(self._target_id)
599599

600-
@utils.cached_property
600+
@property
601601
def category(self) -> enums.AuditLogActionCategory:
602602
"""The category of the action, if applicable."""
603603
return self.action.category
604604

605-
@utils.cached_property
605+
@property
606606
def changes(self) -> AuditLogChanges:
607607
"""The list of changes this entry has."""
608608
obj = AuditLogChanges(self, self._changes, state=self._state)
609609
del self._changes
610610
return obj
611611

612-
@utils.cached_property
612+
@property
613613
def before(self) -> AuditLogDiff:
614614
"""The target's prior state."""
615615
return self.changes.before
616616

617-
@utils.cached_property
617+
@property
618618
def after(self) -> AuditLogDiff:
619619
"""The target's subsequent state."""
620620
return self.changes.after

discord/automod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,12 @@ def __repr__(self) -> str:
406406
def __str__(self) -> str:
407407
return self.name
408408

409-
@cached_property
409+
@property
410410
def guild(self) -> Guild | None:
411411
"""The guild this rule belongs to."""
412412
return self._state._get_guild(self.guild_id)
413413

414-
@cached_property
414+
@property
415415
def creator(self) -> Member | None:
416416
"""The member who created this rule."""
417417
if self.guild is None:

discord/commands/context.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,25 @@
3232
from discord.webhook.async_ import Webhook
3333

3434
if TYPE_CHECKING:
35+
from typing import Awaitable, Callable
36+
3537
from typing_extensions import ParamSpec
3638

3739
import discord
38-
from .. import Bot
39-
from ..state import ConnectionState
40-
from ..voice_client import VoiceClient
4140

42-
from .core import ApplicationCommand, Option
43-
from ..interactions import InteractionChannel
41+
from .. import Bot
42+
from ..client import ClientUser
43+
from ..cog import Cog
4444
from ..guild import Guild
45+
from ..interactions import InteractionChannel
4546
from ..member import Member
4647
from ..message import Message
47-
from ..user import User
4848
from ..permissions import Permissions
49-
from ..client import ClientUser
50-
51-
from ..cog import Cog
49+
from ..state import ConnectionState
50+
from ..user import User
51+
from ..voice_client import VoiceClient
5252
from ..webhook import WebhookMessage
53-
54-
from typing import Callable, Awaitable
55-
56-
from ..utils import cached_property
53+
from .core import ApplicationCommand, Option
5754

5855
T = TypeVar("T")
5956
CogT = TypeVar("CogT", bound="Cog")
@@ -142,68 +139,68 @@ def command(self) -> ApplicationCommand | None:
142139
def command(self, value: ApplicationCommand | None) -> None:
143140
self.interaction.command = value
144141

145-
@cached_property
142+
@property
146143
def channel(self) -> InteractionChannel | None:
147144
"""Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]:
148145
Returns the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel`.
149146
"""
150147
return self.interaction.channel
151148

152-
@cached_property
149+
@property
153150
def channel_id(self) -> int | None:
154151
"""Returns the ID of the channel associated with this context's command.
155152
Shorthand for :attr:`.Interaction.channel_id`.
156153
"""
157154
return self.interaction.channel_id
158155

159-
@cached_property
156+
@property
160157
def guild(self) -> Guild | None:
161158
"""Returns the guild associated with this context's command.
162159
Shorthand for :attr:`.Interaction.guild`.
163160
"""
164161
return self.interaction.guild
165162

166-
@cached_property
163+
@property
167164
def guild_id(self) -> int | None:
168165
"""Returns the ID of the guild associated with this context's command.
169166
Shorthand for :attr:`.Interaction.guild_id`.
170167
"""
171168
return self.interaction.guild_id
172169

173-
@cached_property
170+
@property
174171
def locale(self) -> str | None:
175172
"""Returns the locale of the guild associated with this context's command.
176173
Shorthand for :attr:`.Interaction.locale`.
177174
"""
178175
return self.interaction.locale
179176

180-
@cached_property
177+
@property
181178
def guild_locale(self) -> str | None:
182179
"""Returns the locale of the guild associated with this context's command.
183180
Shorthand for :attr:`.Interaction.guild_locale`.
184181
"""
185182
return self.interaction.guild_locale
186183

187-
@cached_property
184+
@property
188185
def app_permissions(self) -> Permissions:
189186
return self.interaction.app_permissions
190187

191-
@cached_property
188+
@property
192189
def me(self) -> Member | ClientUser | None:
193190
"""Union[:class:`.Member`, :class:`.ClientUser`]:
194191
Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message
195192
message contexts, or when :meth:`Intents.guilds` is absent.
196193
"""
197194
return self.interaction.guild.me if self.interaction.guild is not None else self.bot.user
198195

199-
@cached_property
196+
@property
200197
def message(self) -> Message | None:
201198
"""Returns the message sent with this context's command.
202199
Shorthand for :attr:`.Interaction.message`, if applicable.
203200
"""
204201
return self.interaction.message
205202

206-
@cached_property
203+
@property
207204
def user(self) -> Member | User:
208205
"""Returns the user that sent this context's command.
209206
Shorthand for :attr:`.Interaction.user`.
@@ -222,7 +219,7 @@ def voice_client(self) -> VoiceClient | None:
222219

223220
return self.interaction.guild.voice_client
224221

225-
@cached_property
222+
@property
226223
def response(self) -> InteractionResponse:
227224
"""Returns the response object associated with this context's command.
228225
Shorthand for :attr:`.Interaction.response`.
@@ -263,7 +260,7 @@ def unselected_options(self) -> list[Option] | None:
263260
return self.command.options # type: ignore
264261
return None
265262

266-
@cached_property
263+
@property
267264
def attachment_size_limit(self) -> int:
268265
"""Returns the attachment size limit associated with this context's interaction.
269266
Shorthand for :attr:`.Interaction.attachment_size_limit`.

discord/ext/commands/context.py

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

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

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

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

305-
@discord.utils.cached_property
305+
@property
306306
def me(self) -> Member | ClientUser:
307307
"""Union[:class:`.Member`, :class:`.ClientUser`]:
308308
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
@@ -24,12 +24,13 @@
2424

2525
from __future__ import annotations
2626

27+
from functools import cached_property
2728
from typing import TYPE_CHECKING, Any
2829

2930
from .enums import OnboardingMode, PromptType, try_enum
3031
from .partial_emoji import PartialEmoji
3132
from .utils import MISSING, cached_property, generate_snowflake, get
32-
from discord import utils
33+
from . import utils
3334

3435
if TYPE_CHECKING:
3536
from .abc import Snowflake

discord/poll.py

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

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

3031
from . import utils
@@ -341,7 +342,7 @@ def __init__(
341342
self._expiry = None
342343
self._message = None
343344

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

discord/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,6 @@ def __bool__(self) -> Literal[False]:
122122

123123
MISSING: Literal[Undefined.MISSING] = Undefined.MISSING
124124

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

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

154-
cached_property = property
155-
156138
P = ParamSpec("P")
157139

158140
else:
159-
cached_property = _cached_property
160141
AutocompleteContext = Any
161142
OptionChoice = Any
162143

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)