
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
|pypi| |support| |licence| |readthedocs| |build| |coverage|
.. |pypi| image:: https://img.shields.io/pypi/v/dataclass-mapper.svg?style=flat-square :target: https://pypi.org/project/dataclass-mapper/ :alt: pypi version
.. |support| image:: https://img.shields.io/pypi/pyversions/dataclass-mapper.svg?style=flat-square :target: https://pypi.org/project/dataclass-mapper/ :alt: supported Python version
.. |build| image:: https://github.com/dataclass-mapper/dataclass-mapper/actions/workflows/test.yml/badge.svg :target: https://github.com/dataclass-mapper/dataclass-mapper/actions :alt: build status
.. |coverage| image:: https://codecov.io/gh/dataclass-mapper/dataclass-mapper/branch/main/graphs/badge.svg?branch=main :target: https://codecov.io/gh/dataclass-mapper/dataclass-mapper?branch=main :alt: Code coverage
.. |licence| image:: https://img.shields.io/pypi/l/dataclass-mapper.svg?style=flat-square :target: https://pypi.org/project/dataclass-mapper/ :alt: licence
.. |readthedocs| image:: https://img.shields.io/readthedocs/dataclass-mapper/latest.svg?style=flat-square&label=Read%20the%20Docs :alt: Read the documentation at https://dataclass-mapper.readthedocs.io/en/latest/ :target: https://dataclass-mapper.readthedocs.io/en/latest/
Writing mapper methods between two similar dataclasses is boring, need to be actively maintained and are error-prone. Much better to let this library auto-generate them for you.
The focus of this library is:
Concise and easy syntax:
Safety:
Performance:
A couple of example usecases, that show why this library might be useful.
POST
and a GET
endpoint.
Both models (POST
request body model and GET
response body model) are almost the same, but there are some minor differences.
E.g. response model has an additional id
parameter.
You need a way of mapping the request model to a response model.dataclass-mapper
can be installed using:
.. code-block:: bash
pip install dataclass-mapper
pip install 'dataclass-mapper[pydantic]'
We have the following target data structure, a class called Person
.
.. code-block:: python
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 function by hand, you can let it autogenerate one using this library:
.. code-block:: python
from dataclass_mapper import map_to, mapper
@mapper(Person, {"second_name": "surname"}) ... @dataclass ... class ContactInfo: ... first_name: str ... surname: str ... age: int
contact = ContactInfo(first_name="Henry", surname="Kaye", age=42) map_to(contact, Person) Person(first_name='Henry', second_name='Kaye', age=42)
The dataclass-mapper
library autogenerated 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.
The current version has support for:
dataclass
(with recursive models, custom initializers, optional types, extra-context, ...): see Supported features <https://dataclass-mapper.readthedocs.io/en/latest/features.html>
_ for the full list and examplesEnum mappings <https://dataclass-mapper.readthedocs.io/en/latest/enums.html>
_Pydantic support <https://dataclass-mapper.readthedocs.io/en/latest/pydantic.html>
_Type safety <https://dataclass-mapper.readthedocs.io/en/latest/type_safety.html>
_See CONTRIBUTING.rst <https://github.com/dataclass-mapper/dataclass-mapper/blob/main/CONTRIBUTING.rst>
_.
The project is released under the MIT license <https://github.com/dataclass-mapper/dataclass-mapper/blob/main/LICENSE.md>
_.
FAQs
Autogenerate mappings between dataclasses
We found that dataclass-mapper demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.