Ces*_*esc 6 python-3.x mypy typer
有没有办法让 mypy 对这段代码感到满意而无需更改class Config
,目前我无法更改该类(对 cli 应用程序使用 typer)。我知道我可以使用# type: ignore
,但是还有其他解决方案吗?
"""
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)
Run Code Online (Sandbox Code Playgroud)
Mypy v0.782 不会抱怨上面的代码,但 0.812 会抱怨。
小智 3
我遇到了和你类似的问题,并通过使用Final
注释修复了它。例如
from typing import Final
STRICT: Final = "strict"
Run Code Online (Sandbox Code Playgroud)
我在这里找到提示:/sf/answers/4472467051/
Annotating a variable as Final indicates that its value will not be substituted for a value of similar type. This makes it correct to infer the type as the specific Literal value, instead of just the general type.
归档时间: |
|
查看次数: |
3639 次 |
最近记录: |