Autogenerate mappings between dataclasses
Project description
Writing mapper methods between two similar dataclasses is boring, need to be actively maintained and are error-prone. Much better to let a library auto-generate them for you.
This library makes it easy to autogenerate mappers, makes sure that the types between source and target class match, and that all fields of the target class are actually mapped to. Most of those checks are already done at class definition time, not when the mappings are run. It supports Python’s dataclasses and also Pydantic models, and can also map between those two.
Installation
dataclass-mapper can be installed using:
pip install dataclass-mapper
# or for Pydantic support
pip install dataclass-mapper[pydantic]
Small Example
We have the following target data structure, a class called Person.
from dataclasses import dataclass
@dataclass
class Person:
first_name: str
second_name: str
age: int
We want to have a mapper from the source data structure, a class called ContactInfo. Notice that the attribute second_name of Person is called surname in ContactInfo. Other than that, all the attribute names are the same.
Instead of writing a mapper to_Person by hand:
@dataclass
class ContactInfo:
first_name: str
surname: str
age: int
def to_Person(self) -> Person:
return Person(
first_name=self.first_name,
second_name=self.surname,
age=self.age,
)
person = some_contact.to_Person()
you can let the mapper autogenerate with:
from dataclass_mapper import map_to, mapper
@mapper(Person, {"second_name": "surname"})
@dataclass
class ContactInfo:
first_name: str
surname: str
age: int
person = map_to(some_contact, Person)
The dataclass-mapper library autogenerated some a mapper, that can be used with the map_to function. All we had to specify was the name of the target class, and optionally specify which fields map to which other fields. Notice that we only had to specify that the second_name field has to be mapped to surname, all other fields were mapped automatically because the field names didn’t change.
And the dataclass-mapper library will perform a lot of checks around this mapping. It will check if the data types match, if some fields would be left uninitialized, etc.
Features
The current version has support for:
Python’s dataclass
pydantic classes
Checks if all target fields are actually initialized. Raises a ValueError at class definition time when the type is different.
Checks if the type on the target field is the same as the source field. Raises a TypeError at class definition time when the type is different.
Recursive dataclasses
IGNORE_MISSING_MAPPING for values that you don’t wanna set but have a default value/factory.
Optional types (mapping from an non-optional to an optional field, or to an optional field with default values/fields). Raises a TypeError at class definition time when an optional type is mapped to a non-optional type.
List types
Mapper in both direction with mapper and mapper_from.
Assign Values with lambdas (e.g. {"x": lambda: 42})
Custom mapping computations with with lambdas (e.g. {"x": lambda self: self.x + 1})
For Optional fields in Pydantic classes, only set those target fields that actually set in the source (__fields_set__).
Use Pydantic’s .construct method if no validators are used (can give an up to 30x boost)
Pydantic’s field aliases (including the allow_population_by_field_name configuration)
Mappings between enums in both direction with enum_mapper and enum_mapper_from
Still missing features:
Union types
Dict types
Checking if all source attributes were used
SQLAlchemy ORM / attr
License
The project is released under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dataclass_mapper-1.4.0.tar.gz
.
File metadata
- Download URL: dataclass_mapper-1.4.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6f68e7b088e19da18d172c997778b8a0bbf572aba66ea1a30069fddb4bd7e16 |
|
MD5 | 3d107d44f611b42754d271e5d4ff5c1f |
|
BLAKE2b-256 | 42e384f8efdea7a15988d770fdeaa337a40d8f985f5095237b97a66d274d2d88 |
File details
Details for the file dataclass_mapper-1.4.0-py3-none-any.whl
.
File metadata
- Download URL: dataclass_mapper-1.4.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77ea91072654c995811177bfd2f51aff0034a80617795575e84e350295ea9915 |
|
MD5 | e3aca7bb2d55bf75d252060e21ccc9b4 |
|
BLAKE2b-256 | e2c1ba2d9fc7738b4b9f2c19df6cd815643d1a05c9295747556ff7bd6d22a896 |