adapter-discord icon indicating copy to clipboard operation
adapter-discord copied to clipboard

给on_slash_command提供动态options时无法通过类型检查

Open Shedarshian opened this issue 9 months ago • 2 comments

在使用on_slash_command新建斜杠指令时,为options提供动态的列表,会因为list不是covariant的而报错,即使是仅仅将两个静态列表相加也会报错。

MWE:

from nonebot.adapters.discord import on_slash_command
from nonebot.adapters.discord.api import StringOption
test = on_slash_command(name="test", description="test",
    options=[StringOption(name="1", description="1")] + [])

Pylance报错:

Argument of type "list[StringOption]" cannot be assigned to parameter "options" of type "List[AnyCommandOption] | None" in function "on_slash_command"
  Type "list[StringOption]" is incompatible with type "List[AnyCommandOption] | None"
    "list[StringOption]" is incompatible with "List[AnyCommandOption]"
      Type parameter "_T@list" is invariant, but "StringOption" is not the same as "AnyCommandOption"
      Consider switching from "list" to "Sequence" which is covariant
    "list[StringOption]" is incompatible with "None"

mypy报错:

Argument "options" to "on_slash_command" has incompatible type "list[StringOption]"; expected "list[SubCommandGroupOption | SubCommandOption | IntegerOption | StringOption | UserOption | ChannelOption | RoleOption | MentionableOption | NumberOption | BooleanOption | AttachmentOption] | None"Mypy[arg-type](https://mypy.readthedocs.io/en/latest/_refs.html#code-arg-type)
"List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
Consider using "Sequence" instead, which is covariant

Shedarshian avatar May 18 '24 06:05 Shedarshian