Python 如何键入实现`\u调用``方法的提示类型?

Python 如何键入实现`\u调用``方法的提示类型?,python,pycharm,type-hinting,Python,Pycharm,Type Hinting,我正在用Python 2.7进行实验。在大多数情况下,它非常简单,但我在下面的函数中遇到了一些问题。我试过: from typing import TypeVar, Type, Callable, Union, Dict T = TypeVar('T') def ensure_cls(cls): # type: (Type[T]) -> Callable[[Union[T, Dict]], T] """ If the argument is an instanc

我正在用Python 2.7进行实验。在大多数情况下,它非常简单,但我在下面的函数中遇到了一些问题。我试过:

from typing import TypeVar, Type, Callable, Union, Dict

T = TypeVar('T')

def ensure_cls(cls):
    # type: (Type[T]) -> Callable[[Union[T, Dict]], T]
    """
    If the argument is an instance of cls, pass, else try constructing.
    """

    def converter(val):
        # type: (Union[T, Dict]) -> T
        if isinstance(val, cls):
            return val
        else:
            return cls(**val)  # 'cls' is not callable

    return converter
PyCharm(2017.1.2)抱怨“cls”不可调用。这可能是PyCharm检查的误报,但另一方面,PyCharm确实有一点:并非所有类型都可以调用

也许有更好的方法来类型提示这个函数并指定cls实际上是一个可调用的类型


建议以什么方式键入上述函数提示?

您在此处找到的上述函数的用例:您在此处找到的上述函数的用例: