Skip to content

Commit

Permalink
Merge 451cb9d into ff73ec3
Browse files Browse the repository at this point in the history
  • Loading branch information
bpeake-illuscio committed Jan 30, 2019
2 parents ff73ec3 + 451cb9d commit c3e61ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.pyc
dist/
*.egg-info/
build/
build/
.idea/
7 changes: 6 additions & 1 deletion dacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Config:
transform: Dict[str, Callable[[Any], Any]] = dc_field(default_factory=dict)
flattened: List[str] = dc_field(default_factory=list)
forward_references: Optional[Dict[str, Any]] = None
validate_types: bool = True


T = TypeVar('T')
Expand All @@ -65,6 +66,7 @@ def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None)
config = config or Config()
init_values: Data = {}
post_init_values: Data = {}

_validate_config(data_class, data, config)
try:
data_class_hints = get_type_hints(data_class, globalns=config.forward_references)
Expand Down Expand Up @@ -100,7 +102,7 @@ def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None)
)
if field.name in config.cast:
value = _cast_value(field.type, value)
if not _is_instance(field.type, value):
if config.validate_types and not _is_instance(field.type, value):
raise WrongTypeError(field, value)
if field.init:
init_values[field.name] = value
Expand Down Expand Up @@ -206,6 +208,7 @@ def _make_inner_config(field: Field, config: Config) -> Config:
cast=_extract_nested_list(field, config.cast),
transform=_extract_nested_dict(field, config.transform),
flattened=_extract_nested_list(field, config.flattened),
validate_types=config.validate_types
)


Expand Down Expand Up @@ -264,6 +267,8 @@ def _inner_from_dict_for_union(data: Any, field: Field, outer_config: Config) ->
return data
except DaciteError:
pass
if not outer_config.validate_types:
return data
raise UnionMatchError(field)


Expand Down
42 changes: 42 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,45 @@ class Y:

with pytest.raises(ForwardReferenceError):
from_dict(X, {"y": {"s": "text"}})


def test_validation_false_passes():
@dataclass
class X:
s: int

result = from_dict(X, {'s': 'text'}, config=Config(validate_types=False))
assert result == X("text")


def test_validation_false_missing_data_raises():
@dataclass
class X:
s: int

with pytest.raises(MissingValueError):
from_dict(X, dict(), config=Config(validate_types=False))


def test_validation_false_passes_union():
@dataclass
class X:
s: Union[int, float]

result = from_dict(X, {'s': "text"}, config=Config(validate_types=False))
assert result == X("text")


def test_validation_false_passes_nested():
@dataclass
class X:
s: int
t: Union[int, float]

@dataclass
class Y:
x: X

result = from_dict(Y, {'x': {'s': 'text1', 't': 'text2'}}, config=Config(validate_types=False))
assert result == Y(X('text1', 'text2'))

0 comments on commit c3e61ad

Please sign in to comment.