mypy 不兼容类型列表<nothing> 具有类型列表<str>
mypy incompatible types list<nothing> has the type list<str>
我正在使用带有代码的提示工具包 python 库:
from __future__ import annotations
from prompt_toolkit.shortcuts import checkboxlist_dialog
results: list[str] = checkboxlist_dialog(
title="CheckboxList dialog",
text="What would you like in your breakfast ?",
values=[
("eggs", "Eggs"),
("bacon", "Bacon"),
("croissants", "20 Croissants"),
("daily", "The breakfast of the day"),
],
).run()
当我 运行 mypy 0.931 我得到:
test.py:4: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "List[str]")
test.py:4: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
test.py:4: note: Consider using "Sequence" instead, which is covariant
test.py:7: error: Argument "values" to "checkboxlist_dialog" has incompatible type "List[Tuple[str, str]]"; expected "Optional[List[Tuple[<nothing>, Union[str, MagicFormattedText, List[Union[Tuple[str, str], Tuple[str, str, Callable[[MouseEvent], None]]]], Callable[[], Any], None]]]]"
我不确定问题是否出在我的代码上,因为 return 值类似于 ['eggs', 'bacon']
,即 list[str]
。 mypy 的这个错误也很奇怪,因为我认为我不应该在这里使用协变。关于可能是什么问题的任何提示?
我认为问题在于mypy关于checkboxlist_dialog
函数的信息很少,当然不知道它的return类型可以从value
中算出来参数。
您可能不得不写成:
from typing import cast
results = cast(list[string], checkboxlist_dialog(....))
这告诉 mypy 你知道自己在做什么,而且 return 类型确实是一个 list[string]
,不管它怎么想。
我正在使用带有代码的提示工具包 python 库:
from __future__ import annotations
from prompt_toolkit.shortcuts import checkboxlist_dialog
results: list[str] = checkboxlist_dialog(
title="CheckboxList dialog",
text="What would you like in your breakfast ?",
values=[
("eggs", "Eggs"),
("bacon", "Bacon"),
("croissants", "20 Croissants"),
("daily", "The breakfast of the day"),
],
).run()
当我 运行 mypy 0.931 我得到:
test.py:4: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "List[str]")
test.py:4: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
test.py:4: note: Consider using "Sequence" instead, which is covariant
test.py:7: error: Argument "values" to "checkboxlist_dialog" has incompatible type "List[Tuple[str, str]]"; expected "Optional[List[Tuple[<nothing>, Union[str, MagicFormattedText, List[Union[Tuple[str, str], Tuple[str, str, Callable[[MouseEvent], None]]]], Callable[[], Any], None]]]]"
我不确定问题是否出在我的代码上,因为 return 值类似于 ['eggs', 'bacon']
,即 list[str]
。 mypy 的这个错误也很奇怪,因为我认为我不应该在这里使用协变。关于可能是什么问题的任何提示?
我认为问题在于mypy关于checkboxlist_dialog
函数的信息很少,当然不知道它的return类型可以从value
中算出来参数。
您可能不得不写成:
from typing import cast
results = cast(list[string], checkboxlist_dialog(....))
这告诉 mypy 你知道自己在做什么,而且 return 类型确实是一个 list[string]
,不管它怎么想。