Skip to content

attr.ib converter not recognized when using auto_attribs=True #138

Closed

Description

@natemcmaster
  • cattrs version: 1.4.0
  • Python version: 3.9.2
  • Operating System: Linux

Description

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>).

What I Did

import attr
import cattr
from ipaddress import IPv4Address

## Works
@attr.s
class Works:
    ip = attr.ib(converter=IPv4Address)

print(cattr.structure({"ip": "10.0.0.0"}, Works))

## Does not work
@attr.s(auto_attribs=True)
class DoesNotWork:
    ip: IPv4Address = attr.ib(converter=IPv4Address)

try:
    print(cattr.structure({"ip": "10.0.0.0"}, DoesNotWork))
except Exception as e:
    print(e)

try:
    converter = cattr.GenConverter()
    print(converter.structure({"ip": "10.0.0.0"}, DoesNotWork))
except Exception as e:
    print(e)

Traceback:

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=)

converter = cattr.GenConverter()
converter.register_structure_hook(IPv4Address, lambda val, _: IPv4Address(val))

Activity

  1. Tinche commented on Mar 26, 2021

    @Tinche
    Member

    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.

  2. natemcmaster commented on Mar 26, 2021

    @natemcmaster
    ContributorAuthor

    Thanks for the explanation.

    We could skip structuring data for fields with converters defined

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions