Closed n-splv closed 9 months ago
Same thing with:
@frozen
class Foo:
text: str = field(validator=validators.instance_of(str))
_bar: Bar = field(init=False)
@_bar.default
def _(self):
return Bar(self.text)
Foo(1)
# Exception: Something terrible happens
Yes, but this is on purpose. It's a trade-off between your use-cases and ppl wanting to valid the whole class once it's done.
That said, if you need more complex validation/conversion, we've got our sibling project cattrs for that: https://github.com/python-attrs/cattrs
Setup
Simple validation, all good:
A more nuanced case: