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
Hi, I had a question about best practices for handling exceptions with attr validators.
Suppose I wanted to represent a float in the unit interval with runtime validation. The natural way to do that, afaict, would be:
importattr@attr.sclassUnitFloat:
"""Represent a float in the unit interval."""x: float=attr.field(
validator=[
attr.validators.instance_of(float),
attr.validators.ge(0.0),
attr.validators.le(1.0),
],
)
If I call with:
UnitFloat(1.1) I get: ValueError: 'x' must be <= 1.0: 1.1, which makes sense.
If I call with UnitFloat(1), however, I get: TypeError: ("'x' must be <class 'float'> (got 1 that is a <class 'int'>)..."
This is in accordance with the python docs (https://docs.python.org/3/library/exceptions.html) but requires the user to guard against two kinds of exceptions, depending on how exactly the constructor choked on bad input. This means that code of the form:
is unsafe. Is there an established convention for this pattern, like maybe:
AttrValidationError = (TypeError, ValueError)?
(There is also the option of adding a converter to each field, which would trivially reduce all bad input to ValueError, but this is probably not always desired behavior.)
Thanks!
(attrs version: '23.1.0')
The text was updated successfully, but these errors were encountered:
This is in accordance with the python docs (https://docs.python.org/3/library/exceptions.html) but requires the user to guard against two kinds of exceptions, depending on how exactly the constructor choked on bad input.
I think this is very much on purpose, such that the user can differentiate between what went wrong.
Generally speaking, attrs doesn't try to be a validation / structuring package (we've got cattrs for that!) but a toolkit to build classes that fit into the Python eco system.
IOW a class that is built using attrs should behave as if someone has written it by hand, adhering to Python standards.
All this to say: there's no attrs-specific guidance to be had, because we're trying to be good Python citizens. But all your suggested approaches are entirely valid, just like with any other Python class.
Uh oh!
There was an error while loading. Please reload this page.
Hi, I had a question about best practices for handling exceptions with attr validators.
Suppose I wanted to represent a float in the unit interval with runtime validation. The natural way to do that, afaict, would be:
If I call with:
UnitFloat(1.1)
I get:ValueError: 'x' must be <= 1.0: 1.1
, which makes sense.If I call with
UnitFloat(1)
, however, I get:TypeError: ("'x' must be <class 'float'> (got 1 that is a <class 'int'>)..."
This is in accordance with the python docs (https://docs.python.org/3/library/exceptions.html) but requires the user to guard against two kinds of exceptions, depending on how exactly the constructor choked on bad input. This means that code of the form:
is unsafe. Is there an established convention for this pattern, like maybe:
AttrValidationError = (TypeError, ValueError)
?(There is also the option of adding a converter to each field, which would trivially reduce all bad input to
ValueError
, but this is probably not always desired behavior.)Thanks!
(attrs version: '23.1.0')
The text was updated successfully, but these errors were encountered: