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

@ -66,6 +66,8 @@ __all__ = (
if TYPE_CHECKING:
from typing_extensions import Self
from .context import Context
@ -265,7 +267,7 @@ class FlagsMeta(type):
__commands_flag_prefix__: str
def __new__(
cls: Type[type],
cls,
name: str,
bases: Tuple[type, ...],
attrs: Dict[str, Any],
@ -273,7 +275,7 @@ class FlagsMeta(type):
case_insensitive: bool = MISSING,
delimiter: str = MISSING,
prefix: str = MISSING,
):
) -> Self:
attrs['__commands_is_flag__'] = True
try:
@ -432,9 +434,6 @@ async def convert_flag(ctx, argument: str, flag: Flag, annotation: Any = None) -
raise BadFlagArgument(flag) from e
F = TypeVar('F', bound='FlagConverter')
class FlagConverter(metaclass=FlagsMeta):
"""A converter that allows for a user-friendly flag syntax.
@ -481,8 +480,8 @@ class FlagConverter(metaclass=FlagsMeta):
yield (flag.name, getattr(self, flag.attribute))
@classmethod
async def _construct_default(cls: Type[F], ctx: Context) -> F:
self: F = cls.__new__(cls)
async def _construct_default(cls, ctx: Context) -> Self:
self = cls.__new__(cls)
flags = cls.__commands_flags__
for flag in flags.values():
if callable(flag.default):
@ -547,7 +546,7 @@ class FlagConverter(metaclass=FlagsMeta):
return result
@classmethod
async def convert(cls: Type[F], ctx: Context, argument: str) -> F:
async def convert(cls, ctx: Context, argument: str) -> Self:
"""|coro|
The method that actually converters an argument to the flag mapping.
@ -576,7 +575,7 @@ class FlagConverter(metaclass=FlagsMeta):
arguments = cls.parse_flags(argument)
flags = cls.__commands_flags__
self: F = cls.__new__(cls)
self = cls.__new__(cls)
for name, flag in flags.items():
try:
values = arguments[name]