You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow typing.Annotated hints for defining options.
What is the feature request for?
The core library
The Problem
Pycord allows commands to have options defined through parameter annotations, as in:
asyncdefecho(ctx, txt: Option(str, description="Text to echo")):
awaitctx.respond(txt)
however, this use of annotations precludes a static type checker from being able to be used on the associated parameter (that is, mypy for example gives a warning: "Invalid type comment or annotation [valid-type]". The standard library has introduced with PEP 593 a way for an annotation to provide both a type hint and metadata for that parameter with typing.Annotated. However, this library's command parsing methods are not able to interpret an annotation that is a typing.Annotated.
(As an aside, using decorators to define options is not a work around for this because while then the option's associated parameter in the callback function can have a type hint, the decorator hides from statis type analysis the callback function's parameter types/ return when called)
The Ideal Solution
Add to option parsing the ability to read annotations of the form,
Annotated[<type>, Option(<args>)
(ie. something like this would be valid.
asyncdefsquare(ctx, num: Annotated[int, Option(description="Text to echo")]):
awaitctx.respond(num**2)
) by for instance, by checking if option (as in commands/core.py:731) is an instance of typing.Annotated, picking the input type for the option from option.__args__[0], and iterating through option.__metadata__ trying to find an instance of discord.Option.
The Current Solution
As far as I'm aware, the only way to avoid static type checker warning in a command function definition is ignoring them.
Summary
Allow typing.Annotated hints for defining options.
What is the feature request for?
The core library
The Problem
Pycord allows commands to have options defined through parameter annotations, as in:
however, this use of annotations precludes a static type checker from being able to be used on the associated parameter (that is, mypy for example gives a warning: "Invalid type comment or annotation [valid-type]". The standard library has introduced with PEP 593 a way for an annotation to provide both a type hint and metadata for that parameter with
typing.Annotated
. However, this library's command parsing methods are not able to interpret an annotation that is atyping.Annotated
.(As an aside, using decorators to define options is not a work around for this because while then the option's associated parameter in the callback function can have a type hint, the decorator hides from statis type analysis the callback function's parameter types/ return when called)
The Ideal Solution
Add to option parsing the ability to read annotations of the form,
(ie. something like this would be valid.
) by for instance, by checking if
option
(as in commands/core.py:731) is an instance oftyping.Annotated
, picking the input type for the option fromoption.__args__[0]
, and iterating throughoption.__metadata__
trying to find an instance ofdiscord.Option
.The Current Solution
As far as I'm aware, the only way to avoid static type checker warning in a command function definition is ignoring them.
Additional Context
A brief test for the above ideal solution is
which fails with
The text was updated successfully, but these errors were encountered: