Conversation
| 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.
[Flake8] line too long (124 > 120 characters) (view)
dataclass_type_validator/__init__.py
Outdated
| 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() |
There was a problem hiding this comment.
[Flake8] line too long (124 > 120 characters) (view)
Added functionality to work with pydantic BaseModel
Update of dataclass_type_validator
| 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.
[Flake8] line too long (124 > 120 characters) (view)
dataclass_type_validator/__init__.py
Outdated
| if err is not None: | ||
| errors[field_name] = err | ||
| if enforce: | ||
| val = field.default if not isinstance(val, (dataclasses._MISSING_TYPE, type(None)) else field.default_factory() |
There was a problem hiding this comment.
[Flake8] line too long (127 > 120 characters) (view)
dataclass_type_validator/__init__.py
Outdated
| return cls | ||
|
|
||
| if __name__ == "__main__": | ||
| #@dataclasses.dataclass |
There was a problem hiding this comment.
[Flake8] block comment should start with '# ' (view)
dataclass_type_validator/__init__.py
Outdated
|
|
||
| if __name__ == "__main__": | ||
| #@dataclasses.dataclass | ||
| #class TestClass: |
There was a problem hiding this comment.
[Flake8] block comment should start with '# ' (view)
dataclass_type_validator/__init__.py
Outdated
| # k: str = "key" | ||
| # v: float = 1.2 | ||
|
|
||
| #test_class = TestClass(k=1.2, v="key") |
There was a problem hiding this comment.
[Flake8] block comment should start with '# ' (view)
dataclass_type_validator/__init__.py
Outdated
|
|
||
| #test_class = TestClass(k=1.2, v="key") | ||
|
|
||
| #@dataclasses.dataclass |
There was a problem hiding this comment.
[Flake8] block comment should start with '# ' (view)
dataclass_type_validator/__init__.py
Outdated
| #test_class = TestClass(k=1.2, v="key") | ||
|
|
||
| #@dataclasses.dataclass | ||
| #class TestClass: |
There was a problem hiding this comment.
[Flake8] block comment should start with '# ' (view)
|
|
||
| #test_class = TestClass(k=1.2, v="key") | ||
| from pydantic import root_validator | ||
| class TestClass(BaseModel): |
There was a problem hiding this comment.
[Flake8] expected 1 blank line, found 0 (view)
dataclass_type_validator/__init__.py
Outdated
| cls_name = f"{cls.__module__}.{cls.__name__}" if cls.__module__ != "__main__" else cls.__name__ | ||
| logger.warning(f"Dataclass type validation failed, types are enforced. {cls_name} errors={repr(errors)})") | ||
|
|
||
| def pydantic_type_validator(cls, values: dict, strict: bool = False, enforce: bool = False): |
There was a problem hiding this comment.
[Flake8] expected 2 blank lines, found 1 (view)
|
|
||
| return cls | ||
|
|
||
| if __name__ == "__main__": |
There was a problem hiding this comment.
[Flake8] expected 2 blank lines after class or function definition, found 1 (view)
dataclass_type_validator/__init__.py
Outdated
| if err is not None: | ||
| errors[field_name] = err | ||
| if enforce: | ||
| val = field.default if not isinstance(field.default, (dataclasses._MISSING_TYPE, type(None))) else field.default_factory() |
There was a problem hiding this comment.
[Flake8] line too long (138 > 120 characters) (view)
dataclass_type_validator/__init__.py
Outdated
| setattr(test_class, "v", "key") | ||
| print(test_class) | ||
| test_class.validate_class() | ||
| print(test_class) No newline at end of file |
There was a problem hiding this comment.
[Flake8] no newline at end of file (view)
| 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.
[Flake8] line too long (124 > 120 characters) (view)
| 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.
[Flake8] line too long (124 > 120 characters) (view)
|
|
||
|
|
||
| def pydantic_type_validator( | ||
| model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False |
There was a problem hiding this comment.
[Flake8] undefined name 'DictStrAny' (view)
|
|
||
|
|
||
| def pydantic_type_validator( | ||
| model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False |
There was a problem hiding this comment.
[Flake8] undefined name 'ModelOrDc' (view)
|
|
||
| def pydantic_type_validator( | ||
| model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False | ||
| ) -> Tuple["DictStrAny", "SetStr", Optional[ValidationError]]: |
There was a problem hiding this comment.
[Flake8] undefined name 'DictStrAny' (view)
|
|
||
| def pydantic_type_validator( | ||
| model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False | ||
| ) -> Tuple["DictStrAny", "SetStr", Optional[ValidationError]]: |
There was a problem hiding this comment.
[Flake8] undefined name 'SetStr' (view)
I had a usecase where I wanted to be able to just enforce the default types, whenever another type was given to the class.
The change is a flag to the main validator classes, where if set to True will set the value to the default value if the validation fails