Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preconfigured union for a "plain" python data structures #554

Open
salotz opened this issue Jul 13, 2024 · 0 comments
Open

Preconfigured union for a "plain" python data structures #554

salotz opened this issue Jul 13, 2024 · 0 comments

Comments

@salotz
Copy link

salotz commented Jul 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant