假设我有一个用作工厂的类方法:
class Foo:
def __init__(self, text):
self.text = text
@classmethod
def from_file(cls, path):
with open(path, 'rt') as f:
return cls(f.read())
class Bar(Foo):
def lines(self):
return self.text.count('\n')
print(Bar.from_file('bar.txt').lines())
现在我想为此添加 pytype 注释。from_file
我应该为类方法使用哪些注释?仅将其标记为-> 'Foo'
不会捕获在派生类(如Bar
. 所以print
调用中的表达式不会知道它是 aBar
和 has lines
。我如何表达结果将是参数的一个实例cls
?