I am using prompt-toolkit python library with the code:
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()
When I run mypy 0.931 I get:
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]]]]"
I am not sure if the problem is with my code, since the return value is something like ['eggs', 'bacon']
which is a list[str]
. Also this error from mypy is strange, since I don't think I should use covariant here. Any hints on what may be the problem?
I think the problem is that mypy has very little information about the checkboxlist_dialog
function, and certainly doesn't know that its return type can be figured out from the value
argument.
You may instead have to write:
from typing import cast
results = cast(list[string], checkboxlist_dialog(....))
which tells mypy that you know what you're doing, and the return type really is a list[string]
, no matter what it thinks.