Skip to content

Commit d9b77a7

Browse files
refactor(typing): replace # type: ignores with # pyright: ignore[ruleName]; enable PGH003 (#1381)
Co-authored-by: onerandomusername <me@arielle.codes>
1 parent 6c482d1 commit d9b77a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+651
-544
lines changed

disnake/abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def category(self) -> Optional[CategoryChannel]:
602602
"""
603603
if isinstance(self.guild, Object):
604604
return None
605-
return self.guild.get_channel(self.category_id) # type: ignore
605+
return self.guild.get_channel(self.category_id) # pyright: ignore[reportArgumentType, reportReturnType]
606606

607607
@property
608608
def permissions_synced(self) -> bool:
@@ -1087,7 +1087,7 @@ async def _clone_impl(
10871087
obj = cls(state=self._state, guild=self.guild, data=data)
10881088

10891089
# temporarily add it to the cache
1090-
self.guild._channels[obj.id] = obj # type: ignore
1090+
self.guild._channels[obj.id] = obj # pyright: ignore[reportArgumentType]
10911091
return obj
10921092

10931093
async def clone(self, *, name: Optional[str] = None, reason: Optional[str] = None) -> Self:

disnake/activity.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ def __repr__(self) -> str:
399399
inner = " ".join(f"{k!s}={v!r}" for k, v in attrs)
400400
return f"<Activity {inner}>"
401401

402-
def to_dict(self) -> dict[str, Any]:
403-
ret: dict[str, Any] = {}
402+
def to_dict(self) -> ActivityPayload:
403+
ret: ActivityPayload = {} # pyright: ignore[reportAssignmentType]
404404
for attr in self.__slots__:
405405
value = getattr(self, attr, None)
406406
if value is None:
@@ -409,16 +409,16 @@ def to_dict(self) -> dict[str, Any]:
409409
if isinstance(value, dict) and len(value) == 0:
410410
continue
411411

412-
ret[attr] = value
412+
ret[attr] = value # pyright: ignore[reportGeneralTypeIssues]
413413

414414
# fix type field
415-
ret["type"] = int(self.type)
415+
ret["type"] = int(self.type) # pyright: ignore[reportGeneralTypeIssues] # ActivityPayload.type does not include -1
416416

417417
if self.status_display_type:
418-
ret["status_display_type"] = int(self.status_display_type)
418+
ret["status_display_type"] = int(self.status_display_type) # pyright: ignore[reportGeneralTypeIssues]
419419

420420
if self.emoji:
421-
ret["emoji"] = self.emoji.to_dict()
421+
ret["emoji"] = self.emoji.to_dict() # pyright: ignore[reportGeneralTypeIssues]
422422
# defined in base class slots
423423
if self._timestamps:
424424
ret["timestamps"] = self._timestamps
@@ -609,8 +609,8 @@ def twitch_name(self) -> Optional[str]:
609609
name = self.assets["large_image"]
610610
return name[7:] if name[:7] == "twitch:" else None
611611

612-
def to_dict(self) -> dict[str, Any]:
613-
ret: dict[str, Any] = {
612+
def to_dict(self) -> ActivityPayload:
613+
ret: ActivityPayload = {
614614
"type": ActivityType.streaming.value,
615615
"name": str(self.name),
616616
"url": str(self.url),
@@ -891,7 +891,7 @@ def to_dict(self) -> ActivityPayload:
891891
}
892892

893893
if self.emoji:
894-
o["emoji"] = self.emoji.to_dict() # type: ignore
894+
o["emoji"] = self.emoji.to_dict() # pyright: ignore[reportGeneralTypeIssues]
895895
return o
896896

897897
def __eq__(self, other: Any) -> bool:
@@ -945,16 +945,16 @@ def create_activity(
945945
if game_type is ActivityType.playing and not (
946946
"application_id" in data or "session_id" in data or "state" in data
947947
):
948-
activity = Game(**data) # type: ignore # pyright bug(?)
948+
activity = Game(**data) # pyright: ignore[reportArgumentType] # pyright bug(?)
949949
elif game_type is ActivityType.custom and "name" in data:
950-
activity = CustomActivity(**data) # type: ignore
950+
activity = CustomActivity(**data) # pyright: ignore[reportArgumentType]
951951
elif game_type is ActivityType.streaming and "url" in data:
952952
# url won't be None here
953-
activity = Streaming(**data) # type: ignore
953+
activity = Streaming(**data) # pyright: ignore[reportArgumentType]
954954
elif game_type is ActivityType.listening and "sync_id" in data and "session_id" in data:
955955
activity = Spotify(**data)
956956
else:
957-
activity = Activity(**data) # type: ignore
957+
activity = Activity(**data) # pyright: ignore[reportArgumentType]
958958

959959
if isinstance(activity, (Activity, CustomActivity)) and activity.emoji and state:
960960
activity.emoji._state = state

disnake/app_commands.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
from abc import ABC
88
from collections.abc import Mapping, Sequence
9-
from typing import TYPE_CHECKING, ClassVar, Optional, Union, cast
9+
from typing import TYPE_CHECKING, ClassVar, Optional, Union
1010

1111
from .enums import (
1212
ApplicationCommandPermissionType,
@@ -710,16 +710,14 @@ def to_dict(self) -> EditApplicationCommandPayload:
710710
}
711711

712712
install_types: Optional[list[ApplicationIntegrationTypePayload]] = (
713-
cast("list[ApplicationIntegrationTypePayload]", self._install_types_with_default.values)
713+
self._install_types_with_default.values
714714
if self._install_types_with_default is not None
715715
else None
716716
)
717717
data["integration_types"] = install_types
718718

719719
contexts: Optional[list[InteractionContextTypePayload]] = (
720-
cast("list[InteractionContextTypePayload]", self._contexts_with_default.values)
721-
if self._contexts_with_default is not None
722-
else None
720+
self._contexts_with_default.values if self._contexts_with_default is not None else None
723721
)
724722
data["contexts"] = contexts
725723

@@ -1265,13 +1263,13 @@ def __init__(self, *, data: ApplicationCommandPermissionsPayload, guild_id: int)
12651263
def __repr__(self) -> str:
12661264
return f"<ApplicationCommandPermissions id={self.id!r} type={self.type!r} permission={self.permission!r}>"
12671265

1268-
def __eq__(self, other) -> bool:
1266+
def __eq__(self, other: ApplicationCommandPermissions) -> bool:
12691267
return (
12701268
self.id == other.id and self.type == other.type and self.permission == other.permission
12711269
)
12721270

12731271
def to_dict(self) -> ApplicationCommandPermissionsPayload:
1274-
return {"id": self.id, "type": int(self.type), "permission": self.permission} # type: ignore
1272+
return {"id": self.id, "type": self.type.value, "permission": self.permission}
12751273

12761274
def is_everyone(self) -> bool:
12771275
"""Whether this permission object is affecting the @everyone role.

