33# py-automapper
44
55** Version**
6- 1.1.3
6+ 1.2.0
77
88** Author**
99anikolaienko
@@ -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
132134from dataclasses import dataclass
133135from automapper import mapper
134136
135137@dataclass
136138class 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
142144class 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+
153155address = Address(street = " Main Street" , number = 1 , zip_code = 100001 , city = ' Test City' )
154156info = PersonInfo(' John Doe' , age = 35 , address = address)
155157
158+ # default deepcopy behavior
156159public_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
0 commit comments