Preconfigured union for a "plain" python data structures

Open
#554 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
data

Research direction

Review the existing preconfigured converters and PythonConverter configuration to identify the entry point for adding another preset. Use the proposed plain Python passthrough types and collection overrides as the acceptance criteria, then verify that the new converter preserves those values and collections during conversion.

Written by the indexing model from the issue text.

Description

There are preconfigured converters for the various serialization formats, but I have found need for a converter specific to what I will call "plain" Python.

Basically:

PassThroughTypes: TypeAlias = str | bool | int | float | bytes | None

def python_make_converter(
    *args: Any,
    **kwargs: Any,
) -> PythonConverter:

    # configure specific overrides for this converter
    kwargs["unstruct_collection_overrides"] = {
        # rich built-in types are maintained, rather than all dumped
        # into lists
        Sequence: tuple,
        MutableSequence: list,
        Set: frozenset,
        MutableSet: set,
        Counter: lambda c: Counter(**{k: v for k, v in c}),
    }

    converter = PythonConverter(*args, **kwargs)

    # passthrough union types
    configure_union_passthrough(PassThroughTypes, converter)

    return converter

Just some context for what I am doing that warranted making this concrete versus the more plain cattrs.global_converter. I am using the converter to generate "patches" that all happens within Python and the above to be very useful.

I would propose that a new preconfigured converter be made similar to the others. But I can understand it not being high priority, in which case this issue may help someone else :)

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/cattrs

All issues in python-attrs/cattrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.