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

Attributes of frozen base classes can't be initialized #128

Closed
exarkun opened this issue Jan 3, 2017 · 3 comments
Closed

Attributes of frozen base classes can't be initialized #128

exarkun opened this issue Jan 3, 2017 · 3 comments
Milestone

Comments

@exarkun
Copy link

exarkun commented Jan 3, 2017

Consider

import attr

@attr.s(frozen=True)
class Base(object):
    a = attr.ib()

@attr.s
class Derived(Base):
    b = attr.ib()


Derived("a", "b")

I would expect that this would instantiated aDerived instance and that its a attribute would be immutable and its b attribute would be mutable. Alternatively, perhaps it would be okay for b to be immutable as well (with the idea being that frozen-ness is inherited along with the rest). Not sure about this, but open to considering it.

Instead, the behavior is:

Traceback (most recent call last):
  File "frozenwhat.py", line 12, in <module>
    Derived("a", "b")
  File "<attrs generated init ceb0d1d1d54ce44d01f6dd968d2fb57e3b4d0016>", line 2, in __init__
  File "site-packages/attr/_make.py", line 201, in _frozen_setattrs
    raise FrozenInstanceError()
attr.exceptions.FrozenInstanceError

in other words, Derived instances can't be instantiated at all.

@hynek
Copy link
Member

hynek commented Jan 6, 2017

The problem here is that we currently don’t inherit frozen-ness.

@attr.s(frozen=True)
class Derived(Base):
    b = attr.ib()

works.

@txemi
Copy link

txemi commented Oct 25, 2022

Sorry for commenting on closed issue, but the question is related to this and I did not find other open thread about it and I first preferred not opening a new one for same thing.

I am messed with frozennes and hinheritance; what should be the rules with this for us users? I have simple hierarchical trees (I know avoiding any type of hinheritance is usually recommended in python) and when I freeze some of the classes (base or derived) errors appear anywhere and I finally have to remove frozennes everywhere, even where it could be reasonable.

Thanks,

@hynek
Copy link
Member

hynek commented Nov 21, 2022

unfortunately this question is too vague and general to offer an answer. if you can, please open a question on Stack Overflow with the python-attrs tag and a Short, Self Contained, Correct, Example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants