From d793def05c9e81fd816eeb5af6cf9ef388c80ec2 Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins <90264926+AndrewRiggs-Atkins@users.noreply.github.com> Date: Mon, 17 Jan 2022 17:30:56 +0000 Subject: [PATCH 1/6] Addition of optional validate kwarg for validating object on instantiation even if not a table. --- sqlmodel/main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 4d6d2f2712..4d7f55716c 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -303,6 +303,14 @@ def get_config(name: str) -> Any: # other future tools based on Pydantic can use. new_cls.__config__.read_with_orm_mode = True + config_validate = get_config("validate") + if config_validate is True: + # If it was passed by kwargs, ensure it's also set in config + new_cls.__config__.validate = config_validate + for k, v in new_cls.__fields__.items(): + col = get_column_from_field(v) + setattr(new_cls, k, col) + config_registry = get_config("registry") if config_registry is not Undefined: config_registry = cast(registry, config_registry) @@ -498,10 +506,12 @@ def __init__(__pydantic_self__, **data: Any) -> None: ) # Only raise errors if not a SQLModel model if ( - not getattr(__pydantic_self__.__config__, "table", False) + (not getattr(__pydantic_self__.__config__, "table", False) + or getattr(__pydantic_self__.__config__, "validate", False)) and validation_error ): raise validation_error + raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them # object.__setattr__(__pydantic_self__, '__dict__', values) From 8857b758055c68c94b474197c1304322d4f1d66b Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins <90264926+AndrewRiggs-Atkins@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:34:18 +0000 Subject: [PATCH 2/6] Updated formatting to resolve E128 lint error --- sqlmodel/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index f574421f4d..3cb21d856b 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -507,13 +507,11 @@ def __init__(__pydantic_self__, **data: Any) -> None: __pydantic_self__.__class__, data ) # Only raise errors if not a SQLModel model - if ( - (not getattr(__pydantic_self__.__config__, "table", False) + if ((not getattr(__pydantic_self__.__config__, "table", False) or getattr(__pydantic_self__.__config__, "validate", False)) and validation_error ): raise validation_error - raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them # object.__setattr__(__pydantic_self__, '__dict__', values) From d37b8525b716193f9498464c3ec00929995bc556 Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins <90264926+AndrewRiggs-Atkins@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:38:44 +0000 Subject: [PATCH 3/6] Updated formatting to resolve E124 lint error --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 3cb21d856b..2ab4929ac4 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -510,7 +510,7 @@ def __init__(__pydantic_self__, **data: Any) -> None: if ((not getattr(__pydantic_self__.__config__, "table", False) or getattr(__pydantic_self__.__config__, "validate", False)) and validation_error - ): + ): raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them From d49a0d600fb2aa2f5317a0ed2c29b71dbb16b1ee Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins <90264926+AndrewRiggs-Atkins@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:42:56 +0000 Subject: [PATCH 4/6] Updated formatting to resolve E125 lint error --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 2ab4929ac4..f343a76e5d 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -510,7 +510,7 @@ def __init__(__pydantic_self__, **data: Any) -> None: if ((not getattr(__pydantic_self__.__config__, "table", False) or getattr(__pydantic_self__.__config__, "validate", False)) and validation_error - ): + ): raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them From 115de85de1aca953dc79c67b8925df1684165307 Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins <90264926+AndrewRiggs-Atkins@users.noreply.github.com> Date: Wed, 15 Feb 2023 12:00:07 +0000 Subject: [PATCH 5/6] Updated formatting to resolve E124 lint error --- sqlmodel/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index f343a76e5d..c47902651c 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -508,9 +508,8 @@ def __init__(__pydantic_self__, **data: Any) -> None: ) # Only raise errors if not a SQLModel model if ((not getattr(__pydantic_self__.__config__, "table", False) - or getattr(__pydantic_self__.__config__, "validate", False)) - and validation_error - ): + or getattr(__pydantic_self__.__config__, "validate", False)) + and validation_error): raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them From 4499f96e59c6d7b60720742f46b472d500d25fbe Mon Sep 17 00:00:00 2001 From: AndrewRiggs-Atkins Date: Wed, 15 Feb 2023 12:39:04 +0000 Subject: [PATCH 6/6] Updated formatting to resolve E124 lint error --- sqlmodel/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index c47902651c..8e94be8e8f 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -507,9 +507,10 @@ def __init__(__pydantic_self__, **data: Any) -> None: __pydantic_self__.__class__, data ) # Only raise errors if not a SQLModel model - if ((not getattr(__pydantic_self__.__config__, "table", False) - or getattr(__pydantic_self__.__config__, "validate", False)) - and validation_error): + if ( + not getattr(__pydantic_self__.__config__, "table", False) + or getattr(__pydantic_self__.__config__, "validate", False) + ) and validation_error: raise validation_error # Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy # can handle them