找出在某些条件下返回空哈希的方法的返回值

sco*_*n35 2 python

我试图了解如何进行这项工作:

def someMethod() -> dict[any, any]:
    if not os.path.exists('some path'):
        return {}

    config = {'a': 1, 'b': 2}
    return config
Run Code Online (Sandbox Code Playgroud)

我认为这是不正确的。看到这个错误——Declared return type, "dict[Unknown, Unknown]", is partially unknownPylance

这个想法是,如果路径不存在(或在某些条件下),则返回空字典,或者使用键值对纠正字典。

有任何想法吗?

fto*_*rre 5

小写的any是Python内置函数而不是类型。相反,您必须从打字模块导入大写Any 。

from typing import Any
import os

def someMethod() -> dict[Any, Any]:
    if not os.path.exists('some path'):
        return {}

    config = {'a': 1, 'b': 2}
    return config
Run Code Online (Sandbox Code Playgroud)