Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PEP 655 NotRequired #1705

Merged
merged 10 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions discord/types/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@

from __future__ import annotations

from typing import Literal, TypedDict
import sys
from typing import Literal

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"]


Expand Down Expand Up @@ -70,12 +77,9 @@ class ActivitySecrets(TypedDict, total=False):
match: str


class _ActivityEmojiOptional(TypedDict, total=False):
id: Snowflake
animated: bool


class ActivityEmoji(_ActivityEmojiOptional):
class ActivityEmoji(TypedDict):
id: NotRequired[Snowflake]
animated: NotRequired[bool]
name: str


Expand All @@ -84,14 +88,11 @@ class ActivityButton(TypedDict):
url: str


class _SendableActivityOptional(TypedDict, total=False):
url: str | None


ActivityType = Literal[0, 1, 2, 4, 5]


class SendableActivity(_SendableActivityOptional):
class SendableActivity(TypedDict):
url: NotRequired[str | None]
name: str
type: ActivityType

Expand Down
44 changes: 19 additions & 25 deletions discord/types/appinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

from __future__ import annotations

from typing import TypedDict
import sys

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
Expand All @@ -39,35 +44,24 @@ class BaseAppInfo(TypedDict):
icon: str | None
summary: str
description: str
terms_of_service_url: NotRequired[str]
privacy_policy_url: NotRequired[str]
hook: NotRequired[bool]
max_participants: NotRequired[int]


class _AppInfoOptional(TypedDict, total=False):
team: Team
guild_id: Snowflake
primary_sku_id: Snowflake
slug: str
terms_of_service_url: str
privacy_policy_url: str
hook: bool
max_participants: int


class AppInfo(BaseAppInfo, _AppInfoOptional):
class AppInfo(BaseAppInfo):
team: NotRequired[Team]
guild_id: NotRequired[Snowflake]
primary_sku_id: NotRequired[Snowflake]
slug: NotRequired[str]
rpc_origins: list[str]
owner: User
bot_public: bool
bot_require_code_grant: bool


class _PartialAppInfoOptional(TypedDict, total=False):
rpc_origins: list[str]
cover_image: str
hook: bool
terms_of_service_url: str
privacy_policy_url: str
max_participants: int
flags: int


class PartialAppInfo(_PartialAppInfoOptional, BaseAppInfo):
pass
class PartialAppInfo(BaseAppInfo):
rpc_origins: NotRequired[list[str]]
cover_image: NotRequired[str]
flags: NotRequired[int]
20 changes: 12 additions & 8 deletions discord/types/audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

from __future__ import annotations

from typing import Literal, TypedDict, Union
import sys
from typing import Literal, Union

from .automod import AutoModRule
from .channel import ChannelType, PermissionOverwrite, VideoQualityMode
Expand All @@ -43,6 +44,12 @@
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,
Expand Down Expand Up @@ -268,13 +275,10 @@ class AuditEntryInfo(TypedDict):
role_name: str


class _AuditLogEntryOptional(TypedDict, total=False):
changes: list[AuditLogChange]
options: AuditEntryInfo
reason: str


class AuditLogEntry(_AuditLogEntryOptional):
class AuditLogEntry(TypedDict):
changes: NotRequired[list[AuditLogChange]]
options: NotRequired[AuditEntryInfo]
reason: NotRequired[str]
target_id: str | None
user_id: Snowflake | None
id: Snowflake
Expand Down
20 changes: 12 additions & 8 deletions discord/types/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@

from __future__ import annotations

from typing import Literal, TypedDict
import sys
from typing import Literal

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]
Expand Down Expand Up @@ -64,13 +71,10 @@ class AutoModRule(TypedDict):
exempt_channels: list[Snowflake]


class _CreateAutoModRuleOptional(TypedDict, total=False):
enabled: bool
exempt_roles: list[Snowflake]
exempt_channels: list[Snowflake]


class CreateAutoModRule(_CreateAutoModRuleOptional):
class CreateAutoModRule(TypedDict):
enabled: NotRequired[bool]
exempt_roles: NotRequired[list[Snowflake]]
exempt_channels: NotRequired[list[Snowflake]]
name: str
event_type: AutoModEventType
trigger_type: AutoModTriggerType
Expand Down
46 changes: 20 additions & 26 deletions discord/types/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

