Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error message when both an attr descriptor and a property are defined w/ the same name #30

Closed
cournape opened this issue Oct 20, 2015 · 4 comments

Comments

@cournape
Copy link
Contributor

Not sure that's really fixeable, but it took me a while to detect this bug in my code:

from attr import attributes, attr

@attributes
class Foo(object):
    a = attr()
    @property
    def a(self):
        return "yolo"

Foo("value")

raises

Traceback (most recent call last):
  File "failure.py", line 13, in <module>
    Foo("value")
TypeError: __init__() takes exactly 1 argument (2 given)
@hynek
Copy link
Member

hynek commented Oct 20, 2015

¯\_(ツ)_/¯

No idea what to do save starting an FAQ…

@infinite-Joy
Copy link

class SeriousCoordinates(object):

    def __init__(self, x):
        self.x = x

    @property
    def x(self):
        return "sdkjh"

print(SeriousCoordinates(1))

gives

Traceback (most recent call last):
  File "issue30_001.py", line 10, in <module>
    print(SeriousCoordinates(1))
  File "issue30_001.py", line 4, in __init__
    self.x = x
AttributeError: can't set attribute

Maybe here too we can have something similar.

@mvalkon
Copy link

mvalkon commented Feb 20, 2017

@infinite-Joy this is standard python behavior and not related to attrs at all. The @property decorator is typically used to provide a mechanism for hiding an internal field, and providing a getters and setters to dictate what can and cannot be done with said field. The method name decorated with @property becomes a field name (in your case, SeriousCoordinates.x, which already exists in __init__). Field names generated by the @property-decorator cannot be assigned to directly, which is why an AttributeError is raised.

@hynek
Copy link
Member

hynek commented Apr 25, 2018

fixed in #370

@hynek hynek closed this as completed Apr 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants