-
Notifications
You must be signed in to change notification settings - Fork 12
Enforce default at validation error #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
20530e5
24990d9
93f69cf
a78fdb0
b16913b
5155f90
8c3cef0
1f22e71
66a3a8e
752ebc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,14 @@ | ||||||||
| import dataclasses | ||||||||
| import typing | ||||||||
| import functools | ||||||||
| from typing import Any | ||||||||
| from typing import Optional | ||||||||
| import logging | ||||||||
| import typing | ||||||||
| from typing import Any, Optional | ||||||||
|
|
||||||||
| logger = logging.getLogger(__name__) | ||||||||
|
|
||||||||
|
|
||||||||
| class TypeValidationError(Exception): | ||||||||
| """Exception raised on type validation errors. | ||||||||
| """ | ||||||||
| """Exception raised on type validation errors.""" | ||||||||
|
|
||||||||
| def __init__(self, *args, target: dataclasses.dataclass, errors: dict): | ||||||||
| super(TypeValidationError, self).__init__(*args) | ||||||||
|
|
@@ -16,59 +17,59 @@ def __init__(self, *args, target: dataclasses.dataclass, errors: dict): | |||||||
|
|
||||||||
| def __repr__(self): | ||||||||
| cls = self.class_ | ||||||||
| cls_name = ( | ||||||||
| f"{cls.__module__}.{cls.__name__}" | ||||||||
| if cls.__module__ != "__main__" | ||||||||
| else cls.__name__ | ||||||||
| ) | ||||||||
| cls_name = f"{cls.__module__}.{cls.__name__}" if cls.__module__ != "__main__" else cls.__name__ | ||||||||
| attrs = ", ".join([repr(v) for v in self.args]) | ||||||||
| return f"{cls_name}({attrs}, errors={repr(self.errors)})" | ||||||||
|
|
||||||||
| def __str__(self): | ||||||||
| cls = self.class_ | ||||||||
| cls_name = ( | ||||||||
| f"{cls.__module__}.{cls.__name__}" | ||||||||
| if cls.__module__ != "__main__" | ||||||||
| else cls.__name__ | ||||||||
| ) | ||||||||
| cls_name = f"{cls.__module__}.{cls.__name__}" if cls.__module__ != "__main__" else cls.__name__ | ||||||||
| s = cls_name | ||||||||
| return f"{s} (errors = {self.errors})" | ||||||||
|
|
||||||||
|
|
||||||||
| class EnforceError(Exception): | ||||||||
| """Exception raised on enforcing validation errors.""" | ||||||||
|
|
||||||||
| def __init__(self, *args): | ||||||||
| super(EnforceError, self).__init__(*args) | ||||||||
| pass | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_type(expected_type: type, value: Any) -> Optional[str]: | ||||||||
| if not isinstance(value, expected_type): | ||||||||
| return f'must be an instance of {expected_type}, but received {type(value)}' | ||||||||
| return f"must be an instance of {expected_type}, but received {type(value)}" | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_iterable_items(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| expected_item_type = expected_type.__args__[0] | ||||||||
| errors = [_validate_types(expected_type=expected_item_type, value=v, strict=strict) for v in value] | ||||||||
| errors = [x for x in errors if x] | ||||||||
| if len(errors) > 0: | ||||||||
| return f'must be an instance of {expected_type}, but there are some errors: {errors}' | ||||||||
| return f"must be an instance of {expected_type}, but there are some errors: {errors}" | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_list(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| if not isinstance(value, list): | ||||||||
| return f'must be an instance of list, but received {type(value)}' | ||||||||
| return f"must be an instance of list, but received {type(value)}" | ||||||||
| return _validate_iterable_items(expected_type, value, strict) | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_tuple(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| if not isinstance(value, tuple): | ||||||||
| return f'must be an instance of tuple, but received {type(value)}' | ||||||||
| return f"must be an instance of tuple, but received {type(value)}" | ||||||||
| return _validate_iterable_items(expected_type, value, strict) | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_frozenset(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| if not isinstance(value, frozenset): | ||||||||
| return f'must be an instance of frozenset, but received {type(value)}' | ||||||||
| return f"must be an instance of frozenset, but received {type(value)}" | ||||||||
| return _validate_iterable_items(expected_type, value, strict) | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_dict(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| if not isinstance(value, dict): | ||||||||
| return f'must be an instance of dict, but received {type(value)}' | ||||||||
| return f"must be an instance of dict, but received {type(value)}" | ||||||||
|
|
||||||||
| expected_key_type = expected_type.__args__[0] | ||||||||
| expected_value_type = expected_type.__args__[1] | ||||||||
|
|
@@ -80,18 +81,20 @@ def _validate_typing_dict(expected_type: type, value: Any, strict: bool) -> Opti | |||||||
| val_errors = [v for v in val_errors if v] | ||||||||
|
|
||||||||
| if len(key_errors) > 0 and len(val_errors) > 0: | ||||||||
| return f'must be an instance of {expected_type}, but there are some errors in keys and values. '\ | ||||||||
| f'key errors: {key_errors}, value errors: {val_errors}' | ||||||||
| return ( | ||||||||
| f"must be an instance of {expected_type}, but there are some errors in keys and values. " | ||||||||
| f"key errors: {key_errors}, value errors: {val_errors}" | ||||||||
| ) | ||||||||
| elif len(key_errors) > 0: | ||||||||
| return f'must be an instance of {expected_type}, but there are some errors in keys: {key_errors}' | ||||||||
| return f"must be an instance of {expected_type}, but there are some errors in keys: {key_errors}" | ||||||||
| elif len(val_errors) > 0: | ||||||||
| return f'must be an instance of {expected_type}, but there are some errors in values: {val_errors}' | ||||||||
| return f"must be an instance of {expected_type}, but there are some errors in values: {val_errors}" | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_callable(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
| _ = strict | ||||||||
| if not isinstance(value, type(lambda a: a)): | ||||||||
| return f'must be an instance of {expected_type._name}, but received {type(value)}' | ||||||||
| return f"must be an instance of {expected_type._name}, but received {type(value)}" | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_typing_literal(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
|
|
@@ -101,11 +104,11 @@ def _validate_typing_literal(expected_type: type, value: Any, strict: bool) -> O | |||||||
|
|
||||||||
|
|
||||||||
| _validate_typing_mappings = { | ||||||||
| 'List': _validate_typing_list, | ||||||||
| 'Tuple': _validate_typing_tuple, | ||||||||
| 'FrozenSet': _validate_typing_frozenset, | ||||||||
| 'Dict': _validate_typing_dict, | ||||||||
| 'Callable': _validate_typing_callable, | ||||||||
| "List": _validate_typing_list, | ||||||||
| "Tuple": _validate_typing_tuple, | ||||||||
| "FrozenSet": _validate_typing_frozenset, | ||||||||
| "Dict": _validate_typing_dict, | ||||||||
| "Callable": _validate_typing_callable, | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
|
|
@@ -114,18 +117,17 @@ def _validate_sequential_types(expected_type: type, value: Any, strict: bool) -> | |||||||
| if validate_func is not None: | ||||||||
| return validate_func(expected_type, value, strict) | ||||||||
|
|
||||||||
| if str(expected_type).startswith('typing.Literal'): | ||||||||
| if str(expected_type).startswith("typing.Literal"): | ||||||||
| return _validate_typing_literal(expected_type, value, strict) | ||||||||
|
|
||||||||
| if str(expected_type).startswith('typing.Union'): | ||||||||
| is_valid = any(_validate_types(expected_type=t, value=value, strict=strict) is None | ||||||||
| for t in expected_type.__args__) | ||||||||
| if str(expected_type).startswith("typing.Union"): | ||||||||
| is_valid = any(_validate_types(expected_type=t, value=value, strict=strict) is None for t in expected_type.__args__) | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Flake8] line too long (124 > 120 characters) (view)
You can close this issue if no need to fix it. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Flake8] line too long (124 > 120 characters) (view)
You can close this issue if no need to fix it. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Flake8] line too long (124 > 120 characters) (view)
You can close this issue if no need to fix it. Learn more. |
||||||||
| if not is_valid: | ||||||||
| return f'must be an instance of {expected_type}, but received {value}' | ||||||||
| return f"must be an instance of {expected_type}, but received {value}" | ||||||||
| return | ||||||||
|
|
||||||||
| if strict: | ||||||||
| raise RuntimeError(f'Unknown type of {expected_type} (_name = {expected_type._name})') | ||||||||
| raise RuntimeError(f"Unknown type of {expected_type} (_name = {expected_type._name})") | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_types(expected_type: type, value: Any, strict: bool) -> Optional[str]: | ||||||||
|
|
@@ -136,7 +138,7 @@ def _validate_types(expected_type: type, value: Any, strict: bool) -> Optional[s | |||||||
| return _validate_sequential_types(expected_type=expected_type, value=value, strict=strict) | ||||||||
|
|
||||||||
|
|
||||||||
| def dataclass_type_validator(target, strict: bool = False): | ||||||||
| def dataclass_type_validator(target, strict: bool = False, enforce: bool = False): | ||||||||
| fields = dataclasses.fields(target) | ||||||||
|
|
||||||||
| errors = {} | ||||||||
|
|
@@ -148,14 +150,22 @@ def dataclass_type_validator(target, strict: bool = False): | |||||||
| err = _validate_types(expected_type=expected_type, value=value, strict=strict) | ||||||||
| if err is not None: | ||||||||
| errors[field_name] = err | ||||||||
| if enforce: | ||||||||
| val = field.default if not isinstance(field.default, dataclasses._MISSING_TYPE) else field.default_factory() | ||||||||
|
||||||||
| Rule |
|---|
E501 |
You can close this issue if no need to fix it. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Flake8] line too long (124 > 120 characters) (view)
E501You can close this issue if no need to fix it. Learn more.