Open joshua-alday opened 9 months ago
You're mixing attrs validators and cattrs here. It's not a mistake or anything, but you should be aware attrs validators don't aggregate all errors; they short-circuit on the first one.
You can see this if you try instantiating your model directly:
>>> TestModel(110, "abcdefj")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<attrs generated init a13.TestModel>", line 6, in __init__
File "/Users/tintvrtkovic/pg/cattrs/.venv/lib/python3.12/site-packages/attr/_make.py", line 2927, in __call__
v(inst, attr, value)
File "/Users/tintvrtkovic/pg/cattrs/.venv/lib/python3.12/site-packages/attr/validators.py", line 100, in __call__
raise TypeError(
TypeError: ("'test_data_1' must be <class 'float'> (got 110 that is a <class 'int'>).", Attribute(name='test_data_1', default=NOTHING, validator=_AndValidator(_validators=(<instance_of validator for type <class 'float'>>, <Validator for x < 100>, <function TestModel._check_scale at 0x10c7704a0>)), repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=<class 'float'>, converter=None, kw_only=False, inherited=False, on_setattr=None, alias='test_data_1'), <class 'float'>, 110)
So there's nothing we can do directly here. cattrs doesn't disable attrs validators for various reasons.
There is an effort underway to add a better validation framework to cattrs though, which would probably solve your issue. Right now cattrs will validate the shape of your data (if a field is annotated as an int
, it'll be an int
or bust) but there's no way to validate on the actual values. So that's coming.
@Tinche Okay, this actually makes sense now. I didn't think about attrs being the source of the issue. Thank you for informing me of this. Do we have an idea of when this update will be made? I'm rolling attrs/cattrs into my API project for request validation models. I don't have to heavily use validators at the moment, but it would be good to fully implement them later on.
It's coming in 24.1, I think in 3-6 months. Sorry :/
No need to apologize. I can work around the issue as proper a engineer should haha. This will give me a chance to get better familiarized with exception attributes. With a little bit of intuition I can have something working soon.
I was OK with keeping this open and marked as an enhancement request until we implement this validation, up to you.
Sure, yeah. I just didn't think of it as an immediate issue. But if it helps to mark as a request then let's do that.
Description
cattrs.structure
is seemingly failing to properly aggregate validation failures, even when explicitly settingdetailed_validation=True
on new converter.What I Did
This will result in
['invalid value @ $']
which is unexpected.If I print out the exceptions from
e
:(ValueError("'test_data_1' must be < 100: 110.0"),)
Unless I'm missing something very critical, this seems like a bug.