【发布时间】:2021-12-17 04:50:34
【问题描述】:
有人知道为什么 MyPy 会抱怨这个吗?这非常令人沮丧,因为如果它是三元则它可以工作,但如果在标准 if/else 中则不能:
from typing import List, Optional, Union
def get_relevant_params() -> List[str]:
return sorted(list(set(['1', '2', '3'])))
# also doesn't work with: ... -> Union[List[str], None]
def get_config(config_path: Optional[str] = None) -> Optional[List[str]]:
# this doesn't work
if config_path:
read_cols = get_relevant_params()
else:
read_cols = None
# # this works
# read_cols = get_relevant_params() if config_path else None
return read_cols
这是一个带有示例的交互式 MyPy 游乐场: https://mypy-play.net/?mypy=latest&python=3.8&gist=2c846e569ecbd5f8884367393a754adc
【问题讨论】:
标签: python python-3.x mypy python-typing typing