Python 类型自引用类型别名

Python 类型自引用类型别名,python,mypy,Python,Mypy,我想要一个引用自身的类型: SelfReferenceType = Dict[str, Union[str, 'SelfReferenceType']] 使用python 3.5和最新的mypy,我可以: test.py:1: error: Invalid type "test.SelfReferenceType" 有没有一个简单的方法可以做到这一点?我猜正向引用只支持类,而不支持别名 我想做的是: SelfReferenceType = Dict[str, Union[str, 'Self

我想要一个引用自身的类型:

SelfReferenceType = Dict[str, Union[str, 'SelfReferenceType']]
使用python 3.5和最新的mypy,我可以:

test.py:1: error: Invalid type "test.SelfReferenceType"
有没有一个简单的方法可以做到这一点?我猜正向引用只支持类,而不支持别名

我想做的是:

SelfReferenceType = Dict[str, Union[str, 'SelfReferenceType']]

class Foo(object):
    def __init__():
        self.data = {} # type: SelfReferenceType

    # Functions that work on self.data

不幸的是,mypy当前不支持递归类型--您可以在此处跟踪未解决的问题:不幸的是,mypy当前不支持递归类型--您可以在此处跟踪未解决的问题: