From 52b5c12111177088b3e72316f3bbda4a1f2c99ea Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 28 Aug 2021 16:38:35 +0100 Subject: [PATCH 1/3] chore : Hoist repeated code outside conditional statement --- sqlmodel/sql/sqltypes.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sqlmodel/sql/sqltypes.py b/sqlmodel/sql/sqltypes.py index e7b77b8c52..2be7324895 100644 --- a/sqlmodel/sql/sqltypes.py +++ b/sqlmodel/sql/sqltypes.py @@ -52,9 +52,6 @@ def process_bind_param(self, value, dialect): return f"{value.int:x}" def process_result_value(self, value, dialect): - if value is None: - return value - else: - if not isinstance(value, uuid.UUID): - value = uuid.UUID(value) - return value + if value is not None and not isinstance(value, uuid.UUID): + value = uuid.UUID(value) + return value From 0a5460b29a2b9c65d3ca5e68ab36cfe651bdd94b Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 28 Aug 2021 16:39:11 +0100 Subject: [PATCH 2/3] Merges multiple nested `if` conditions into one --- sqlmodel/main.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 661276b31d..f9dd1a557b 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -199,14 +199,13 @@ def Relationship( sa_relationship_args: Optional[Sequence[Any]] = None, sa_relationship_kwargs: Optional[Mapping[str, Any]] = None, ) -> Any: - relationship_info = RelationshipInfo( + return RelationshipInfo( back_populates=back_populates, link_model=link_model, sa_relationship=sa_relationship, sa_relationship_args=sa_relationship_args, sa_relationship_kwargs=sa_relationship_kwargs, ) - return relationship_info @__dataclass_transform__(kw_only_default=True, field_descriptors=(Field, FieldInfo)) @@ -511,9 +510,8 @@ def __setattr__(self, name: str, value: Any) -> None: return else: # Set in SQLAlchemy, before Pydantic to trigger events and updates - if getattr(self.__config__, "table", False): - if is_instrumented(self, name): - set_attribute(self, name, value) + if getattr(self.__config__, "table", False) and is_instrumented(self, name): + set_attribute(self, name, value) # Set in Pydantic model to trigger possible validation changes, only for # non relationship values if name not in self.__sqlmodel_relationships__: @@ -531,13 +529,9 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None): if update is not None: obj = {**obj, **update} # End SQLModel support dict - if not getattr(cls.__config__, "table", False): - # If not table, normal Pydantic code - m = cls.__new__(cls) - else: - # If table, create the new instance normally to make SQLAlchemy create - # the _sa_instance_state attribute - m = cls() + m = cls.__new__(cls) if not getattr(cls.__config__, "table", False) else cls() + # If table, create the new instance normally to make SQLAlchemy create + # the _sa_instance_state attribute values, fields_set, validation_error = validate_model(cls, obj) if validation_error: raise validation_error @@ -601,7 +595,7 @@ def _calculate_keys( # type: ignore exclude_unset: bool, update: Optional[Dict[str, Any]] = None, ) -> Optional[AbstractSet[str]]: - if include is None and exclude is None and exclude_unset is False: + if include is None and exclude is None and not exclude_unset: # Original in Pydantic: # return None # Updated to not return SQLAlchemy attributes @@ -610,16 +604,7 @@ def _calculate_keys( # type: ignore return self.__fields__.keys() # | self.__sqlmodel_relationships__.keys() keys: AbstractSet[str] - if exclude_unset: - keys = self.__fields_set__.copy() - else: - # Original in Pydantic: - # keys = self.__dict__.keys() - # Updated to not return SQLAlchemy attributes - # Do not include relationships as that would easily lead to infinite - # recursion, or traversing the whole database - keys = self.__fields__.keys() # | self.__sqlmodel_relationships__.keys() - + keys = self.__fields_set__.copy() if exclude_unset else self.__fields__.keys() if include is not None: keys &= include.keys() From eda3d8a20ed8725a47fa5dd9afebd833fed94166 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Sat, 27 Aug 2022 22:45:54 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=94=A7=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 13 +++++++++++-- sqlmodel/sql/sqltypes.py | 1 - 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 8365ab2437..d728cfc569 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -198,13 +198,14 @@ def Relationship( sa_relationship_args: Optional[Sequence[Any]] = None, sa_relationship_kwargs: Optional[Mapping[str, Any]] = None, ) -> Any: - return RelationshipInfo( + relationship_info = RelationshipInfo( back_populates=back_populates, link_model=link_model, sa_relationship=sa_relationship, sa_relationship_args=sa_relationship_args, sa_relationship_kwargs=sa_relationship_kwargs, ) + return relationship_info @__dataclass_transform__(kw_only_default=True, field_descriptors=(Field, FieldInfo)) @@ -620,7 +621,15 @@ def _calculate_keys( return self.__fields__.keys() # | self.__sqlmodel_relationships__.keys() keys: AbstractSet[str] - keys = self.__fields_set__.copy() if exclude_unset else self.__fields__.keys() + if exclude_unset: + keys = self.__fields_set__.copy() + else: + # Original in Pydantic: + # keys = self.__dict__.keys() + # Updated to not return SQLAlchemy attributes + # Do not include relationships as that would easily lead to infinite + # recursion, or traversing the whole database + keys = self.__fields__.keys() # | self.__sqlmodel_relationships__.keys() if include is not None: keys &= include.keys() diff --git a/sqlmodel/sql/sqltypes.py b/sqlmodel/sql/sqltypes.py index 2a5ef3e45e..b3fda87739 100644 --- a/sqlmodel/sql/sqltypes.py +++ b/sqlmodel/sql/sqltypes.py @@ -52,7 +52,6 @@ def process_bind_param(self, value: Any, dialect: Dialect) -> Optional[str]: # hexstring return f"{value.int:x}" - def process_result_value(self, value: Any, dialect: Dialect) -> Optional[uuid.UUID]: if value is None: return value