diff --git a/discord/audit_logs.py b/discord/audit_logs.py index e2ff277dfe..6f872b30f8 100644 --- a/discord/audit_logs.py +++ b/discord/audit_logs.py @@ -602,12 +602,12 @@ def _get_member(self, user_id: int) -> Member | User | None: def __repr__(self) -> str: return f"" - @utils.cached_property + @property def created_at(self) -> datetime.datetime: """Returns the entry's creation time in UTC.""" return utils.snowflake_time(self.id) - @utils.cached_property + @property def target( self, ) -> ( @@ -631,24 +631,24 @@ def target( else: return converter(self._target_id) - @utils.cached_property + @property def category(self) -> enums.AuditLogActionCategory: """The category of the action, if applicable.""" return self.action.category - @utils.cached_property + @property def changes(self) -> AuditLogChanges: """The list of changes this entry has.""" obj = AuditLogChanges(self, self._changes, state=self._state) del self._changes return obj - @utils.cached_property + @property def before(self) -> AuditLogDiff: """The target's prior state.""" return self.changes.before - @utils.cached_property + @property def after(self) -> AuditLogDiff: """The target's subsequent state.""" return self.changes.after diff --git a/discord/automod.py b/discord/automod.py index c4b1e23c0e..b7bbfb3360 100644 --- a/discord/automod.py +++ b/discord/automod.py @@ -416,12 +416,12 @@ def __repr__(self) -> str: def __str__(self) -> str: return self.name - @cached_property + @property def guild(self) -> Guild | None: """The guild this rule belongs to.""" return self._state._get_guild(self.guild_id) - @cached_property + @property def creator(self) -> Member | None: """The member who created this rule.""" if self.guild is None: diff --git a/discord/commands/context.py b/discord/commands/context.py index 532d8abe2a..b53d56dd56 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -32,28 +32,25 @@ from discord.webhook.async_ import Webhook if TYPE_CHECKING: + from typing import Awaitable, Callable + from typing_extensions import ParamSpec import discord - from .. import Bot - from ..state import ConnectionState - from ..voice_client import VoiceClient - from .core import ApplicationCommand, Option - from ..interactions import InteractionChannel + from .. import Bot + from ..client import ClientUser + from ..cog import Cog from ..guild import Guild + from ..interactions import InteractionChannel from ..member import Member from ..message import Message - from ..user import User from ..permissions import Permissions - from ..client import ClientUser - - from ..cog import Cog + from ..state import ConnectionState + from ..user import User + from ..voice_client import VoiceClient from ..webhook import WebhookMessage - - from typing import Callable, Awaitable - -from ..utils import cached_property + from .core import ApplicationCommand, Option T = TypeVar("T") CogT = TypeVar("CogT", bound="Cog") @@ -136,53 +133,53 @@ async def invoke( """ return await command(self, *args, **kwargs) - @cached_property + @property def channel(self) -> InteractionChannel | None: """Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]: Returns the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel`. """ return self.interaction.channel - @cached_property + @property def channel_id(self) -> int | None: """Returns the ID of the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel_id`. """ return self.interaction.channel_id - @cached_property + @property def guild(self) -> Guild | None: """Returns the guild associated with this context's command. Shorthand for :attr:`.Interaction.guild`. """ return self.interaction.guild - @cached_property + @property def guild_id(self) -> int | None: """Returns the ID of the guild associated with this context's command. Shorthand for :attr:`.Interaction.guild_id`. """ return self.interaction.guild_id - @cached_property + @property def locale(self) -> str | None: """Returns the locale of the guild associated with this context's command. Shorthand for :attr:`.Interaction.locale`. """ return self.interaction.locale - @cached_property + @property def guild_locale(self) -> str | None: """Returns the locale of the guild associated with this context's command. Shorthand for :attr:`.Interaction.guild_locale`. """ return self.interaction.guild_locale - @cached_property + @property def app_permissions(self) -> Permissions: return self.interaction.app_permissions - @cached_property + @property def me(self) -> Member | ClientUser | None: """Union[:class:`.Member`, :class:`.ClientUser`]: Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message @@ -194,14 +191,14 @@ def me(self) -> Member | ClientUser | None: else self.bot.user ) - @cached_property + @property def message(self) -> Message | None: """Returns the message sent with this context's command. Shorthand for :attr:`.Interaction.message`, if applicable. """ return self.interaction.message - @cached_property + @property def user(self) -> Member | User: """Returns the user that sent this context's command. Shorthand for :attr:`.Interaction.user`. @@ -220,7 +217,7 @@ def voice_client(self) -> VoiceClient | None: return self.interaction.guild.voice_client - @cached_property + @property def response(self) -> InteractionResponse: """Returns the response object associated with this context's command. Shorthand for :attr:`.Interaction.response`. diff --git a/discord/ext/commands/context.py b/discord/ext/commands/context.py index c140586faa..4ab081ba23 100644 --- a/discord/ext/commands/context.py +++ b/discord/ext/commands/context.py @@ -283,28 +283,28 @@ def cog(self) -> Cog | None: return None return self.command.cog - @discord.utils.cached_property + @property def guild(self) -> Guild | None: """Returns the guild associated with this context's command. None if not available. """ return self.message.guild - @discord.utils.cached_property + @property def channel(self) -> MessageableChannel: """Returns the channel associated with this context's command. Shorthand for :attr:`.Message.channel`. """ return self.message.channel - @discord.utils.cached_property + @property def author(self) -> User | Member: """Union[:class:`~discord.User`, :class:`.Member`]: Returns the author associated with this context's command. Shorthand for :attr:`.Message.author` """ return self.message.author - @discord.utils.cached_property + @property def me(self) -> Member | ClientUser: """Union[:class:`.Member`, :class:`.ClientUser`]: Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message diff --git a/discord/onboarding.py b/discord/onboarding.py index cf6a721a00..4e4fa7e993 100644 --- a/discord/onboarding.py +++ b/discord/onboarding.py @@ -24,11 +24,12 @@ from __future__ import annotations +from functools import cached_property from typing import TYPE_CHECKING, Any from .enums import OnboardingMode, PromptType, try_enum from .partial_emoji import PartialEmoji -from .utils import MISSING, cached_property, generate_snowflake, get +from .utils import MISSING, generate_snowflake, get if TYPE_CHECKING: from .abc import Snowflake diff --git a/discord/poll.py b/discord/poll.py index 087d2833f6..fcc94cc4b4 100644 --- a/discord/poll.py +++ b/discord/poll.py @@ -25,6 +25,7 @@ from __future__ import annotations import datetime +from functools import cached_property from typing import TYPE_CHECKING, Any from . import utils @@ -358,7 +359,7 @@ def __init__( self._expiry = None self._message = None - @utils.cached_property + @cached_property def expiry(self) -> datetime.datetime | None: """An aware datetime object that specifies the date and time in UTC when the poll will end.""" return utils.parse_time(self._expiry) diff --git a/discord/utils.py b/discord/utils.py index 363d339391..3e02efddcc 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -121,22 +121,6 @@ def __repr__(self) -> str: # error. MissingField = field(default_factory=lambda: MISSING) - -class _cached_property: - def __init__(self, function): - self.function = function - self.__doc__ = getattr(function, "__doc__") - - def __get__(self, instance, owner): - if instance is None: - return self - - value = self.function(instance) - setattr(instance, self.function.__name__, value) - - return value - - if TYPE_CHECKING: from typing_extensions import ParamSpec @@ -150,12 +134,9 @@ def __get__(self, instance, owner): class _RequestLike(Protocol): headers: Mapping[str, Any] - cached_property = property - P = ParamSpec("P") else: - cached_property = _cached_property AutocompleteContext = Any OptionChoice = Any diff --git a/tests/test_utils.py b/tests/test_utils.py index d0f94acb93..86ccb3dfb2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -30,7 +30,6 @@ from discord.utils import ( MISSING, - _cached_property, _parse_ratelimit_header, _unique, async_all, @@ -80,22 +79,6 @@ def test_temporary(): # assert repr(MISSING) == '...' # # -# def test_cached_property() -> None: -# class Test: -# def __init__(self, x: int): -# self.x = x -# -# @_cached_property -# def foo(self) -> int: -# self.x += 1 -# return self.x -# -# t = Test(0) -# assert isinstance(_cached_property.__get__(_cached_property(None), None, None), _cached_property) -# assert t.foo == 1 -# assert t.foo == 1 -# -# # def test_find_get() -> None: # class Obj: # def __init__(self, value: int):