Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit c9f777c

Browse files
authored
Fix type annotations to adhere to latest pyright release
1 parent 334ef1d commit c9f777c

File tree

14 files changed

+50
-47
lines changed

14 files changed

+50
-47
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: Run Pyright
3939
uses: jakebailey/pyright-action@v1
4040
with:
41-
version: '1.1.242'
41+
version: '1.1.253'
4242
warnings: false
4343
no-comments: ${{ matrix.python-version != '3.x' }}
4444

discord/app_commands/commands.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,7 @@ async def _check_can_run(self, interaction: Interaction) -> bool:
771771
if not predicates:
772772
return True
773773

774-
# Type checker does not understand negative narrowing cases like this function
775-
return await async_all(f(interaction) for f in predicates) # type: ignore
774+
return await async_all(f(interaction) for f in predicates)
776775

777776
def error(self, coro: Error[GroupT]) -> Error[GroupT]:
778777
"""A decorator that registers a coroutine as a local error handler.
@@ -997,8 +996,7 @@ async def _check_can_run(self, interaction: Interaction) -> bool:
997996
if not predicates:
998997
return True
999998

1000-
# Type checker does not understand negative narrowing cases like this function
1001-
return await async_all(f(interaction) for f in predicates) # type: ignore
999+
return await async_all(f(interaction) for f in predicates)
10021000

10031001
def _has_any_error_handlers(self) -> bool:
10041002
return self.on_error is not None

discord/app_commands/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ class AllChannels:
9696
__slots__ = ('guild',)
9797

9898
def __init__(self, guild: Guild):
99-
self.guild = guild
99+
self.guild: Guild = guild
100100

101101
@property
102102
def id(self) -> int:
103103
""":class:`int`: The ID sentinel used to represent all channels. Equivalent to the guild's ID minus 1."""
104104
return self.guild.id - 1
105105

106-
def __repr__(self):
106+
def __repr__(self) -> str:
107107
return f'<AllChannels guild={self.guild}>'
108108

109109

discord/errors.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727

2828
if TYPE_CHECKING:
2929
from aiohttp import ClientResponse, ClientWebSocketResponse
30+
from requests import Response
3031

31-
try:
32-
from requests import Response
33-
34-
_ResponseType = Union[ClientResponse, Response]
35-
except ModuleNotFoundError:
36-
_ResponseType = ClientResponse
32+
_ResponseType = Union[ClientResponse, Response]
3733

3834
from .interactions import Interaction
3935

discord/ext/commands/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
ContextT_co = TypeVar('ContextT_co', bound='Context[Any]', covariant=True)
6060

6161

62-
class Check(Protocol[ContextT_co]):
62+
class Check(Protocol[ContextT_co]): # type: ignore # TypeVar is expected to be invariant
6363

6464
predicate: Callable[[ContextT_co], Coroutine[Any, Any, bool]]
6565

discord/ext/commands/bot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ async def can_run(self, ctx: Context[BotT], /, *, call_once: bool = False) -> bo
460460
if len(data) == 0:
461461
return True
462462

463-
# type-checker doesn't distinguish between functions and methods
464-
return await discord.utils.async_all(f(ctx) for f in data) # type: ignore
463+
return await discord.utils.async_all(f(ctx) for f in data)
465464

466465
async def is_owner(self, user: User, /) -> bool:
467466
"""|coro|

discord/ext/commands/context.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from ._types import BotT
3636

3737
if TYPE_CHECKING:
38-
from typing_extensions import Self, ParamSpec
38+
from typing_extensions import Self, ParamSpec, TypeGuard
3939

4040
from discord.abc import MessageableChannel
4141
from discord.guild import Guild
@@ -77,6 +77,10 @@
7777
P = TypeVar('P')
7878

7979

80+
def is_cog(obj: Any) -> TypeGuard[Cog]:
81+
return hasattr(obj, '__cog_commands__')
82+
83+
8084
class DeferTyping:
8185
def __init__(self, ctx: Context[BotT], *, ephemeral: bool):
8286
self.ctx: Context[BotT] = ctx
@@ -526,7 +530,7 @@ async def send_help(self, *args: Any) -> Any:
526530
await cmd.prepare_help_command(self, entity.qualified_name)
527531

528532
try:
529-
if hasattr(entity, '__cog_commands__'):
533+
if is_cog(entity):
530534
injected = wrap_callback(cmd.send_cog_help)
531535
return await injected(entity)
532536
elif isinstance(entity, Group):

discord/ext/commands/converter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ async def convert(self, ctx: Context[BotT], argument: str) -> discord.Member:
234234
guild = ctx.guild
235235
result = None
236236
user_id = None
237+
237238
if match is None:
238239
# not a mention...
239240
if guild:
@@ -247,7 +248,7 @@ async def convert(self, ctx: Context[BotT], argument: str) -> discord.Member:
247248
else:
248249
result = _get_from_guilds(bot, 'get_member', user_id)
249250

250-
if result is None:
251+
if not isinstance(result, discord.Member):
251252
if guild is None:
252253
raise MemberNotFound(argument)
253254

@@ -1182,7 +1183,7 @@ async def _actual_conversion(ctx: Context[BotT], converter: Any, argument: str,
11821183
except CommandError:
11831184
raise
11841185
except Exception as exc:
1185-
raise ConversionError(converter, exc) from exc
1186+
raise ConversionError(converter, exc) from exc # type: ignore
11861187

11871188
try:
11881189
return converter(argument)

discord/ext/commands/core.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Self:
354354
def __init__(
355355
self,
356356
func: Union[
357-
Callable[Concatenate[CogT, ContextT, P], Coro[T]],
358-
Callable[Concatenate[ContextT, P], Coro[T]],
357+
Callable[Concatenate[CogT, Context[Any], P], Coro[T]],
358+
Callable[Concatenate[Context[Any], P], Coro[T]],
359359
],
360360
/,
361361
**kwargs: Any,
@@ -399,7 +399,7 @@ def __init__(
399399
except AttributeError:
400400
checks = kwargs.get('checks', [])
401401

402-
self.checks: List[UserCheck[ContextT]] = checks
402+
self.checks: List[UserCheck[Context[Any]]] = checks
403403

404404
try:
405405
cooldown = func.__commands_cooldown__
@@ -479,7 +479,7 @@ def callback(
479479

480480
self.params: Dict[str, Parameter] = get_signature_parameters(function, globalns)
481481

482-
def add_check(self, func: UserCheck[ContextT], /) -> None:
482+
def add_check(self, func: UserCheck[Context[Any]], /) -> None:
483483
"""Adds a check to the command.
484484
485485
This is the non-decorator interface to :func:`.check`.
@@ -500,7 +500,7 @@ def add_check(self, func: UserCheck[ContextT], /) -> None:
500500

