Skip to content

Commit 0990633

Browse files
committed
Made an assumption (see PR) to eliminate all mypy errors
1 parent 1888b06 commit 0990633

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

extensions/settings/objects/server_settings.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import discord
1111

1212
from resources.utils.debug import debug, DebugColor
13-
from .server_attributes import ServerAttributes, GuildAttributeType
13+
from .server_attributes import ServerAttributes, GuildAttributeType, MessageableGuildChannel
1414
from .server_attribute_ids import ServerAttributeIds
1515
from .enabled_modules import EnabledModules
1616

@@ -123,7 +123,7 @@ def is_attribute_type(val: type):
123123
)
124124
return f
125125

126-
funcs: set[Callable[[int], GuildAttributeType | discord.abc.PrivateChannel | None]] = set()
126+
funcs: set[Callable[[int], GuildAttributeType| None]] = set()
127127

128128
if is_attribute_type(discord.Guild):
129129
funcs.add(client.get_guild)
@@ -133,9 +133,10 @@ def is_attribute_type(val: type):
133133
# parse if the type matches exactly.
134134
funcs.add(guild.get_channel_or_thread)
135135
if is_attribute_type(discord.abc.Messageable):
136-
funcs.add(client.get_channel)
136+
# There is no attribute that gives a PrivateChannel
137+
funcs.add(typing.cast(Callable[[int], MessageableGuildChannel|None], client.get_channel))
137138
if is_attribute_type(discord.TextChannel):
138-
funcs.add(client.get_channel)
139+
funcs.add(typing.cast(Callable[[int], discord.TextChannel|None], client.get_channel))
139140
if is_attribute_type(discord.User):
140141
funcs.add(client.get_user)
141142
if is_attribute_type(discord.Role):
@@ -144,9 +145,9 @@ def is_attribute_type(val: type):
144145
# I think it's safe to assume the stored value was an object of
145146
# the correct type in the first place. As in, it's a
146147
# CategoryChannel id, not a VoiceChannel id.
147-
funcs.add(client.get_channel)
148+
funcs.add(typing.cast(Callable[[int], discord.CategoryChannel|None], client.get_channel))
148149
if is_attribute_type(discord.channel.VoiceChannel):
149-
funcs.add(client.get_channel)
150+
funcs.add(typing.cast(Callable[[int], discord.VoiceChannel|None], client.get_channel))
150151
if is_attribute_type(discord.Emoji):
151152
funcs.add(guild.get_emoji)
152153
if is_attribute_type(int):

resources/customs/bot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# ^ for scheduling Reminders
99
from datetime import datetime
1010
# ^ for startup and crash logging, and Reminders
11-
from typing import TYPE_CHECKING, TypeVar
11+
from typing import TYPE_CHECKING, TypeVar, reveal_type, cast
1212
import motor.core as motorcore # for typing
1313
from pymongo.database import Database as PyMongoDatabase
1414
# ^ for MongoDB database typing
@@ -28,7 +28,7 @@
2828

2929
T = TypeVar('T')
3030
G = TypeVar('G')
31-
31+
type GetGuildAttributeReturn[T] = GuildAttributeType | T | None | list["GetGuildAttributeReturn"[T]]
3232

3333
class Bot(commands.Bot):
3434
# bot uptime start, used in /version in cmd_staffaddons
@@ -132,7 +132,7 @@ def get_guild_attribute(
132132
guild: discord.Guild | int,
133133
*args: str,
134134
default: T | None = None
135-
) -> GuildAttributeType | T | None | list[GuildAttributeType | T | None]:
135+
) -> GetGuildAttributeReturn[T]:
136136
"""
137137
Get ServerSettings attributes for the given guild.
138138
@@ -161,12 +161,12 @@ def get_guild_attribute(
161161
# doesn't have any settings (perhaps the bot was recently
162162
# added).
163163
if len(args) > 1:
164-
return [default for _ in args]
164+
return [cast(GetGuildAttributeReturn[T], default) for _ in args]
165165
return default
166166

167167
attributes = self.server_settings[guild_id].attributes
168168

169-
output: list[GuildAttributeType | T] = []
169+
output: list[GetGuildAttributeReturn[T]] = []
170170

171171
parent_server = attributes[AttributeKeys.parent_server] # type: ignore[literal-required]
172172

0 commit comments

Comments
 (0)