Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Shedarshian opened this issue May 18, 2024 · 2 comments
Open

Comments

@Shedarshian
Copy link

在使用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
@Autuamn
Copy link
Contributor

Autuamn commented May 21, 2024

试试为动态的 options 指定类型

example:

from nonebot.adapters.discord import on_slash_command
from nonebot.adapters.discord.api import AnyCommandOption, StringOption

options: list[AnyCommandOption] = [StringOption(name="1", description="1")]

test = on_slash_command(name="test", description="test",
    options=options)

@Shedarshian
Copy link
Author

唔 把它拆成两行似乎是可以的

options: list[AnyCommandOption] = [StringOption(name="1", description="1")]
options += []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants