While 37d38c4 is pretty cool, when working on it it was very obvious to me, that it should be united with converter. After @Tinche pointed it out too, there’s no way back.
IOW, something like this:
@attr.s
class C:
x = attr.ib()
@x.callback_but_a_better_name:
def take_number_make_it_a_hex_string(self, attribute, value):
if not isinstance(value, int):
raise TypeError(f"{attribute.name} must be an int")
return hex(value)
Any other ideas?
The downside that it breaks the simplicity of the current implementation and we’d have to treat those callbacks in a special way (instead of just like any other validator).
Thinking of it, it might make sense to make it a different feature altogether (i.e. having a lightweight validator and something more substantial) because requiring to return the new value is a source for errors…in any case it mustn’t be called validator because the semantics would be different.
I’d tend to say that people can just do a self.x = hex(value) but that breaks frozen classes and we have that problem already in #120.
While 37d38c4 is pretty cool, when working on it it was very obvious to me, that it should be united with converter. After @Tinche pointed it out too, there’s no way back.
IOW, something like this:
Any other ideas?
The downside that it breaks the simplicity of the current implementation and we’d have to treat those callbacks in a special way (instead of just like any other validator).
Thinking of it, it might make sense to make it a different feature altogether (i.e. having a lightweight validator and something more substantial) because requiring to return the new value is a source for errors…in any case it mustn’t be called validator because the semantics would be different.
I’d tend to say that people can just do a
self.x = hex(value)but that breaks frozen classes and we have that problem already in #120.