Skip to content

Allow overriding unstruct_collection_overrides in preconf make_converter #350

@ericbn

Description

@ericbn
  • cattrs version: 22.2.0
  • Python version: 3.9.16
  • Operating System: macOS

Description

What I was trying to do:

from collections.abc import Set

from cattrs.preconf.json import make_converter as json_make_converter

json_converter = json_make_converter(unstruct_collection_overrides={Set: sorted})

This didn't work because cattrs.preconf.json.make_converter overrides the kwargs:

kwargs["unstruct_collection_overrides"] = {
**kwargs.get("unstruct_collection_overrides", {}),
AbstractSet: list,
Counter: dict,
}

Then I tried:

from collections.abc import Set

from cattrs.preconf.json import make_converter as json_make_converter

json_converter = json_make_converter().copy(unstruct_collection_overrides={Set: sorted})

But later in the code I call json_converter.dumps and mypy complained error: "Converter" has no attribute "dumps" [attr-defined] because copy returns a Converter.

What I Did

from collections.abc import Set

from cattrs.preconf.json import JsonConverter
from cattrs.preconf.json import configure_converter as json_configure_converter

json_converter = JsonConverter(unstruct_collection_overrides={Set: sorted})
json_configure_converter(json_converter)

Maybe the two forms that I've tried above should also be allowed?

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