501501
self.checks.append(func)
502502

503-
def remove_check(self, func: UserCheck[ContextT], /) -> None:
503+
def remove_check(self, func: UserCheck[Context[Any]], /) -> None:
504504
"""Removes a check from the command.
505505
506506
This function is idempotent and will not raise an exception
@@ -1249,7 +1249,7 @@ async def can_run(self, ctx: Context[BotT], /) -> bool:
12491249
# since we have no checks, then we just return True.
12501250
return True
12511251

1252-
return await discord.utils.async_all(predicate(ctx) for predicate in predicates) # type: ignore
1252+
return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
12531253
finally:
12541254
ctx.command = original
12551255

@@ -1448,7 +1448,7 @@ def command(
14481448
def command(
14491449
self: GroupMixin[CogT],
14501450
name: str = ...,
1451-
cls: Type[CommandT] = ...,
1451+
cls: Type[CommandT] = ..., # type: ignore # previous overload handles case where cls is not set
14521452
*args: Any,
14531453
**kwargs: Any,
14541454
) -> Callable[
@@ -1508,7 +1508,7 @@ def group(
15081508
def group(
15091509
self: GroupMixin[CogT],
15101510
name: str = ...,
1511-
cls: Type[GroupT] = ...,
1511+
cls: Type[GroupT] = ..., # type: ignore # previous overload handles case where cls is not set
15121512
*args: Any,
15131513
**kwargs: Any,
15141514
) -> Callable[
@@ -1700,7 +1700,7 @@ def command(
17001700
@overload
17011701
def command(
17021702
name: str = ...,
1703-
cls: Type[CommandT] = ...,
1703+
cls: Type[CommandT] = ..., # type: ignore # previous overload handles case where cls is not set
17041704
**attrs: Any,
17051705
) -> Callable[
17061706
[
@@ -1770,7 +1770,7 @@ def group(
17701770
@overload
17711771
def group(
17721772
name: str = ...,
1773-
cls: Type[GroupT] = ...,
1773+
cls: Type[GroupT] = ..., # type: ignore # previous overload handles case where cls is not set
17741774
**attrs: Any,
17751775
) -> Callable[
17761776
[
@@ -1878,9 +1878,9 @@ async def only_me(ctx):
18781878
The predicate to check if the command should be invoked.
18791879
"""
18801880

1881-
def decorator(func: Union[Command, CoroFunc]) -> Union[Command, CoroFunc]:
1881+
def decorator(func: Union[Command[Any, ..., Any], CoroFunc]) -> Union[Command[Any, ..., Any], CoroFunc]:
18821882
if isinstance(func, Command):
1883-
func.checks.append(predicate)
1883+
func.checks.append(predicate) # type: ignore
18841884
else:
18851885
if not hasattr(func, '__commands_checks__'):
18861886
func.__commands_checks__ = []

discord/ext/commands/help.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161

6262
from ._types import (
6363
UserCheck,
64-
ContextT,
6564
BotT,
6665
_Bot,
6766
)
@@ -378,7 +377,7 @@ def _remove_from_bot(self, bot: BotBase) -> None:
378377
bot.remove_command(self._command_impl.name)
379378
self._command_impl._eject_cog()
380379

381-
def add_check(self, func: UserCheck[ContextT], /) -> None:
380+
def add_check(self, func: UserCheck[Context[Any]], /) -> None:
382381
"""
383382
Adds a check to the help command.
384383
@@ -398,7 +397,7 @@ def add_check(self, func: UserCheck[ContextT], /) -> None:
398397

399398
self._command_impl.add_check(func)
400399

401-
def remove_check(self, func: UserCheck[ContextT], /) -> None:
400+
def remove_check(self, func: UserCheck[Context[Any]], /) -> None:
402401
"""
403402
Removes a check from the help command.
404403

0 commit comments

Comments
 (0)