从List [type]继承时的Pylint错误>> [

问题描述 投票:0回答:2
我编写了以下代码:

from typing import List class Foo(): def __init__(self, _a : int, _b : bool): self.a = _a self.b = _b class Bar(List[Foo]): def print(self): for entry in self: # Non-iterable value self is used in an iterating contextpylint(not-an-iterable) print(entry.a) print(entry.b) foo0 = Foo(0, True) foo1 = Foo(1, False) foo2 = Foo(2, True) foo3 = Foo(3, False) bar = Bar() bar.append(foo0) # Instance of 'Bar' has no 'append' memberpylint(no-member) bar.append(foo1) # Instance of 'Bar' has no 'append' memberpylint(no-member) bar.append(foo2) # Instance of 'Bar' has no 'append' memberpylint(no-member) bar.append(foo3) # Instance of 'Bar' has no 'append' memberpylint(no-member) bar.print()

它运行得很好,似乎可以完成预期的工作,但Pylint似乎并不喜欢它(注释中的错误消息。)>

有没有办法使此停止?

我编写了以下代码:输入import List类Foo():def __init __(self,_a:int,_b:bool):self.a = _a self.b = _b class Bar(List [Foo] ):def打印(...

python pylint typing
2个回答
-1
投票
这将解决所有Pylint错误

from typing import List class Foo(): def __init__(self, _a : int, _b : bool): self.a = _a self.b = _b class Bar(List[Foo]): def print(self): for entry in list(self): print(entry.a) print(entry.b) foo0 = Foo(0, True) foo1 = Foo(1, False) foo2 = Foo(2, True) foo3 = Foo(3, False) bar = Bar([foo0,foo1,foo2,foo3]) bar.print()


-1
投票
正如其他人在评论中指出的那样,List[Foo]不是list类型,而是某些类型检查器(例如mypy)将用来分析代码的

类型提示

© www.soinside.com 2019 - 2024. All rights reserved.