如何为通用工厂方法创建类型提示?

How to create type hinting for a generic factory method?

如何声明类型提示以指示函数 returns 作为参数传递的 class 引用的实例?

如下声明似乎不对,因为它表明返回类型与参数类型相同:

from typing import TypeVar


T = TypeVar('T')

def my_factory(some_class: T) -> T:
    instance_of_some_class = some_class()
    return instance_of_some_class

用法示例:

class MyClass:
    pass

my_class = my_factory(MyClass)  # Inferred type should be MyClass

根据 PEP-484,正确的做法是使用 Type[T] 作为参数:

from typing import TypeVar, Type


T = TypeVar('T')

def my_factory(some_class: Type[T]) -> T:
    instance_of_some_class = some_class()
    return instance_of_some_class

然而,我的编辑似乎(还)不支持这一点。