from typing import List, Literal, Optional, TypedDict, Union
import sys
from typing import List, Literal, Optional, Union

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]


Expand Down Expand Up @@ -82,12 +88,9 @@ class NewsChannel(_BaseGuildChannel, _TextChannelOptional):
VideoQualityMode = Literal[1, 2]


class _VoiceChannelOptional(TypedDict, total=False):
rtc_region: Optional[str]
video_quality_mode: VideoQualityMode


class VoiceChannel(_BaseGuildChannel, _VoiceChannelOptional):
class VoiceChannel(_BaseGuildChannel):
rtc_region: NotRequired[Optional[str]]
video_quality_mode: NotRequired[VideoQualityMode]
type: Literal[2]
bitrate: int
user_limit: int
Expand All @@ -97,33 +100,24 @@ class CategoryChannel(_BaseGuildChannel):
type: Literal[4]


class _StageChannelOptional(TypedDict, total=False):
rtc_region: Optional[str]
topic: str


class StageChannel(_BaseGuildChannel, _StageChannelOptional):
class StageChannel(_BaseGuildChannel):
rtc_region: NotRequired[Optional[str]]
topic: NotRequired[str]
type: Literal[13]
bitrate: int
user_limit: int


class _ThreadChannelOptional(TypedDict, total=False):
member: ThreadMember
owner_id: Snowflake
rate_limit_per_user: int
last_message_id: Optional[Snowflake]
last_pin_timestamp: str


class ThreadChannel(_BaseChannel, _ThreadChannelOptional):
class ThreadChannel(_BaseChannel):
member: NotRequired[ThreadMember]
owner_id: NotRequired[Snowflake]
rate_limit_per_user: NotRequired[int]
last_message_id: NotRequired[Optional[Snowflake]]
last_pin_timestamp: NotRequired[str]
type: Literal[10, 11, 12]
guild_id: Snowflake
parent_id: Snowflake
owner_id: Snowflake
nsfw: bool
last_message_id: Optional[Snowflake]
rate_limit_per_user: int
message_count: int
member_count: int
thread_metadata: ThreadMetadata
Expand Down
60 changes: 27 additions & 33 deletions discord/types/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@

from __future__ import annotations

from typing import Literal, TypedDict, Union
import sys
from typing import Literal, Union

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]
Expand All @@ -39,53 +45,41 @@ class ActionRow(TypedDict):
components: list[Component]


class _ButtonComponentOptional(TypedDict, total=False):
custom_id: str
url: str
disabled: bool
emoji: PartialEmoji
label: str


class ButtonComponent(_ButtonComponentOptional):
class ButtonComponent(TypedDict):
custom_id: NotRequired[str]
url: NotRequired[str]
disabled: NotRequired[bool]
emoji: NotRequired[PartialEmoji]
label: NotRequired[str]
type: Literal[2]
style: ButtonStyle


class _InputTextComponentOptional(TypedDict, total=False):
min_length: int
max_length: int
required: bool
placeholder: str
value: str


class InputText(_InputTextComponentOptional):
class InputText(TypedDict):
min_length: NotRequired[int]
max_length: NotRequired[int]
required: NotRequired[bool]
placeholder: NotRequired[str]
value: NotRequired[str]
type: Literal[4]
style: InputTextStyle
custom_id: str
label: str


class _SelectMenuOptional(TypedDict, total=False):
placeholder: str
min_values: int
max_values: int
disabled: bool


class _SelectOptionsOptional(TypedDict, total=False):
description: str
emoji: PartialEmoji


class SelectOption(_SelectOptionsOptional):
class SelectOption(TypedDict):
description: NotRequired[str]
emoji: NotRequired[PartialEmoji]
label: str
value: str
default: bool


class SelectMenu(_SelectMenuOptional):
class SelectMenu(TypedDict):
placeholder: NotRequired[str]
min_values: NotRequired[int]
max_values: NotRequired[int]
disabled: NotRequired[bool]
type: Literal[3]
custom_id: str
options: list[SelectOption]
Expand Down