引用通用类型的类属性

时间:2019-06-12 15:36:36

标签: python typing mypy python-typing

我有一个抽象的基类,它定义了几个本身就是类型的类属性(即嵌套类型);这些属性在继承自抽象类的具体派生类中被覆盖。具体的派生类与其他几个类型标注为泛型的类一起使用,其中类型参数具有必须从抽象类派生的类型约束。我希望这些其他类(泛型)引用具体类的类属性(嵌套类型)。下面的示例应说明这一想法:

import typing

class Base:
    A: typing.Type[Base]

class One(Base):
    class A(Base):  # overrides Base.A
        pass

class Two(Base):
    class A(One):  # overrides Base.A
        pass


T = typing.TypeVar('T', bound=Base)

class Foo(typing.Generic[T]):                 # the generic
    def foo(self, a: typing.Type[T.A]) -> T:  # <-- here, see T.A
        ...

f: Foo[Two] = Foo()
f.foo(Two.A)        # line 22 is here

MyPy v0.701在第22行报告错误:

  

“ Foo”的“ foo”的参数1具有不兼容的类型“ Type [A]”;预期的“ Type [Two]”

MyPy似乎忽略了T.A处的属性引用。如何理解我试图通过属性.A引用可用的类型?由于类型变量T被限制为Base,该变量具有A,因此保证该属性可用。注意,扩展泛型类型集不是一种选择。我可以对此进行详细说明,但说明可能对我的应用程序来说太具体了。

更新:带有--new-semantic-analyzer的MyPy v0.711产生了更易理解的错误消息:

  

未定义名称“ T.A”

1 个答案:

答案 0 :(得分:0)

我不确定这是否有帮助,但是如果您对绑定到Base的参数进行参数设置,它会起作用:

class Base:
    A: typing.Type['Base']


class One(Base):
    class A(Base):  # overrides Base.A
        pass


class Two(Base):
    class A(One):  # overrides Base.A
        pass


T = typing.TypeVar('T', bound='Base')
A = typing.TypeVar('A', bound='Base')


class Foo(typing.Generic[T, A]):    # the generic
    def foo(self, a: typing.Type[A]) -> T:  # <-- here, see T.A
        pass


f: Foo[Two, Two.A] = Foo()
f.foo(Two.A)        # line 22 is here

相关问题