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
{{ message }}
Repository navigation
attr.ib converter not recognized when using auto_attribs=True #138
It looks like using auto_attribs=True and type annotations can break the code which otherwise invokes a converter specified via attr.ib(converter=<something>).
Traceback (most recent call last):
File "/home/nate/workspace/test.py", line 17, in <module>
print(cattr.structure({"a": "10.0.0.0"}, A))
File "/home/nate/workspace/.venv/lib/python3.9/site-packages/cattr/converters.py", line 205, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "/home/nate/workspace/.venv/lib/python3.9/site-packages/cattr/converters.py", line 342, in structure_attrs_fromdict
dispatch(type_)(val, type_) if type_ is not None else val
File "/home/nate/workspace/.venv/lib/python3.9/site-packages/cattr/converters.py", line 286, in _structure_default
raise ValueError(msg)
ValueError: Unsupported type: <class 'ipaddress.IPv4Address'>. Register a structure hook for it.
Workaround
Manually register converters instead of using attr.ib(converter=)
Yep, cattrs doesn't integrate with converters, and IPv4Address is neither an attrs class nor a dataclass so it doesn't know how to structure it without help. The cattrs way would be to skip the converter and only register the structure hook, and in the actual code always pass in an actual instance of IPv4Address instead.
Converters are difficult to really support properly in this context, even Mypy doesn't really have good support for them. Consider the following snippet, which passes Mypy but blows up in runtime:
import attr
from ipaddress import IPv4Address
@attr.s(auto_attribs=True)
class DoesNotWork:
ip: IPv4Address = attr.ib(converter=IPv4Address)
DoesNotWork([])
That said, I'm willing to improve the situation if we can come up with some good ideas.
The most correct would be getting the type signature from the converter (i.e. IPv4Address is essentially a callable accepting a Union[int, str, IPv4Address], so cattrs could pretend the type of the field, for structuring purposes, is Union[int, str, IPv4Address]), but in reality this information is not available since IPv4Address is not type annotated, any a large amount of classes in the stdlib are like this.
We could skip structuring data for fields with converters defined, but this assumes people always use converters for structuring data. I guess it could be a flag on the converter that you could opt-in.
Activity
Tinche commentedon Mar 26, 2021
Yep, cattrs doesn't integrate with converters, and IPv4Address is neither an attrs class nor a dataclass so it doesn't know how to structure it without help. The cattrs way would be to skip the converter and only register the structure hook, and in the actual code always pass in an actual instance of IPv4Address instead.
Converters are difficult to really support properly in this context, even Mypy doesn't really have good support for them. Consider the following snippet, which passes Mypy but blows up in runtime:
That said, I'm willing to improve the situation if we can come up with some good ideas.
The most correct would be getting the type signature from the converter (i.e. IPv4Address is essentially a callable accepting a
Union[int, str, IPv4Address], so cattrs could pretend the type of the field, for structuring purposes, isUnion[int, str, IPv4Address]), but in reality this information is not available since IPv4Address is not type annotated, any a large amount of classes in the stdlib are like this.We could skip structuring data for fields with converters defined, but this assumes people always use converters for structuring data. I guess it could be a flag on the converter that you could opt-in.
natemcmaster commentedon Mar 26, 2021
Thanks for the explanation.
This seems like the simplest approach, and also seems like the least surprising behavior. It is what I was expecting. If this is okay with you, here is an implementation of that: https://github.com/Tinche/cattrs/pull/139
That said, I realize I'm new here so let me know if I'm missing something.
chore(deps): update cattrs requirement from <1.10,>=1.8 to >=1.8,<1.1…