Skip to content

Commit 0463e37

Browse files
committed
incremented version. modified description and example
1 parent 7010841 commit 0463e37

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.2.0 - 2022/10/25
2+
* [g-pichler] Ability to disable deepcopy on mapping: `use_deepcopy` flag in `map` method.
3+
* Updated doc comments.
4+
15
1.1.3 - 2022/10/07
26
* [g-pichler] Added support for SQLAlchemy models mapping
37
* Upgraded code checking tool and improved code formatting

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# py-automapper
44

55
**Version**
6-
1.1.3
6+
1.2.0
77

88
**Author**
99
anikolaienko
@@ -32,6 +32,7 @@ Table of Contents:
3232
- [Usage](#usage)
3333
- [Different field names](#different-field-names)
3434
- [Overwrite field value in mapping](#overwrite-field-value-in-mapping)
35+
- [Disable Deepcopy](#disable-deepcopy)
3536
- [Extensions](#extensions)
3637
- [Pydantic/FastAPI Support](#pydanticfastapi-support)
3738
- [TortoiseORM Support](#tortoiseorm-support)
@@ -124,20 +125,21 @@ print(vars(public_user_info))
124125
# {'full_name': 'John Cusack', 'profession': 'engineer'}
125126
```
126127

127-
## Use of Deepcopy
128-
By default, automapper performs a recursive deepcopy() on all attributes. This makes sure that changes in the attributes of the source
129-
do not affect the target and vice-versa:
128+
## Disable Deepcopy
129+
By default, py-automapper performs a recursive `copy.deepcopy()` call on all attributes when copying from source object into target class instance.
130+
This makes sure that changes in the attributes of the source do not affect the target and vice versa.
131+
If you need your target and source class share same instances of child objects, set `use_deepcopy=False` in `map` function.
130132

131133
```python
132134
from dataclasses import dataclass
133135
from automapper import mapper
134136

135137
@dataclass
136138
class Address:
137-
street: str
138-
number: int
139-
zip_code: int
140-
city: str
139+
street: str
140+
number: int
141+
zip_code: int
142+
city: str
141143

142144
class PersonInfo:
143145
def __init__(self, name: str, age: int, address: Address):
@@ -149,20 +151,19 @@ class PublicPersonInfo:
149151
def __init__(self, name: str, address: Address):
150152
self.name = name
151153
self.address = address
152-
154+
153155
address = Address(street="Main Street", number=1, zip_code=100001, city='Test City')
154156
info = PersonInfo('John Doe', age=35, address=address)
155157

158+
# default deepcopy behavior
156159
public_info = mapper.to(PublicPersonInfo).map(info)
157-
assert address is not public_info.address
158-
```
160+
print("Target public_info.address is same as source address: ", address is public_info.address)
161+
# Target public_info.address is same as source address: False
159162

160-
To disable this behavior, you may pass `deepcopy=False` to either `mapper.map()` or to `mapper.add()`. If both are passed,
161-
the argument of the `.map()` call has priority. E.g.
162-
163-
```python
164-
public_info = mapper.to(PublicPersonInfo).map(info, deepcopy=False)
165-
assert address is public_info.address
163+
# disable deepcopy
164+
public_info = mapper.to(PublicPersonInfo).map(info, use_deepcopy=False)
165+
print("Target public_info.address is same as source address: ", address is public_info.address)
166+
# Target public_info.address is same as source address: True
166167
```
167168

168169
## Extensions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "py-automapper"
3-
version = "1.1.3"
3+
version = "1.2.0"
44
description = "Library for automatically mapping one object to another"
55
authors = ["Andrii Nikolaienko <anikolaienko14@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)