Is there a way to make mypy happy with this code without changing class Config
, at the moment I cannot change that class (using typer for a cli app). I am aware I can use # type: ignore
but is there any other solution?
"""
mypy --version
mypy 0.812
"""
from enum import Enum
from pathlib import Path
from typing_extensions import Literal
class Config(str, Enum):
Debug = 'Debug'
Release = 'Release'
BuildMode = Literal["ASAN", "UBSAN", "Coverage", "Release", "Debug"]
def run_c_tests(results_dir: Path, mode: BuildMode):
...
configuration = Config.Debug
# mypy error: Argument 2 to "run_c_tests" has incompatible type "str"; expected "Union[Literal['ASAN'], Literal['UBSAN'], Literal['Coverage'], Literal['Release'], Literal['Debug']]"
run_c_tests(Path('apath'), configuration.value)
Mypy v0.782 does not complain on the code above but 0.812 does.
As of today the best I could do was to simply ignore the problem
# type: ignore[arg-type]
I also tried adding an assert by duplicating the same tuple already available in Literal, this assert did not help mypy.
assert configuration.value in ["ASAN", "UBSAN", "Coverage", "Release", "Debug"]
run_c_tests(Path('apath'), configuration.value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With