Skip to content

Structuring arbitrary types by identity ? #393

Closed
@philpep

Description

@philpep
  • cattrs version: 23.1.2
  • Python version: 3.11.2
  • Operating System: debian bookworm

Description

Hi, I've attrs models with datetime.datetime and datetime.date fields (parsed by pyyaml) which are not supported by base converter.

I wonder if we could have a default structure hook for unknown classes that return given object if its class match expected class ?

What I Did

Minimal example:

import datetime

import cattrs

cattrs.structure(datetime.date.today(), datetime.date)

Gives:

  File "lib/python3.11/site-packages/cattrs/converters.py", line 334, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/site-packages/cattrs/converters.py", line 402, in _structure_error
    raise StructureHandlerNotFoundError(msg, type_=cl)
cattrs.errors.StructureHandlerNotFoundError: Unsupported type: <class 'datetime.date'>. Register a structure hook for it.

Related to #50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions