Skip to content

Commit

Permalink
Change transform to type_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
konradhalas committed May 12, 2019
1 parent 7e92063 commit 71f05ab
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dacite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@dataclass
class Config:
transform: Dict[Type, Callable[[Any], Any]] = dc_field(default_factory=dict)
type_hooks: Dict[Type, Callable[[Any], Any]] = dc_field(default_factory=dict)
forward_references: Optional[Dict[str, Any]] = None
check_types: bool = True
2 changes: 1 addition & 1 deletion dacite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None)
try:
field_data = data[field.name]
transformed_value = transform_value(
types_hooks=config.transform, target_type=field.type, value=field_data
type_hooks=config.type_hooks, target_type=field.type, value=field_data
)
value = _build_value(type_=field.type, data=transformed_value, config=config)
except DaciteFieldError as error:
Expand Down
12 changes: 6 additions & 6 deletions dacite/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
T = TypeVar("T", bound=Any)


def transform_value(types_hooks: Dict[Type, Callable[[Any], Any]], target_type: Type, value: Any) -> Any:
if target_type in types_hooks:
value = types_hooks[target_type](value)
def transform_value(type_hooks: Dict[Type, Callable[[Any], Any]], target_type: Type, value: Any) -> Any:
if target_type in type_hooks:
value = type_hooks[target_type](value)
if is_optional(target_type):
if value is None:
return None
target_type = extract_optional(target_type)
return transform_value(types_hooks, target_type, value)
return transform_value(type_hooks, target_type, value)
if is_generic_collection(target_type) and isinstance(value, extract_origin_collection(target_type)):
collection_cls = extract_origin_collection(target_type)
if issubclass(collection_cls, dict):
key_cls, item_cls = extract_generic(target_type)
return collection_cls(
{
transform_value(types_hooks, key_cls, key): transform_value(types_hooks, item_cls, item)
transform_value(type_hooks, key_cls, key): transform_value(type_hooks, item_cls, item)
for key, item in value.items()
}
)
item_cls = extract_generic(target_type)[0]
return collection_cls(transform_value(types_hooks, item_cls, item) for item in value)
return collection_cls(transform_value(type_hooks, item_cls, item) for item in value)
return value


Expand Down
12 changes: 6 additions & 6 deletions tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
from dacite import from_dict, Config, ForwardReferenceError


def test_from_dict_with_transform():
def test_from_dict_with_type_hooks():
@dataclass
class X:
s: str

result = from_dict(X, {"s": "TEST"}, Config(transform={str: str.lower}))
result = from_dict(X, {"s": "TEST"}, Config(type_hooks={str: str.lower}))

assert result == X(s="test")


def test_from_dict_with_transform_and_optional():
def test_from_dict_with_type_hooks_and_optional():
@dataclass
class X:
s: Optional[str]

result = from_dict(X, {"s": "TEST"}, Config(transform={str: str.lower}))
result = from_dict(X, {"s": "TEST"}, Config(type_hooks={str: str.lower}))

assert result == X(s="test")


def test_from_dict_with_transform_and_generic_sequence():
def test_from_dict_with_type_hooks_and_generic_sequence():
@dataclass
class X:
c: List[str]

result = from_dict(X, {"c": ["TEST"]}, config=Config(transform={str: str.lower}))
result = from_dict(X, {"c": ["TEST"]}, config=Config(type_hooks={str: str.lower}))

assert result == X(c=["test"])

Expand Down

0 comments on commit 71f05ab

Please sign in to comment.