sqlmodel 0.0.15__tar.gz → 0.0.16__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlmodel
3
- Version: 0.0.15
3
+ Version: 0.0.16
4
4
  Summary: SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
5
5
  Home-page: https://github.com/tiangolo/sqlmodel
6
6
  License: MIT
@@ -1,4 +1,4 @@
1
- __version__ = "0.0.15"
1
+ __version__ = "0.0.16"
2
2
 
3
3
  # Re-export from SQLAlchemy
4
4
  from sqlalchemy.engine import create_engine as create_engine
@@ -6,6 +6,7 @@ from typing import (
6
6
  TYPE_CHECKING,
7
7
  AbstractSet,
8
8
  Any,
9
+ Callable,
9
10
  Dict,
10
11
  ForwardRef,
11
12
  Generator,
@@ -18,6 +19,7 @@ from typing import (
18
19
  )
19
20
 
20
21
  from pydantic import VERSION as PYDANTIC_VERSION
22
+ from pydantic import BaseModel
21
23
  from pydantic.fields import FieldInfo
22
24
  from typing_extensions import get_args, get_origin
23
25
 
@@ -46,9 +48,11 @@ class ObjectWithUpdateWrapper:
46
48
  update: Dict[str, Any]
47
49
 
48
50
  def __getattribute__(self, __name: str) -> Any:
49
- if __name in self.update:
50
- return self.update[__name]
51
- return getattr(self.obj, __name)
51
+ update = super().__getattribute__("update")
52
+ obj = super().__getattribute__("obj")
53
+ if __name in update:
54
+ return update[__name]
55
+ return getattr(obj, __name)
52
56
 
53
57
 
54
58
  def _is_union_type(t: Any) -> bool:
@@ -94,9 +98,14 @@ if IS_PYDANTIC_V2:
94
98
  ) -> None:
95
99
  model.model_config[parameter] = value # type: ignore[literal-required]
96
100
 
97
- def get_model_fields(model: InstanceOrType["SQLModel"]) -> Dict[str, "FieldInfo"]:
101
+ def get_model_fields(model: InstanceOrType[BaseModel]) -> Dict[str, "FieldInfo"]:
98
102
  return model.model_fields
99
103
 
104
+ def get_fields_set(
105
+ object: InstanceOrType["SQLModel"],
106
+ ) -> Union[Set[str], Callable[[BaseModel], Set[str]]]:
107
+ return object.model_fields_set
108
+
100
109
  def init_pydantic_private_attrs(new_object: InstanceOrType["SQLModel"]) -> None:
101
110
  object.__setattr__(new_object, "__pydantic_fields_set__", set())
102
111
  object.__setattr__(new_object, "__pydantic_extra__", None)
@@ -384,9 +393,14 @@ else:
384
393
  ) -> None:
385
394
  setattr(model.__config__, parameter, value) # type: ignore
386
395
 
387
- def get_model_fields(model: InstanceOrType["SQLModel"]) -> Dict[str, "FieldInfo"]:
396
+ def get_model_fields(model: InstanceOrType[BaseModel]) -> Dict[str, "FieldInfo"]:
388
397
  return model.__fields__ # type: ignore
389
398
 
399
+ def get_fields_set(
400
+ object: InstanceOrType["SQLModel"],
401
+ ) -> Union[Set[str], Callable[[BaseModel], Set[str]]]:
402
+ return object.__fields_set__
403
+
390
404
  def init_pydantic_private_attrs(new_object: InstanceOrType["SQLModel"]) -> None:
391
405
  object.__setattr__(new_object, "__fields_set__", set())
392
406
 
@@ -758,7 +758,6 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
758
758
  update=update,
759
759
  )
760
760
 
761
- # TODO: remove when deprecating Pydantic v1, only for compatibility
762
761
  def model_dump(
763
762
  self,
764
763
  *,
@@ -869,3 +868,32 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
869
868
  exclude_unset=exclude_unset,
870
869
  update=update,
871
870
  )
871
+
872
+ def sqlmodel_update(
873
+ self: _TSQLModel,
874
+ obj: Union[Dict[str, Any], BaseModel],
875
+ *,
876
+ update: Union[Dict[str, Any], None] = None,
877
+ ) -> _TSQLModel:
878
+ use_update = (update or {}).copy()
879
+ if isinstance(obj, dict):
880
+ for key, value in {**obj, **use_update}.items():
881
+ if key in get_model_fields(self):
882
+ setattr(self, key, value)
883
+ elif isinstance(obj, BaseModel):
884
+ for key in get_model_fields(obj):
885
+ if key in use_update:
886
+ value = use_update.pop(key)
887
+ else:
888
+ value = getattr(obj, key)
889
+ setattr(self, key, value)
890
+ for remaining_key in use_update:
891
+ if remaining_key in get_model_fields(self):
892
+ value = use_update.pop(remaining_key)
893
+ setattr(self, remaining_key, value)
894
+ else:
895
+ raise ValueError(
896
+ "Can't use sqlmodel_update() with something that "
897
+ f"is not a dict or SQLModel or Pydantic model: {obj}"
898
+ )
899
+ return self
File without changes
File without changes
File without changes
File without changes
File without changes