Skip to content

frozen=True doesn't play well with converters #76

@Deewiant

Description

@Deewiant

Code that I would expect to run without errors:

import attr

@attr.s(frozen=True)
class Ice:
    x = attr.ib(convert=lambda y: y + 1)

assert Ice(1).x == 2

Actual runtime result:

Traceback (most recent call last):
  File "asdf.py", line 7, in <module>
    assert Ice(1).x == 2
  File "<attrs generated init cd74cad05a4efad0b01528e489d51126583d23cb>", line 4, in __init__
  File "/usr/lib/python3.5/site-packages/attr/_make.py", line 483, in _convert
    setattr(inst, a.name, a.convert(getattr(inst, a.name)))
  File "/usr/lib/python3.5/site-packages/attr/_make.py", line 156, in _frozen_setattrs
    raise FrozenInstanceError()
attr.exceptions.FrozenInstanceError

Clearly _convert isn't aware of the class's frozenness. I took a quick peek at the code and it seems like it should perhaps be using _cached_setattr instead of the ordinary setattr?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions