From 158cbd4cad03a0e50452fbfa3627c5c2038fcab3 Mon Sep 17 00:00:00 2001 From: dolfies Date: Wed, 30 Aug 2023 18:10:01 +0300 Subject: [PATCH] Generalize dict/metadata documentation types --- discord/application.py | 8 ++++---- discord/channel.py | 2 +- discord/client.py | 4 ++-- discord/metadata.py | 20 ++++++++++---------- discord/store.py | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/discord/application.py b/discord/application.py index bbc4d5ed6..54a01cb11 100644 --- a/discord/application.py +++ b/discord/application.py @@ -324,11 +324,11 @@ class Achievement(Hashable): ----------- name: :class:`str` The achievement's name. - name_localizations: Dict[:class:`Locale`, :class:`str`] + name_localizations: Mapping[:class:`Locale`, :class:`str`] The achievement's name localized to other languages. description: :class:`str` The achievement's description. - description_localizations: Dict[:class:`Locale`, :class:`str`] + description_localizations: Mapping[:class:`Locale`, :class:`str`] The achievement's description localized to other languages. icon: :class:`bytes` A :term:`py:bytes-like object` representing the new icon. @@ -1193,7 +1193,7 @@ class Manifest(Hashable): Parameters ----------- - manifest: :class:`Metadata` + manifest: Mapping[:class:`str`, Any] A dict-like object representing the manifest to upload. Raises @@ -1617,7 +1617,7 @@ class ApplicationBranch(Hashable): Parameters ----------- - manifests: List[:class:`Metadata`] + manifests: List[Mapping[:class:`str`, Any]] A list of dict-like objects representing the manifests. source_build: Optional[:class:`ApplicationBuild`] The source build of the build, if any. diff --git a/discord/channel.py b/discord/channel.py index be8186d56..e249248e4 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -4186,7 +4186,7 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, discord.abc An argument list of users to add to this group. If the user is of type :class:`Object`, then the ``nick`` attribute is used as the nickname for the added recipient. - nicks: Optional[Dict[:class:`~discord.abc.Snowflake`, :class:`str`]] + nicks: Optional[Mapping[:class:`~discord.abc.Snowflake`, :class:`str`]] A mapping of user IDs to nicknames to use for the added recipients. .. versionadded:: 2.0 diff --git a/discord/client.py b/discord/client.py index 5f1585e94..fa4b152e6 100644 --- a/discord/client.py +++ b/discord/client.py @@ -3633,7 +3633,7 @@ class Client: Whether the previewed subscription should be a renewal. code: Optional[:class:`str`] Unknown. - metadata: Optional[:class:`.Metadata`] + metadata: Optional[Mapping[:class:`str`, Any]] Extra metadata about the subscription. guild: Optional[:class:`.Guild`] The guild the previewed subscription's entitlements should be applied to. @@ -3706,7 +3706,7 @@ class Client: The current checkout context. code: Optional[:class:`str`] Unknown. - metadata: Optional[:class:`.Metadata`] + metadata: Optional[Mapping[:class:`str`, Any]] Extra metadata about the subscription. guild: Optional[:class:`.Guild`] The guild the subscription's entitlements should be applied to. diff --git a/discord/metadata.py b/discord/metadata.py index 2a026d92d..b88500b78 100644 --- a/discord/metadata.py +++ b/discord/metadata.py @@ -24,12 +24,18 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations -from typing import TYPE_CHECKING, Any, Dict, Iterator, Optional, Tuple, Union +from collections.abc import Mapping +from typing import TYPE_CHECKING, Any, Iterator, Tuple from .utils import parse_time +if TYPE_CHECKING: + MetadataObject = Mapping[str, Any] + +__all__ = ('Metadata',) -class Metadata: + +class Metadata(Mapping[str, Any]): """Represents a raw model from Discord. Because of how unstable and wildly varying some metadata in Discord can be, this is a simple class @@ -65,10 +71,8 @@ class Metadata: to be used as an iterable in list/dict/etc constructions. """ - def __init__(self, data: Optional[MetadataObject] = None) -> None: - if not data: - return - + def __init__(self, *args, **kwargs) -> None: + data = dict(*args, **kwargs) for key, value in data.items(): key, value = self.__parse(key, value) self.__dict__[key] = value @@ -143,7 +147,3 @@ class Metadata: def items(self): """A set-like object providing a view on the metadata's items.""" return self.__dict__.items() - - -if TYPE_CHECKING: - MetadataObject = Union[Metadata, Dict[str, Any]] diff --git a/discord/store.py b/discord/store.py index 5dfc6fc25..9602fde8c 100644 --- a/discord/store.py +++ b/discord/store.py @@ -765,15 +765,15 @@ class StoreListing(Hashable): ---------- summary: Optional[:class:`str`] The summary of the store listing. - summary_localizations: Dict[:class:`Locale`, :class:`str`] + summary_localizations: Mapping[:class:`Locale`, :class:`str`] The summary of the store listing localized to different languages. description: Optional[:class:`str`] The description of the store listing. - description_localizations: Dict[:class:`Locale`, :class:`str`] + description_localizations: Mapping[:class:`Locale`, :class:`str`] The description of the store listing localized to different languages. tagline: Optional[:class:`str`] The tagline of the store listing. - tagline_localizations: Dict[:class:`Locale`, :class:`str`] + tagline_localizations: Mapping[:class:`Locale`, :class:`str`] The tagline of the store listing localized to different languages. child_skus: List[:class:`SKU`] The child SKUs of the store listing. @@ -1324,21 +1324,21 @@ class SKU(Hashable): ----------- name: :class:`str` The SKU's name. - name_localizations: Dict[:class:`Locale`, :class:`str`] + name_localizations: Mapping[:class:`Locale`, :class:`str`] The SKU's name localized to other languages. legal_notice: Optional[:class:`str`] The SKU's legal notice. - legal_notice_localizations: Dict[:class:`Locale`, :class:`str`] + legal_notice_localizations: Mapping[:class:`Locale`, :class:`str`] The SKU's legal notice localized to other languages. price_tier: Optional[:class:`int`] The price tier of the SKU. This is the base price in USD that other currencies will be calculated from. - price_overrides: Dict[:class:`str`, :class:`int`] + price_overrides: Mapping[:class:`str`, :class:`int`] A mapping of currency to price. These prices override the base price tier. sale_price_tier: Optional[:class:`int`] The sale price tier of the SKU. This is the base sale price in USD that other currencies will be calculated from. - sale_price_overrides: Dict[:class:`str`, :class:`int`] + sale_price_overrides: Mapping[:class:`str`, :class:`int`] A mapping of currency to sale price. These prices override the base sale price tier. dependent_sku: Optional[:class:`SKU`] The ID of the SKU that this SKU is dependent on. @@ -1536,15 +1536,15 @@ class SKU(Hashable): ---------- summary: :class:`str` The summary of the store listing. - summary_localizations: Optional[Dict[:class:`Locale`, :class:`str`]] + summary_localizations: Optional[Mapping[:class:`Locale`, :class:`str`]] The summary of the store listing localized to different languages. description: :class:`str` The description of the store listing. - description_localizations: Optional[Dict[:class:`Locale`, :class:`str`]] + description_localizations: Optional[Mapping[:class:`Locale`, :class:`str`]] The description of the store listing localized to different languages. tagline: Optional[:class:`str`] The tagline of the store listing. - tagline_localizations: Optional[Dict[:class:`Locale`, :class:`str`]] + tagline_localizations: Optional[Mapping[:class:`Locale`, :class:`str`]] The tagline of the store listing localized to different languages. child_skus: Optional[List[:class:`SKU`]] The child SKUs of the store listing.