Skip to content

Commit aaa8d51

Browse files
authored
Update object_mapper.py
1 parent 9d62e9b commit aaa8d51

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

mapper/object_mapper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Copyright (C) 2015, marazt. All rights reserved.
44
"""
5-
from inspect import getmembers, isroutine
5+
from inspect import getmembers, isroutine, signature
66
from datetime import date, datetime
77

88
from mapper.casedict import CaseDict
@@ -166,10 +166,6 @@ def map(self, from_obj, to_type=type(None), ignore_case=False, allow_none=False,
166166
.format(key_from.__module__, key_from.__name__, to_type.__module__, to_type.__name__))
167167
key_to = to_type
168168
custom_mappings = self.mappings[key_from][key_to][1]
169-
170-
# Currently, all target class data members need to have default value
171-
# Object with __init__ that carries required non-default arguments are not supported
172-
inst = key_to()
173169

174170
def not_private(s):
175171
return not s.startswith('_')
@@ -183,6 +179,12 @@ def is_included(s, mapping):
183179
from_obj_attributes = getmembers(from_obj, lambda a: not isroutine(a))
184180
from_obj_dict = {k: v for k, v in from_obj_attributes}
185181

182+
# support __init__ by passing arguments by keyword when instantiating the key_to
183+
sig = signature(key_to.__init__)
184+
kwargs = {x: from_obj_dict[x] for x in sig.parameters if x not in ('self', 'kwargs')}
185+
186+
inst = key_to(**kwargs)
187+
186188
to_obj_attributes = getmembers(inst, lambda a: not isroutine(a))
187189
to_obj_dict = {k: v for k, v in to_obj_attributes if not_excluded(k) and (not_private(k) or is_included(k, custom_mappings))}
188190

0 commit comments

Comments
 (0)