Search code examples
pythonpylinttyping

Pylint error when inheriting from List[type]


I wrote the following piece of code:

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()

It runs just fine and seems to do what it's supposed to do, but Pylint really doesn't seem to like it (error messages in the comments).

Is there a way to make this stop?


Solution

  • This will fix all Pylint errors

    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()