Skip to content

Conversation

@Hojland
Copy link

@Hojland Hojland commented Oct 12, 2021

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

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__)
Copy link

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)

Rule
E501

You can close this issue if no need to fix it. Learn more.

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()
Copy link

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)

Rule
E501

You can close this issue if no need to fix it. Learn more.

GA-Ramlov and others added 2 commits November 10, 2021 17:34
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__)
Copy link

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)

Rule
E501

You can close this issue if no need to fix it. Learn more.

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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] line too long (127 > 120 characters) (view)

Rule
E501

You can close this issue if no need to fix it. Learn more.

return cls

if __name__ == "__main__":
#@dataclasses.dataclass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] block comment should start with '# ' (view)

Rule
E265

You can close this issue if no need to fix it. Learn more.


if __name__ == "__main__":
#@dataclasses.dataclass
#class TestClass:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] block comment should start with '# ' (view)

Rule
E265

You can close this issue if no need to fix it. Learn more.

# k: str = "key"
# v: float = 1.2

#test_class = TestClass(k=1.2, v="key")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] block comment should start with '# ' (view)

Rule
E265

You can close this issue if no need to fix it. Learn more.


#test_class = TestClass(k=1.2, v="key")

#@dataclasses.dataclass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] block comment should start with '# ' (view)

Rule
E265

You can close this issue if no need to fix it. Learn more.

#test_class = TestClass(k=1.2, v="key")

#@dataclasses.dataclass
#class TestClass:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] block comment should start with '# ' (view)

Rule
E265

You can close this issue if no need to fix it. Learn more.


#test_class = TestClass(k=1.2, v="key")
from pydantic import root_validator
class TestClass(BaseModel):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] expected 1 blank line, found 0 (view)

Rule
E301

You can close this issue if no need to fix it. Learn more.

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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] expected 2 blank lines, found 1 (view)

Rule
E302

You can close this issue if no need to fix it. Learn more.


return cls

if __name__ == "__main__":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] expected 2 blank lines after class or function definition, found 1 (view)

Rule
E305

You can close this issue if no need to fix it. Learn more.

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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] line too long (138 > 120 characters) (view)

Rule
E501

You can close this issue if no need to fix it. Learn more.

setattr(test_class, "v", "key")
print(test_class)
test_class.validate_class()
print(test_class)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] no newline at end of file (view)

Rule
W292

You can close this issue if no need to fix it. Learn more.

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__)
Copy link

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)

Rule
E501

You can close this issue if no need to fix it. Learn more.

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__)
Copy link

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)

Rule
E501

You can close this issue if no need to fix it. Learn more.



def pydantic_type_validator(
model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] undefined name 'DictStrAny' (view)

Rule
F821

You can close this issue if no need to fix it. Learn more.



def pydantic_type_validator(
model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] undefined name 'ModelOrDc' (view)

Rule
F821

You can close this issue if no need to fix it. Learn more.


def pydantic_type_validator(
model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False
) -> Tuple["DictStrAny", "SetStr", Optional[ValidationError]]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] undefined name 'DictStrAny' (view)

Rule
F821

You can close this issue if no need to fix it. Learn more.


def pydantic_type_validator(
model: Type[BaseModel], input_data: "DictStrAny", cls: "ModelOrDc" = None, enforce: bool = False
) -> Tuple["DictStrAny", "SetStr", Optional[ValidationError]]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8] undefined name 'SetStr' (view)

Rule
F821

You can close this issue if no need to fix it. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants