You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warning in docs about frozen classes no longer applicable #816
However... with attr.define/attr.frozen both take the same amount of time.
In [1]: importattrIn [2]: @attr.frozen
...: classSlottedFrozen:
...: foo: int
...: bar: int
...: baz: str
...: bing: float=5.0
...:
In [3]: @attr.define
...: classSlottedUnfrozen:
...: foo: int
...: bar: int
...: baz: str
...: bing: float=5.0
...:
In [4]: @attr.s(slots=True, frozen=True, auto_attribs=True)
...: classOldSlottedFrozen:
...: foo: int
...: bar: int
...: baz: str
...: bing: float=5.0
...:
In [5]: @attr.s(slots=True, auto_attribs=True)
...: classOldSlottedUnfrozen:
...: foo: int
...: bar: int
...: baz: str
...: bing: float=5.0
...:
In [6]: %timeitSlottedFrozen(7, 42, "hi!")
553ns ± 10.6nsperloop (mean ± std. dev. of7runs, 1000000loopseach)
In [7]: %timeitSlottedUnfrozen(7, 42, "hi!")
566ns ± 17.2nsperloop (mean ± std. dev. of7runs, 1000000loopseach)
In [8]: %timeitOldSlottedFrozen(7, 42, "hi!")
553ns ± 8.64nsperloop (mean ± std. dev. of7runs, 1000000loopseach)
In [9]: %timeitOldSlottedUnfrozen(7, 42, "hi!")
296ns ± 3.09nsperloop (mean ± std. dev. of7runs, 1000000loopseach)
What's up? Well, turns out the new API is setting on_setattr=attr.setters.validate. This turns the __init__ from this: (old)
In [14]: print(inspect.getsource(OldSlottedUnfrozen.__init__))
def__init__(self, foo, bar, baz, bing=attr_dict['bing'].default):
self.foo=fooself.bar=barself.baz=bazself.bing=bing
To this: (new)
In [15]: print(inspect.getsource(SlottedUnfrozen.__init__))
def__init__(self, foo, bar, baz, bing=attr_dict['bing'].default):
_setattr=_cached_setattr.__get__(self, self.__class__)
_setattr('foo', foo)
_setattr('bar', bar)
_setattr('baz', baz)
_setattr('bing', bing)
Possible solutions:
Mark that the note no longer applies
Make a fast path -- only set on_setattr=attr.setters.validate in new API if there is a validator.
Activity
Tinche commentedon May 16, 2021
Ooph, I was not aware of this. A fast path would be great here.
hynek commentedon May 16, 2021
Yeah fast path is definitely the way here. I'm surprised there isn't one – I suspect I just forgot to implement it.
Optimize the case of on_setattr=validate & no validators
Optimize the case of on_setattr=validate & no validators
Optimize the case of on_setattr=validate & no validators
Optimize the case of on_setattr=validate & no validators
Optimize the case of on_setattr=validate & no validators (#817)