Dataclass-mapper

Latest version: v1.9.3

Safety actively analyzes 702088 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 3 of 5

1.7.0

Feature: Allow typing with strings

It not unusual that types are given as strings. E.g.
python
dataclass
class Item:
value: "int"

dataclass
class Order:
items: "list[Item]"

It's also happens automatically when you use `from __future__ import annotations`.

Previously the library didn't understand those types, with 1.7.0 it does.

One warning though, just because strings are now allowed to be used as types, it is still not possible to use cyclic models (e.g. a dataclass that has itself as member in it).

Improvement: Python 3.8 support

Thanks to wbarnha it's now possible to use the library under Python 3.8.

1.6.0

Feature: Provide extra context to mapping

Sometimes you need additional infos for the target object, that you don’t have stored in the source class. With provide_with_extra you can mark fields, so that no mapping is generated, and the field is filled using an extra dictionary that can be given to the map_to function.

python
class TargetItem(BaseModel):
x: int

mapper(TargetItem, {"x": provide_with_extra()})
class SourceItem(BaseModel):
pass

class TargetCollection(BaseModel):
x: int
item: TargetItem
optional_item: Optional[TargetItem]
items: list[TargetItem]

mapper(TargetCollection, {"x": provide_with_extra()})
class SourceCollection(BaseModel):
item: SourceItem
optional_item: Optional[SourceItem]
items: list[SourceItem]

source_collection = SourceCollection(
item=SourceItem(), optional_item=SourceItem(), items=[SourceItem(), SourceItem()]
)

map_to(
source_collection,
TargetCollection,
extra={"x": 1, "item": {"x": 2}, "optional_item": {"x": 3}, "items": [{"x": 4}, {"x": 5}]}
) == TargetCollection(
x=1, item=TargetItem(x=2), optional_item=TargetItem(x=3), items=[TargetItem(x=4), TargetItem(x=5)]
)

1.5.3

Before that release the error message for mapping between e.g. `float | str` to `float | int` was pretty bad (when writing a nicely formatted exception message another exception was raised with a bad message).
With this release, you get a nice message: `'x' of type 'float | str' of 'Foo' cannot be converted to 'x' of type 'float | int'`.

1.5.2

Python 3.10 has a new Union syntax. Instead of `Union[T, S]` you can write `T | S`.
This new syntax was not supported, even for `T | None` (as a shortcut for `Union[T, None]` or `Optional[T]`), and it even crashed when formatting the string for the `TypeError` exception.

With `dataclass-mapper=1.5.2` this is now fixed.
The library now can understand `T | None`, and also doesn't crash any more when formatting exception strings.

1.5.1

1.5.0

Feature: init_with_default()

Same feature as `IGNORE_MISSING_MAPPING`.
Just with a better name, and a function signature.

py
dataclass
class X:
x: int = 5

mapper(X, {"x": init_with_default()})
dataclass
class Y:
pass

assert map_to(Y(), X) == X(5)


The old setting `IGNORE_MISSING_MAPPING` still works, but will give a deprecation warning.

Feature: assume_not_none()

It's possible to tell the library that some fields will have values, even though they have an optional type.
Without specifying `assume_not_none()`, the library would complain that the field might be `None`.

py
dataclass
class Car:
value: int
color: str

mapper(Car, {"value": assume_not_none("price"), "color": assume_not_none()})
dataclass
class SportCar:
price: Optional[int]
color: Optional[str]

assert map_to(SportCar(price=30_000, color="red"), Car) == Car(value=30_000, color="red")


Documentation: explanation of the features

All the important features are now explained with examples in the documentation:

- https://dataclass-mapper.readthedocs.io/en/latest/features.html
- https://dataclass-mapper.readthedocs.io/en/latest/enums.html
- https://dataclass-mapper.readthedocs.io/en/latest/pydantic.html
- https://dataclass-mapper.readthedocs.io/en/latest/type_safety.html

Python 3.11 support

The CI pipelines now also run with 3.11.

Page 3 of 5

© 2025 Safety CLI Cybersecurity Inc. All Rights Reserved.