相关疑难解决方法(0)

在 Python 类型注释中排除类型

我写了以下函数:

def _clean_dict(d):
    return {k: v for k, v in d.items() if v is not None}
Run Code Online (Sandbox Code Playgroud)

我想为函数添加类型注释:

def _clean_dict(d: Dict[Any, Any]) -> Dict[Any, Any]:                           
    return {k: v for k, v in d.items() if v is not None}
Run Code Online (Sandbox Code Playgroud)

但是,我想明确定义返回的字典中的值不能为 None。

有没有办法说“Any类型,除了NoneType”或“每个可能的值但是None”?

python type-hinting python-3.x mypy python-typing

11
推荐指数
2
解决办法
1683
查看次数

标签 统计

mypy ×1

python ×1

python-3.x ×1

python-typing ×1

type-hinting ×1