disnake/audit_logs.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,16 @@ def _transform_icon(entry: AuditLogEntry, data: Optional[str]) -> Optional[Asset
140140
if data is None:
141141
return None
142142
if entry.action.name.startswith("role_"):
143-
return Asset._from_role_icon(entry._state, entry._target_id, data) # type: ignore
143+
assert entry._target_id is not None
144+
return Asset._from_role_icon(entry._state, entry._target_id, data)
144145
return Asset._from_guild_icon(entry._state, entry.guild.id, data)
145146

146147

147148
def _transform_avatar(entry: AuditLogEntry, data: Optional[str]) -> Optional[Asset]:
148149
if data is None:
149150
return None
150-
return Asset._from_avatar(entry._state, entry._target_id, data) # type: ignore
151+
assert entry._target_id is not None
152+
return Asset._from_avatar(entry._state, entry._target_id, data)
151153

152154

153155
def _guild_hash_transformer(path: str) -> Callable[[AuditLogEntry, Optional[str]], Optional[Asset]]:
@@ -255,7 +257,8 @@ def _transform_guild_scheduled_event_image(
255257
) -> Optional[Asset]:
256258
if data is None:
257259
return None
258-
return Asset._from_guild_scheduled_event_image(entry._state, entry._target_id, data) # type: ignore
260+
assert entry._target_id is not None
261+
return Asset._from_guild_scheduled_event_image(entry._state, entry._target_id, data)
259262

260263

261264
def _transform_automod_action(
@@ -373,10 +376,12 @@ def __init__(self, entry: AuditLogEntry, data: list[AuditLogChangePayload]) -> N
373376

374377
# special cases for role add/remove
375378
if attr == "$add":
376-
self._handle_role(self.before, self.after, entry, elem["new_value"]) # type: ignore
379+
assert "new_value" in elem
380+
self._handle_role(self.before, self.after, entry, elem["new_value"]) # pyright: ignore[reportArgumentType]
377381
continue
378382
if attr == "$remove":
379-
self._handle_role(self.after, self.before, entry, elem["new_value"]) # type: ignore
383+
assert "new_value" in elem
384+
self._handle_role(self.after, self.before, entry, elem["new_value"]) # pyright: ignore[reportArgumentType]
380385
continue
381386

382387
# special case for application command permissions update
@@ -449,7 +454,7 @@ def _handle_role(
449454

450455
if role is None:
451456
role = Object(id=role_id)
452-
role.name = e["name"] # type: ignore
457+
role.name = e["name"] # pyright: ignore[reportAttributeAccessIssue]
453458

454459
data.append(role)
455460

@@ -642,7 +647,7 @@ def _from_data(self, data: AuditLogEntryPayload) -> None:
642647
role = self.guild.get_role(instance_id)
643648
if role is None:
644649
role = Object(id=instance_id)
645-
role.name = extra.get("role_name") # type: ignore
650+
role.name = extra.get("role_name") # pyright: ignore[reportAttributeAccessIssue]
646651
self.extra = role
647652
elif self.action.name.startswith("stage_instance"):
648653
elems = {

disnake/automod.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def with_changes(
350350
:class:`AutoModTriggerMetadata`
351351
The new metadata instance.
352352
"""
353-
return self.__class__( # type: ignore # call doesn't match any overloads
353+
return self.__class__(
354354
keyword_filter=self.keyword_filter if keyword_filter is MISSING else keyword_filter,
355355
regex_patterns=self.regex_patterns if regex_patterns is MISSING else regex_patterns,
356356
presets=self.presets if presets is MISSING else presets,
@@ -363,7 +363,7 @@ def with_changes(
363363
if mention_raid_protection_enabled is MISSING
364364
else mention_raid_protection_enabled
365365
),
366-
)
366+
) # pyright: ignore[reportCallIssue] # call doesn't match any overloads
367367

368368
@classmethod
369369
def _from_dict(cls, data: AutoModTriggerMetadataPayload) -> Self:
@@ -372,14 +372,14 @@ def _from_dict(cls, data: AutoModTriggerMetadataPayload) -> Self:
372372
else:
373373
presets = None
374374

375-
return cls( # type: ignore # call doesn't match any overloads
375+
return cls(
376376
keyword_filter=data.get("keyword_filter"),
377377
regex_patterns=data.get("regex_patterns"),
378378
presets=presets,
379379
allow_list=data.get("allow_list"),
380380
mention_total_limit=data.get("mention_total_limit"),
381381
mention_raid_protection_enabled=data.get("mention_raid_protection_enabled"),
382-
)
382+
) # pyright: ignore[reportCallIssue] # call doesn't match any overloads
383383

384384
def to_dict(self) -> AutoModTriggerMetadataPayload:
385385
data: AutoModTriggerMetadataPayload = {}
@@ -388,7 +388,7 @@ def to_dict(self) -> AutoModTriggerMetadataPayload:
388388
if self.regex_patterns is not None:
389389
data["regex_patterns"] = list(self.regex_patterns)
390390
if self.presets is not None:
391-
data["presets"] = self.presets.values # type: ignore # `values` contains ints instead of preset literal values
391+
data["presets"] = self.presets.values
392392
if self.allow_list is not None:
393393
data["allow_list"] = list(self.allow_list)
394394
if self.mention_total_limit is not None:

disnake/channel.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(self, *, data: VoiceChannelEffectPayload, state: ConnectionState) -
147147
else:
148148
sound_data: PartialSoundboardSoundPayload = {
149149
"sound_id": sound_id,
150-
"volume": data.get("sound_volume"), # type: ignore # assume this exists if sound_id is set
150+
"volume": data.get("sound_volume"), # pyright: ignore[reportAssignmentType] # assume this exists if sound_id is set
151151
}
152152
self.sound = PartialSoundboardSound(data=sound_data, state=state)
153153

@@ -541,7 +541,7 @@ async def edit(
541541
)
542542
if payload is not None:
543543
# the payload will always be the proper channel payload
544-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
544+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
545545
return None
546546

547547
async def clone(
@@ -1089,26 +1089,22 @@ async def create_thread(
10891089
:class:`Thread`
10901090
The newly created thread
10911091
"""
1092-
if not ((message is None) ^ (type is None)):
1093-
msg = "Exactly one of message and type must be provided."
1094-
raise ValueError(msg)
1095-
10961092
if auto_archive_duration is not None:
10971093
auto_archive_duration = cast(
10981094
"ThreadArchiveDurationLiteral", try_enum_to_int(auto_archive_duration)
10991095
)
11001096

1101-
if message is None:
1097+
if message is None and type is not None:
11021098
data = await self._state.http.start_thread_without_message(
11031099
self.id,
11041100
name=name,
11051101
auto_archive_duration=auto_archive_duration or self.default_auto_archive_duration,
1106-
type=type.value, # type: ignore
1102+
type=type.value,
11071103
invitable=invitable if invitable is not None else True,
11081104
rate_limit_per_user=slowmode_delay,
11091105
reason=reason,
11101106
)
1111-
else:
1107+
elif message is not None and type is None:
11121108
data = await self._state.http.start_thread_with_message(
11131109
self.id,
11141110
message.id,
@@ -1117,6 +1113,9 @@ async def create_thread(
11171113
rate_limit_per_user=slowmode_delay,
11181114
reason=reason,
11191115
)
1116+
else:
1117+
msg = "Exactly one of message and type must be provided."
1118+
raise ValueError(msg)
11201119

11211120
return Thread(guild=self.guild, state=self._state, data=data)
11221121

@@ -1705,7 +1704,7 @@ async def edit(
17051704
)
17061705
if payload is not None:
17071706
# the payload will always be the proper channel payload
1708-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
1707+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
17091708
return None
17101709

17111710
async def delete_messages(self, messages: Iterable[Snowflake]) -> None:
@@ -2563,7 +2562,7 @@ async def edit(
25632562
)
25642563
if payload is not None:
25652564
# the payload will always be the proper channel payload
2566-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
2565+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
25672566
return None
25682567

25692568
async def delete_messages(self, messages: Iterable[Snowflake]) -> None:
@@ -3056,7 +3055,7 @@ async def edit(
30563055
)
30573056
if payload is not None:
30583057
# the payload will always be the proper channel payload
3059-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
3058+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
30603059
return None
30613060

30623061
@overload
@@ -3472,7 +3471,7 @@ def last_thread(self) -> Optional[Thread]:
34723471
:class:`Thread` | :data:`None`
34733472
The last created thread in this channel or :data:`None` if not found.
34743473
"""
3475-
return self._state.get_channel(self.last_thread_id) if self.last_thread_id else None # type: ignore
3474+
return self._state.get_channel(self.last_thread_id) if self.last_thread_id else None # pyright: ignore[reportReturnType]
34763475

34773476
@property
34783477
def available_tags(self) -> list[ForumTag]:
@@ -4220,7 +4219,7 @@ async def edit(
42204219
)
42214220
if payload is not None:
42224221
# the payload will always be the proper channel payload
4223-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
4222+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
42244223
return None
42254224

42264225
async def clone(
@@ -4619,7 +4618,7 @@ async def edit(
46194618
)
46204619
if payload is not None:
46214620
# the payload will always be the proper channel payload
4622-
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
4621+
return self.__class__(state=self._state, guild=self.guild, data=payload) # pyright: ignore[reportArgumentType]
46234622
return None
46244623

46254624
async def clone(
@@ -4794,7 +4793,7 @@ def __init__(self, *, me: ClientUser, state: ConnectionState, data: DMChannelPay
47944793
self._state: ConnectionState = state
47954794
self.recipient: Optional[User] = None
47964795
if recipients := data.get("recipients"):
4797-
self.recipient = state.store_user(recipients[0]) # type: ignore
4796+
self.recipient = state.store_user(recipients[0]) # pyright: ignore[reportArgumentType]
47984797

47994798
self.me: ClientUser = me
48004799
self.id: int = int(data["id"])

0 commit comments

Comments
 (0)