From aa702f66164f14ae3ec5aebf03ec871b978252f9 Mon Sep 17 00:00:00 2001 From: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:40:12 -0500 Subject: [PATCH] Improve import logic & __future__ annotations --- discord/types/activity.py | 8 +------- discord/types/appinfo.py | 8 +------- discord/types/audit_log.py | 8 +------- discord/types/automod.py | 8 +------- discord/types/channel.py | 30 +++++++++++++----------------- discord/types/components.py | 7 +------ discord/types/embed.py | 12 +++++------- discord/types/emoji.py | 9 +++++---- discord/types/guild.py | 7 +------ discord/types/integration.py | 7 +------ discord/types/interactions.py | 8 +------- discord/types/invite.py | 7 +------ discord/types/message.py | 7 +------ discord/types/raw_models.py | 11 +++-------- discord/types/role.py | 8 +------- discord/types/sticker.py | 7 +------ discord/types/threads.py | 7 +------ discord/types/user.py | 7 ++++--- discord/types/voice.py | 17 +++++++---------- discord/types/webhook.py | 7 +------ discord/types/widget.py | 15 +++++---------- 21 files changed, 56 insertions(+), 149 deletions(-) diff --git a/discord/types/activity.py b/discord/types/activity.py index 4a4253e169..3c610e3138 100644 --- a/discord/types/activity.py +++ b/discord/types/activity.py @@ -25,18 +25,12 @@ from __future__ import annotations -import sys from typing import Literal +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake from .user import PartialUser -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - - StatusType = Literal["idle", "dnd", "online", "offline"] diff --git a/discord/types/appinfo.py b/discord/types/appinfo.py index 18db297137..989d3f7a58 100644 --- a/discord/types/appinfo.py +++ b/discord/types/appinfo.py @@ -25,17 +25,11 @@ from __future__ import annotations -import sys - +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake from .team import Team from .user import User -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class BaseAppInfo(TypedDict): id: Snowflake diff --git a/discord/types/audit_log.py b/discord/types/audit_log.py index 9f9331b5b7..7e00526e6b 100644 --- a/discord/types/audit_log.py +++ b/discord/types/audit_log.py @@ -25,9 +25,9 @@ from __future__ import annotations -import sys from typing import Literal, Union +from .._typed_dict import NotRequired, TypedDict from .automod import AutoModRule from .channel import ChannelType, PermissionOverwrite, VideoQualityMode from .guild import ( @@ -44,12 +44,6 @@ from .user import User from .webhook import Webhook -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - - AuditLogEvent = Literal[ 1, 10, diff --git a/discord/types/automod.py b/discord/types/automod.py index 3118ee3a22..1a1542c06e 100644 --- a/discord/types/automod.py +++ b/discord/types/automod.py @@ -22,17 +22,11 @@ from __future__ import annotations -import sys from typing import Literal +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - - AutoModTriggerType = Literal[1, 2, 3, 4] AutoModEventType = Literal[1] diff --git a/discord/types/channel.py b/discord/types/channel.py index 6c179ad3bd..ee3384e9f3 100644 --- a/discord/types/channel.py +++ b/discord/types/channel.py @@ -22,19 +22,15 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import sys -from typing import List, Literal, Optional, Union +from __future__ import annotations +from typing import Literal, Union + +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake from .threads import ThreadArchiveDuration, ThreadMember, ThreadMetadata from .user import PartialUser -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - - OverwriteType = Literal[0, 1] @@ -56,9 +52,9 @@ class _BaseChannel(TypedDict): class _BaseGuildChannel(_BaseChannel): guild_id: Snowflake position: int - permission_overwrites: List[PermissionOverwrite] + permission_overwrites: list[PermissionOverwrite] nsfw: bool - parent_id: Optional[Snowflake] + parent_id: Snowflake | None class PartialChannel(_BaseChannel): @@ -67,7 +63,7 @@ class PartialChannel(_BaseChannel): class _TextChannelOptional(TypedDict, total=False): topic: str - last_message_id: Optional[Snowflake] + last_message_id: Snowflake | None last_pin_timestamp: str rate_limit_per_user: int default_auto_archive_duration: ThreadArchiveDuration @@ -89,7 +85,7 @@ class NewsChannel(_BaseGuildChannel, _TextChannelOptional): class VoiceChannel(_BaseGuildChannel): - rtc_region: NotRequired[Optional[str]] + rtc_region: NotRequired[str | None] video_quality_mode: NotRequired[VideoQualityMode] type: Literal[2] bitrate: int @@ -101,7 +97,7 @@ class CategoryChannel(_BaseGuildChannel): class StageChannel(_BaseGuildChannel): - rtc_region: NotRequired[Optional[str]] + rtc_region: NotRequired[str | None] topic: NotRequired[str] type: Literal[13] bitrate: int @@ -112,7 +108,7 @@ class ThreadChannel(_BaseChannel): member: NotRequired[ThreadMember] owner_id: NotRequired[Snowflake] rate_limit_per_user: NotRequired[int] - last_message_id: NotRequired[Optional[Snowflake]] + last_message_id: NotRequired[Snowflake | None] last_pin_timestamp: NotRequired[str] type: Literal[10, 11, 12] guild_id: Snowflake @@ -137,13 +133,13 @@ class ThreadChannel(_BaseChannel): class DMChannel(TypedDict): id: Snowflake type: Literal[1] - last_message_id: Optional[Snowflake] - recipients: List[PartialUser] + last_message_id: Snowflake | None + recipients: list[PartialUser] class GroupDMChannel(_BaseChannel): type: Literal[3] - icon: Optional[str] + icon: str | None owner_id: Snowflake diff --git a/discord/types/components.py b/discord/types/components.py index fe342b329e..7ca131cd27 100644 --- a/discord/types/components.py +++ b/discord/types/components.py @@ -25,16 +25,11 @@ from __future__ import annotations -import sys from typing import Literal, Union +from .._typed_dict import NotRequired, TypedDict from .emoji import PartialEmoji -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - ComponentType = Literal[1, 2, 3, 4] ButtonStyle = Literal[1, 2, 3, 4, 5] InputTextStyle = Literal[1, 2] diff --git a/discord/types/embed.py b/discord/types/embed.py index 2c3d78fa7f..0d73afe40c 100644 --- a/discord/types/embed.py +++ b/discord/types/embed.py @@ -22,13 +22,11 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import sys -from typing import List, Literal +from __future__ import annotations -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict +from typing import Literal + +from .._typed_dict import NotRequired, TypedDict class EmbedFooter(TypedDict): @@ -94,4 +92,4 @@ class Embed(TypedDict, total=False): video: EmbedVideo provider: EmbedProvider author: EmbedAuthor - fields: List[EmbedField] + fields: list[EmbedField] diff --git a/discord/types/emoji.py b/discord/types/emoji.py index 83289754c5..68040ebce2 100644 --- a/discord/types/emoji.py +++ b/discord/types/emoji.py @@ -22,16 +22,17 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from __future__ import annotations -from typing import Optional, TypedDict +from typing import TypedDict from .snowflake import Snowflake, SnowflakeList from .user import User class PartialEmoji(TypedDict): - id: Optional[Snowflake] - name: Optional[str] + id: Snowflake | None + name: str | None class Emoji(PartialEmoji, total=False): @@ -45,4 +46,4 @@ class Emoji(PartialEmoji, total=False): class EditEmoji(TypedDict): name: str - roles: Optional[SnowflakeList] + roles: SnowflakeList | None diff --git a/discord/types/guild.py b/discord/types/guild.py index 9f6d2005dd..d404dcf086 100644 --- a/discord/types/guild.py +++ b/discord/types/guild.py @@ -24,9 +24,9 @@ """ from __future__ import annotations -import sys from typing import Literal +from .._typed_dict import NotRequired, Required, TypedDict from .activity import PartialPresenceUpdate from .channel import GuildChannel from .emoji import Emoji @@ -39,11 +39,6 @@ from .voice import GuildVoiceState from .welcome_screen import WelcomeScreen -if sys.version_info >= (3, 11): - from typing import NotRequired, Required, TypedDict -else: - from typing_extensions import NotRequired, Required, TypedDict - class Ban(TypedDict): reason: str | None diff --git a/discord/types/integration.py b/discord/types/integration.py index eb6ae83d5a..219992d225 100644 --- a/discord/types/integration.py +++ b/discord/types/integration.py @@ -25,17 +25,12 @@ from __future__ import annotations -import sys from typing import Literal, Union +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake from .user import User -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class IntegrationApplication(TypedDict): bot: NotRequired[User] diff --git a/discord/types/interactions.py b/discord/types/interactions.py index 422f28aa3b..458247709d 100644 --- a/discord/types/interactions.py +++ b/discord/types/interactions.py @@ -25,7 +25,6 @@ from __future__ import annotations -import sys from typing import TYPE_CHECKING, Literal, Union from ..permissions import Permissions @@ -41,12 +40,7 @@ if TYPE_CHECKING: from .message import AllowedMentions, Message - -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - +from .._typed_dict import NotRequired, TypedDict ApplicationCommandType = Literal[1, 2, 3] diff --git a/discord/types/invite.py b/discord/types/invite.py index 43bcc13426..7f1e23b96e 100644 --- a/discord/types/invite.py +++ b/discord/types/invite.py @@ -25,9 +25,9 @@ from __future__ import annotations -import sys from typing import Literal, Union +from .._typed_dict import NotRequired, TypedDict from .appinfo import PartialAppInfo from .channel import PartialChannel from .guild import InviteGuild, _GuildPreviewUnique @@ -35,11 +35,6 @@ from .snowflake import Snowflake from .user import PartialUser -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - InviteTargetType = Literal[1, 2] diff --git a/discord/types/message.py b/discord/types/message.py index 0aa0957392..4d6d295d09 100644 --- a/discord/types/message.py +++ b/discord/types/message.py @@ -25,7 +25,6 @@ from __future__ import annotations -import sys from typing import TYPE_CHECKING, Literal from .channel import ChannelType @@ -41,11 +40,7 @@ if TYPE_CHECKING: from .interactions import MessageInteraction - -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict +from .._typed_dict import NotRequired, TypedDict class ChannelMention(TypedDict): diff --git a/discord/types/raw_models.py b/discord/types/raw_models.py index e8ad39819a..d8e64531f5 100644 --- a/discord/types/raw_models.py +++ b/discord/types/raw_models.py @@ -22,19 +22,14 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import sys -from typing import List +from __future__ import annotations +from .._typed_dict import NotRequired, TypedDict from .automod import AutoModAction, AutoModTriggerType from .emoji import PartialEmoji from .member import Member from .snowflake import Snowflake -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class _MessageEventOptional(TypedDict, total=False): guild_id: Snowflake @@ -46,7 +41,7 @@ class MessageDeleteEvent(_MessageEventOptional): class BulkMessageDeleteEvent(_MessageEventOptional): - ids: List[Snowflake] + ids: list[Snowflake] channel_id: Snowflake diff --git a/discord/types/role.py b/discord/types/role.py index 22dced79ab..8996e7e21d 100644 --- a/discord/types/role.py +++ b/discord/types/role.py @@ -25,15 +25,9 @@ from __future__ import annotations -import sys - +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class Role(TypedDict): tags: NotRequired[RoleTags] diff --git a/discord/types/sticker.py b/discord/types/sticker.py index 1538bfc182..129b14cc46 100644 --- a/discord/types/sticker.py +++ b/discord/types/sticker.py @@ -25,17 +25,12 @@ from __future__ import annotations -import sys from typing import Literal, Union +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake from .user import User -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - StickerFormatType = Literal[1, 2, 3] diff --git a/discord/types/threads.py b/discord/types/threads.py index 08ada318ea..87abe9af4b 100644 --- a/discord/types/threads.py +++ b/discord/types/threads.py @@ -25,16 +25,11 @@ from __future__ import annotations -import sys from typing import Literal +from .._typed_dict import NotRequired, TypedDict from .snowflake import Snowflake -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - ThreadType = Literal[10, 11, 12] ThreadArchiveDuration = Literal[60, 1440, 4320, 10080] diff --git a/discord/types/user.py b/discord/types/user.py index a344cac984..233eb4e4aa 100644 --- a/discord/types/user.py +++ b/discord/types/user.py @@ -22,8 +22,9 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from __future__ import annotations -from typing import Literal, Optional, TypedDict +from typing import Literal, TypedDict from .snowflake import Snowflake @@ -32,7 +33,7 @@ class PartialUser(TypedDict): id: Snowflake username: str discriminator: str - avatar: Optional[str] + avatar: str | None PremiumType = Literal[0, 1, 2] @@ -44,7 +45,7 @@ class User(PartialUser, total=False): mfa_enabled: bool local: str verified: bool - email: Optional[str] + email: str | None flags: int premium_type: PremiumType public_flags: int diff --git a/discord/types/voice.py b/discord/types/voice.py index 160c50feda..353bf1023d 100644 --- a/discord/types/voice.py +++ b/discord/types/voice.py @@ -22,17 +22,14 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import sys -from typing import List, Literal, Optional +from __future__ import annotations +from typing import Literal + +from .._typed_dict import NotRequired, TypedDict from .member import MemberWithUser from .snowflake import Snowflake -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - SupportedModes = Literal[ "xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305" ] @@ -56,7 +53,7 @@ class GuildVoiceState(_VoiceState): class VoiceState(_VoiceState, total=False): - channel_id: Optional[Snowflake] + channel_id: Snowflake | None guild_id: Snowflake @@ -72,7 +69,7 @@ class VoiceRegion(TypedDict): class VoiceServerUpdate(TypedDict): token: str guild_id: Snowflake - endpoint: Optional[str] + endpoint: str | None class VoiceIdentify(TypedDict): @@ -86,5 +83,5 @@ class VoiceReady(TypedDict): ssrc: int ip: str port: int - modes: List[SupportedModes] + modes: list[SupportedModes] heartbeat_interval: int diff --git a/discord/types/webhook.py b/discord/types/webhook.py index 1666a3576d..b5db096e11 100644 --- a/discord/types/webhook.py +++ b/discord/types/webhook.py @@ -25,18 +25,13 @@ from __future__ import annotations -import sys from typing import Literal +from .._typed_dict import NotRequired, TypedDict from .channel import PartialChannel from .snowflake import Snowflake from .user import User -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class SourceGuild(TypedDict): id: int diff --git a/discord/types/widget.py b/discord/types/widget.py index 7808807eaa..daeef2db95 100644 --- a/discord/types/widget.py +++ b/discord/types/widget.py @@ -22,18 +22,13 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import sys -from typing import List, Optional +from __future__ import annotations +from .._typed_dict import NotRequired, TypedDict from .activity import Activity from .snowflake import Snowflake from .user import User -if sys.version_info >= (3, 11): - from typing import NotRequired, TypedDict -else: - from typing_extensions import NotRequired, TypedDict - class WidgetChannel(TypedDict): id: Snowflake @@ -54,8 +49,8 @@ class WidgetMember(User, total=False): class Widget(TypedDict): - channels: NotRequired[List[WidgetChannel]] - members: NotRequired[List[WidgetMember]] + channels: NotRequired[list[WidgetChannel]] + members: NotRequired[list[WidgetMember]] presence_count: NotRequired[int] id: Snowflake name: str @@ -64,4 +59,4 @@ class Widget(TypedDict): class WidgetSettings(TypedDict): enabled: bool - channel_id: Optional[Snowflake] + channel_id: Snowflake | None