fix pyflakes dealing with @property setter decorator
Asked Answered
K

3

11

Pyflakes does not deal very well with the following code:

@property
def nodes(self):
    return self._nodes

@nodes.setter
def nodes(self, nodes):
    """
    set the nodes on this object.
    """
    assert nodes != []  # without nodes no route..

    self.node_names = [node.name for node in nodes]
    self._nodes = nodes

Using vim and syntastic which uses pyflakes I get the following error:

    W806 redefinition of function 'nodes' from line 5

So I get warnings about @nodes.setter because I redefine nodes.

How do I disable this useless warning since this code is correct? Or which python checker deals with this code correctly?

Update

I ran into some problems when I refactored my code because properties and functions have different inheritance behavior. Accessing properties of a base class is different. see:

so I now tend to avoid this syntax and use proper functions instead.

Kight answered 24/9, 2012 at 10:48 Comment(0)
U
3

Various fixes that might be released at some point:

The last seems closest to release, as divmod is the parent project for PyFlakes.

Other than patching the package yourself, you could always work around the issue:

@property
def nodes(self):
    return self._nodes

@nodes.setter
def _nodes_setter(self, nodes):    # FIXME: pyflakes
    ...

Unfortunately, this will result in pollution of the class namespace.

Ursula answered 24/9, 2012 at 11:8 Comment(2)
my code breaks if if apply those 'fixes' sinse I use override __setattr__ where the Object.__setattr__ call I make fails if I rename the setters method. It cannot find the nodes method.Kight
couldn't you follow it with del _nodes_setterStronghold
B
5

There is an open pull request on the pyflakes issue tracker that includes a patch for this issue; you could download the patched version from GitHub, or apply the patch manually.

Bathilda answered 24/9, 2012 at 11:2 Comment(0)
U
3

Various fixes that might be released at some point:

The last seems closest to release, as divmod is the parent project for PyFlakes.

Other than patching the package yourself, you could always work around the issue:

@property
def nodes(self):
    return self._nodes

@nodes.setter
def _nodes_setter(self, nodes):    # FIXME: pyflakes
    ...

Unfortunately, this will result in pollution of the class namespace.

Ursula answered 24/9, 2012 at 11:8 Comment(2)
my code breaks if if apply those 'fixes' sinse I use override __setattr__ where the Object.__setattr__ call I make fails if I rename the setters method. It cannot find the nodes method.Kight
couldn't you follow it with del _nodes_setterStronghold
A
0

I ran into this same issue, and to effectively suppress just this specific instance, I added the # NOQA line at the end of the line where I added the decorator. In this case it should look like

@nose.setter  #  NOQA 

And this fixed the issues for me. It's not ideal, but for my needs it was sufficient.

Instead of suppressing all W806 warnings, this is done to catch other instances where it might actually need to be fixed.

Averir answered 15/1, 2013 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.