Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mypy v0.812 incompatible type "str"; expected "Union[Literal['a'], Union[Literal['b']..]

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.

like image 225
Cesc Avatar asked Nov 07 '22 02:11

Cesc


1 Answers

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)
like image 93
Cesc Avatar answered Nov 27 '22 01:11

Cesc