Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fix pyflakes dealing with @property setter decorator

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:

  • How to call a property of the base class if this property is being overwritten in the derived class?.
  • Python derived class and base class attributes?

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

like image 642
Stephan Avatar asked Sep 24 '12 10:09

Stephan


People also ask

Is @property a decorator?

The @property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class.

What is @property in Python decorator?

The @property Decorator In Python, property() is a built-in function that creates and returns a property object. The syntax of this function is: property(fget=None, fset=None, fdel=None, doc=None) where, fget is function to get value of the attribute. fset is function to set value of the attribute.

What does @property in Python mean?

@property decorator is a built-in decorator in Python which is helpful in defining the properties effortlessly without manually calling the inbuilt function property(). Which is used to return the property attributes of a class from the stated getter, setter and deleter as parameters.

How do you call a property function in Python?

Python property() function. Python property() function returns the object of the property class and it is used to create property of a class. Parameters: fget() – used to get the value of attribute.


2 Answers

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.

like image 108
Martijn Pieters Avatar answered Oct 18 '22 01:10

Martijn Pieters


Various fixes that might be released at some point:

  • http://bazaar.launchpad.net/~menesis/pyflakes/pyflakes-mg/revision/38
  • https://github.com/kevinw/pyflakes/pull/12
  • http://bazaar.launchpad.net/~divmod-dev/divmod.org/trunk/revision/2685

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.

like image 40
ecatmur Avatar answered Oct 18 '22 02:10

ecatmur