Skip to content

Commit a8d903c

Browse files
BobDotComLulalaby
andauthored
Implement PEP 655 NotRequired (#1705)
Co-authored-by: Lala Sabathil <[email protected]>
1 parent 9ba56ef commit a8d903c

20 files changed

+371
-412
lines changed

discord/types/activity.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import Literal, TypedDict
28+
import sys
29+
from typing import Literal
2930

3031
from .snowflake import Snowflake
3132
from .user import PartialUser
3233

34+
if sys.version_info >= (3, 11):
35+
from typing import NotRequired, TypedDict
36+
else:
37+
from typing_extensions import NotRequired, TypedDict
38+
39+
3340
StatusType = Literal["idle", "dnd", "online", "offline"]
3441

3542

@@ -70,12 +77,9 @@ class ActivitySecrets(TypedDict, total=False):
7077
match: str
7178

7279

73-
class _ActivityEmojiOptional(TypedDict, total=False):
74-
id: Snowflake
75-
animated: bool
76-
77-
78-
class ActivityEmoji(_ActivityEmojiOptional):
80+
class ActivityEmoji(TypedDict):
81+
id: NotRequired[Snowflake]
82+
animated: NotRequired[bool]
7983
name: str
8084

8185

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

8690

87-
class _SendableActivityOptional(TypedDict, total=False):
88-
url: str | None
89-
90-
9191
ActivityType = Literal[0, 1, 2, 4, 5]
9292

9393

94-
class SendableActivity(_SendableActivityOptional):
94+
class SendableActivity(TypedDict):
95+
url: NotRequired[str | None]
9596
name: str
9697
type: ActivityType
9798

discord/types/appinfo.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TypedDict
28+
import sys
2929

3030
from .snowflake import Snowflake
3131
from .team import Team
3232
from .user import User
3333

34+
if sys.version_info >= (3, 11):
35+
from typing import NotRequired, TypedDict
36+
else:
37+
from typing_extensions import NotRequired, TypedDict
38+
3439

3540
class BaseAppInfo(TypedDict):
3641
id: Snowflake
@@ -39,35 +44,24 @@ class BaseAppInfo(TypedDict):
3944
icon: str | None
4045
summary: str
4146
description: str
47+
terms_of_service_url: NotRequired[str]
48+
privacy_policy_url: NotRequired[str]
49+
hook: NotRequired[bool]
50+
max_participants: NotRequired[int]
4251

4352

44-
class _AppInfoOptional(TypedDict, total=False):
45-
team: Team
46-
guild_id: Snowflake
47-
primary_sku_id: Snowflake
48-
slug: str
49-
terms_of_service_url: str
50-
privacy_policy_url: str
51-
hook: bool
52-
max_participants: int
53-
54-
55-
class AppInfo(BaseAppInfo, _AppInfoOptional):
53+
class AppInfo(BaseAppInfo):
54+
team: NotRequired[Team]
55+
guild_id: NotRequired[Snowflake]
56+
primary_sku_id: NotRequired[Snowflake]
57+
slug: NotRequired[str]
5658
rpc_origins: list[str]
5759
owner: User
5860
bot_public: bool
5961
bot_require_code_grant: bool
6062

6163

62-
class _PartialAppInfoOptional(TypedDict, total=False):
63-
rpc_origins: list[str]
64-
cover_image: str
65-
hook: bool
66-
terms_of_service_url: str
67-
privacy_policy_url: str
68-
max_participants: int
69-
flags: int
70-
71-
72-
class PartialAppInfo(_PartialAppInfoOptional, BaseAppInfo):
73-
pass
64+
class PartialAppInfo(BaseAppInfo):
65+
rpc_origins: NotRequired[list[str]]
66+
cover_image: NotRequired[str]
67+
flags: NotRequired[int]

discord/types/audit_log.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import Literal, TypedDict, Union
28+
import sys
29+
from typing import Literal, Union
2930

3031
from .automod import AutoModRule
3132
from .channel import ChannelType, PermissionOverwrite, VideoQualityMode
@@ -43,6 +44,12 @@
4344
from .user import User
4445
from .webhook import Webhook
4546

47+
if sys.version_info >= (3, 11):
48+
from typing import NotRequired, TypedDict
49+
else:
50+
from typing_extensions import NotRequired, TypedDict
51+
52+
4653
AuditLogEvent = Literal[
4754
1,
4855
10,
@@ -268,13 +275,10 @@ class AuditEntryInfo(TypedDict):
268275
role_name: str
269276

270277

271-
class _AuditLogEntryOptional(TypedDict, total=False):
272-
changes: list[AuditLogChange]
273-
options: AuditEntryInfo
274-
reason: str
275-
276-
277-
class AuditLogEntry(_AuditLogEntryOptional):
278+
class AuditLogEntry(TypedDict):
279+
changes: NotRequired[list[AuditLogChange]]
280+
options: NotRequired[AuditEntryInfo]
281+
reason: NotRequired[str]
278282
target_id: str | None
279283
user_id: Snowflake | None
280284
id: Snowflake

discord/types/automod.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@
2222

2323
from __future__ import annotations
2424

25-
from typing import Literal, TypedDict
25+
import sys
26+
from typing import Literal
2627

2728
from .snowflake import Snowflake
2829

30+
if sys.version_info >= (3, 11):
31+
from typing import NotRequired, TypedDict
32+
else:
33+
from typing_extensions import NotRequired, TypedDict
34+
35+
2936
AutoModTriggerType = Literal[1, 2, 3, 4]
3037

3138
AutoModEventType = Literal[1]
@@ -64,13 +71,10 @@ class AutoModRule(TypedDict):
6471
exempt_channels: list[Snowflake]
6572

6673

67-
class _CreateAutoModRuleOptional(TypedDict, total=False):
68-
enabled: bool
69-
exempt_roles: list[Snowflake]
70-
exempt_channels: list[Snowflake]
71-
72-
73-
class CreateAutoModRule(_CreateAutoModRuleOptional):
74+
class CreateAutoModRule(TypedDict):
75+
enabled: NotRequired[bool]
76+
exempt_roles: NotRequired[list[Snowflake]]
77+
exempt_channels: NotRequired[list[Snowflake]]
7478
name: str
7579
event_type: AutoModEventType
7680
trigger_type: AutoModTriggerType

discord/types/channel.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25-
26-
from typing import List, Literal, Optional, TypedDict, Union
25+
import sys
26+
from typing import List, Literal, Optional, Union
2727

2828
from .snowflake import Snowflake
2929
from .threads import ThreadArchiveDuration, ThreadMember, ThreadMetadata
3030
from .user import PartialUser
3131

32+
if sys.version_info >= (3, 11):
33+
from typing import NotRequired, TypedDict
34+
else:
35+
from typing_extensions import NotRequired, TypedDict
36+
37+
3238
OverwriteType = Literal[0, 1]
3339

3440

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

8490

85-
class _VoiceChannelOptional(TypedDict, total=False):
86-
rtc_region: Optional[str]
87-
video_quality_mode: VideoQualityMode
88-
89-
90-
class VoiceChannel(_BaseGuildChannel, _VoiceChannelOptional):
91+
class VoiceChannel(_BaseGuildChannel):
92+
rtc_region: NotRequired[Optional[str]]
93+
video_quality_mode: NotRequired[VideoQualityMode]
9194
type: Literal[2]
9295
bitrate: int
9396
user_limit: int
@@ -97,33 +100,24 @@ class CategoryChannel(_BaseGuildChannel):
97100
type: Literal[4]
98101

99102

100-
class _StageChannelOptional(TypedDict, total=False):
101-
rtc_region: Optional[str]
102-
topic: str
103-
104-
105-
class StageChannel(_BaseGuildChannel, _StageChannelOptional):
103+
class StageChannel(_BaseGuildChannel):
104+
rtc_region: NotRequired[Optional[str]]
105+
topic: NotRequired[str]
106106
type: Literal[13]
107107
bitrate: int
108108
user_limit: int
109109

110110

111-
class _ThreadChannelOptional(TypedDict, total=False):
112-
member: ThreadMember
113-
owner_id: Snowflake
114-
rate_limit_per_user: int
115-
last_message_id: Optional[Snowflake]
116-
last_pin_timestamp: str
117-
118-
119-
class ThreadChannel(_BaseChannel, _ThreadChannelOptional):
111+
class ThreadChannel(_BaseChannel):
112+
member: NotRequired[ThreadMember]
113+
owner_id: NotRequired[Snowflake]
114+
rate_limit_per_user: NotRequired[int]
115+
last_message_id: NotRequired[Optional[Snowflake]]
116+
last_pin_timestamp: NotRequired[str]
120117
type: Literal[10, 11, 12]
121118
guild_id: Snowflake
122119
parent_id: Snowflake
123-
owner_id: Snowflake
124120
nsfw: bool
125-
last_message_id: Optional[Snowflake]
126-
rate_limit_per_user: int
127121
message_count: int
128122
member_count: int
129123
thread_metadata: ThreadMetadata

discord/types/components.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import Literal, TypedDict, Union
28+
import sys
29+
from typing import Literal, Union
2930

3031
from .emoji import PartialEmoji
3132

33+
if sys.version_info >= (3, 11):
34+
from typing import NotRequired, TypedDict
35+
else:
36+
from typing_extensions import NotRequired, TypedDict
37+
3238
ComponentType = Literal[1, 2, 3, 4]
3339
ButtonStyle = Literal[1, 2, 3, 4, 5]
3440
InputTextStyle = Literal[1, 2]
@@ -39,53 +45,41 @@ class ActionRow(TypedDict):
3945
components: list[Component]
4046

4147

42-
class _ButtonComponentOptional(TypedDict, total=False):
43-
custom_id: str
44-
url: str
45-
disabled: bool
46-
emoji: PartialEmoji
47-
label: str
48-
49-
50-
class ButtonComponent(_ButtonComponentOptional):
48+
class ButtonComponent(TypedDict):
49+
custom_id: NotRequired[str]
50+
url: NotRequired[str]
51+
disabled: NotRequired[bool]
52+
emoji: NotRequired[PartialEmoji]
53+
label: NotRequired[str]
5154
type: Literal[2]
5255
style: ButtonStyle
5356

5457

55-
class _InputTextComponentOptional(TypedDict, total=False):
56-
min_length: int
57-
max_length: int
58-
required: bool
59-
placeholder: str
60-
value: str
61-
62-
63-
class InputText(_InputTextComponentOptional):
58+
class InputText(TypedDict):
59+
min_length: NotRequired[int]
60+
max_length: NotRequired[int]
61+
required: NotRequired[bool]
62+
placeholder: NotRequired[str]
63+
value: NotRequired[str]
6464
type: Literal[4]
6565
style: InputTextStyle
6666
custom_id: str
6767
label: str
6868

6969

70-
class _SelectMenuOptional(TypedDict, total=False):
71-
placeholder: str
72-
min_values: int
73-
max_values: int
74-
disabled: bool
75-
76-
77-
class _SelectOptionsOptional(TypedDict, total=False):
78-
description: str
79-
emoji: PartialEmoji
80-
81-
82-
class SelectOption(_SelectOptionsOptional):
70+
class SelectOption(TypedDict):
71+
description: NotRequired[str]
72+
emoji: NotRequired[PartialEmoji]
8373
label: str
8474
value: str
8575
default: bool
8676

8777

88-
class SelectMenu(_SelectMenuOptional):
78+
class SelectMenu(TypedDict):
79+
placeholder: NotRequired[str]
80+
min_values: NotRequired[int]
81+
max_values: NotRequired[int]
82+
disabled: NotRequired[bool]
8983
type: Literal[3]
9084
custom_id: str
9185
options: list[SelectOption]

0 commit comments

Comments
 (0)