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
In [45]: A()
Out[45]: A(b=1) # before/after NOT called
In [46]: A(b=2)
Out[46]: A(b=2) # again there is no before/after
In [47]: A().b=3 # they are work **only** when attrib is assigned explicitly
before: b=3
after: b=3
Why on_setattr is ignored on initialization stage? Please explain the logic behind it. It was unexpected for me.
What are before and after here? converters or validators? At least before is called as validator, receiving instance, attrib, new_value, but return value is used further in chain like in converter.
I realize that attrs is not a validator class, but:
converters can't access attributes values (including «current» one). This is pretty common when one attribute depends on another.
validators can't access «original» or «previous in chain» value, leading to incorrect exceptions (because new_value may be converted at this stage)
Is there any way to skip setters chain for default attribute value? Sometimes initial value can be «invalid» from the point of subsequent assignment, i.e. we can have None as initial attribute value, but assigning None to an attribute is invalid
It can be convenient writing/using simple converters/validators that throws the same class-bound Exception in the end, something like:
@define(setter_exception=ErrorA)
class A:
b: int = field(converter=lambda v: int(v), validator=[validators.ge(1), validators(le(3))], setter_error_msg='expected to be ⩾1 and ⩽3')
A().b = 'invalid' throws ErrorA(assigned_value, Attribute(), exception_caught)
class B can be defined with setter_exception=ErrorB. So we can use one error handler for assignments for both classes while having all info to produce useful error message, using «original» invalid value, underlying exception and all Attribute() attributes, including predefined error_msg
I've tried to extend attrs in such a way, but failed.
The text was updated successfully, but these errors were encountered:
At first, thank for the great and inspiring package!
But let me explain my question:
on_setattr
is ignored on initialization stage? Please explain the logic behind it. It was unexpected for me.before
andafter
here? converters or validators? At leastbefore
is called as validator, receivinginstance, attrib, new_value
, but return value is used further in chain like in converter.new_value
may be converted at this stage)A().b = 'invalid' throws ErrorA(assigned_value, Attribute(), exception_caught)
class B can be defined with setter_exception=ErrorB. So we can use one error handler for assignments for both classes while having all info to produce useful error message, using «original» invalid value, underlying exception and all Attribute() attributes, including predefined error_msg
I've tried to extend attrs in such a way, but failed.
The text was updated successfully, but these errors were encountered: