attrs icon indicating copy to clipboard operation
attrs copied to clipboard

attr.s(frozen=True) breaks writeable properties

Open exarkun opened this issue 8 years ago • 5 comments

(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$ cat brokenattrs.py
import attr

@attr.s(frozen=True)
class X(object):
    y = attr.ib()

    @property
    def z(self):
        return 3

    @z.setter
    def z(self, value):
        print("z=", value)

X(y=None).z = 10
(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$ python brokenattrs.py 
Traceback (most recent call last):
  File "brokenattrs.py", line 15, in <module>
    X(y=None).z = 10
  File "/home/exarkun/Environments/txkube/local/lib/python2.7/site-packages/attr/_make.py", line 201, in _frozen_setattrs
    raise FrozenInstanceError()
attr.exceptions.FrozenInstanceError
(txkube) exarkun@baryon:~/Work/LeastAuthority/txkube$ 

exarkun avatar Feb 10 '17 11:02 exarkun

Heh we freeze the classes for good ATM. There seems to be interest in a more…flexible freezing process (c.f. also #133).

hynek avatar Feb 10 '17 11:02 hynek

I opened an issue for with initial pull request for customizing setattr and delattr for frozen classes here: https://github.com/python-attrs/attrs/issues/378

joeblackwaslike avatar May 07 '18 14:05 joeblackwaslike

I'm having trouble understanding the solution to this. Is this still not supported or am I missing something? What I ultimately want to do is be able to set a single attribute of a class that has inherited a frozen status. Is this even possible?

maz808 avatar Jan 14 '22 03:01 maz808

you can always set any attribute using object.__setattr__. An example is in https://www.attrs.org/en/stable/init.html#post-init

hynek avatar Jan 21 '22 08:01 hynek

you can always set any attribute using object.__setattr__. An example is in https://www.attrs.org/en/stable/init.html#post-init

I have started doing that in a method for the frozen class, but I really would prefer this: class_instance.attribute = value over this: class_instance.change_attribute(value)

I guess I should have mentioned that. I appreciate the response, but I'm still uncertain if what I want is even doable right now.

maz808 avatar Jan 21 '22 16:01 maz808