Python type hinting: how to tell X is a subclass for Foo?
我应该如何为Python中的类类型编写一个类型提示?考虑此代码:
1 2 3 4 5 6 7 8 9 10 11 | class A(object): pass class B(A): pass def register(cls: type[A]): assert issubclass(cls, A) register(A) register(B) |
具体来说,我想指出的是,参数应该是Django型号。
其他当前(2016年9月22日)的答案似乎不正确。根据PEP484(关于类型提示),存在一个类对象类型的提示,称为类型[C]。根据
引用政治公众人物:
Sometimes you want to talk about class objects, in particular class objects that inherit from a given class. This can be spelled as Type[C] where C is a class. To clarify: while C (when used as an annotation) refers to instances of class C , Type[C] refers to subclasses of C .
号
并引用文件:
A variable annotated with C may accept a value of type C. In contrast, a variable annotated with Type[C] may accept values that are classes themselves – specifically, it will accept the class object of C.
号
并参考您的具体示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import typing class A(object): pass class B(A): pass def register(cls: typing.Type[A]): assert issubclass(cls, A) register(A) register(B) |
号
您可以使用mypy静态地检查这些代码,它应该在简单的情况下工作——但是要注意mypy是一个正在进行的工作,到目前为止,关于类型[C]提示还有几个问题尚待解决。
为了解决您的一般情况,您必须使用适当的
在Django模型类的特定情况下,已经存在一个显式元类,因此注释应该可以完成以下工作:
1 2 3 | import django.db.model as model def register(cls: model.base.ModelBase): ... |
这是因为