Use typing.Self throughout library

This commit is contained in:
Josh
2022-03-01 22:53:24 +10:00
committed by GitHub
parent a90e1824f4
commit 147948af9b
28 changed files with 212 additions and 191 deletions

View File

@ -31,6 +31,8 @@ from typing import Any, Callable, ClassVar, Dict, Generator, List, Optional, TYP
from ._types import _BaseCommand
if TYPE_CHECKING:
from typing_extensions import Self
from .bot import BotBase
from .context import Context
from .core import Command
@ -40,7 +42,6 @@ __all__ = (
'Cog',
)
CogT = TypeVar('CogT', bound='Cog')
FuncT = TypeVar('FuncT', bound=Callable[..., Any])
MISSING: Any = discord.utils.MISSING
@ -111,7 +112,7 @@ class CogMeta(type):
__cog_commands__: List[Command]
__cog_listeners__: List[Tuple[str, str]]
def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
name, bases, attrs = args
attrs['__cog_name__'] = kwargs.pop('name', name)
attrs['__cog_settings__'] = kwargs.pop('command_attrs', {})
@ -190,10 +191,10 @@ class Cog(metaclass=CogMeta):
__cog_name__: ClassVar[str]
__cog_settings__: ClassVar[Dict[str, Any]]
__cog_commands__: ClassVar[List[Command]]
__cog_commands__: ClassVar[List[Command[Self, Any, Any]]]
__cog_listeners__: ClassVar[List[Tuple[str, str]]]
def __new__(cls: Type[CogT], *args: Any, **kwargs: Any) -> CogT:
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
# For issue 426, we need to store a copy of the command objects
# since we modify them to inject `self` to them.
# To do this, we need to interfere with the Cog creation process.
@ -220,7 +221,7 @@ class Cog(metaclass=CogMeta):
return self
def get_commands(self) -> List[Command]:
def get_commands(self) -> List[Command[Self, Any, Any]]:
r"""
Returns
--------
@ -248,7 +249,7 @@ class Cog(metaclass=CogMeta):
def description(self, description: str) -> None:
self.__cog_description__ = description
def walk_commands(self) -> Generator[Command, None, None]:
def walk_commands(self) -> Generator[Command[Self, Any, Any], None, None]:
"""An iterator that recursively walks through this cog's commands and subcommands.
Yields
@ -418,7 +419,7 @@ class Cog(metaclass=CogMeta):
"""
pass
def _inject(self: CogT, bot: BotBase) -> CogT:
def _inject(self, bot: BotBase) -> Self:
cls = self.__class__
# realistically, the only thing that can cause loading errors