Skip to content

Use default factory if attribute is None or treat None as missing #570

Closed
@danielnelson

Description

@danielnelson
  • cattrs version: 23.2.3
  • Python version: 3.11.9
  • Operating System: Linux

Description

This is an issue I can workaround but I would like to know if there is a better way to do it.

I have a type that should have a field with a list type and I want it to default to an empty list. I'm converting from a JSON API that sets the field as null if it is not set and I want cattrs to use the default factory for the field if it is None.

I used attrs.converters.default_if_none, though I'm not particularly fond of it. If there is a way in the converter to make this go away I would prefer it.

What I Did

import attrs
import cattrs


@attrs.define
class A:
    x: list[str] = attrs.field(
        converter=attrs.converters.default_if_none(factory=list),
        factory=list,
    )


print(A())

print(A(x=None))

a = cattrs.structure({}, A)
print(a)

a = cattrs.structure({"x": None}, A)
print(a)

Output:

A(x=[])
A(x=[])
A(x=[])
  + Exception Group Traceback (most recent call last):
  |   File "/home/dbn/src/rockfish/cuttlefish/bar.py", line 21, in <module>
  |     a = cattrs.structure({"x": None}, A)
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/dbn/usr/py-3.11/lib/python3.11/site-packages/cattrs/converters.py", line 332, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "<cattrs generated structure __main__.A>", line 10, in structure_A
  | cattrs.errors.ClassValidationError: While structuring A (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<cattrs generated structure __main__.A>", line 6, in structure_A
    |   File "/home/dbn/usr/py-3.11/lib/python3.11/site-packages/cattrs/converters.py", line 519, in _structure_list
    |     for e in obj:
    | TypeError: 'NoneType' object is not iterable
    | Structuring class A @ attribute x
    +------------------------------------

My desired output would be 4 lines of A(x=[]), no exception.

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