Search code examples
pythondjangopylint

Pylint complains about the member of an instance that is set in another file


For my tests I created a method in the init.py file that I include in a lot of tests. Besides the fact that it seems to work pylint shows that the instance of the test class has no member floor.

init.py

def create_floor(self):
    self.floor = Floor.objects.create(level=0)
    ...

test_something.py

def setUp(self):
    create_floor(self)
    self.building = Building.objects.create(floor=self.floor)
    ...

Solution

  • You have a valid pylint violation, in my opinion. The normal way to do this would be to define it in a base class and inherit.

    If you just want to shut pylint up, you have to either:

    1. add the relevant ignore/disable comment
    2. customise your pylint.rc file
    3. initialise self.floor = None or something in the test class __init__

    1 litters your code with crap. 2 can hide other problems by suppressing all the warnings of that type. 3 is the exact same kind of code duplication you are trying to avoid by doing this in the first place.

    My recommendation is to just do it the proper OO way and use inheritance.