Skip to content

Commit

Permalink
Merge branch 'main' into update
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored Feb 17, 2024
2 parents a65b62b + 7fec884 commit 89b7546
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ jobs:
run: python -m poetry install
- name: Install Pydantic v1
if: matrix.pydantic-version == 'pydantic-v1'
run: pip install "pydantic>=1.10.0,<2.0.0"
run: pip install --upgrade "pydantic>=1.10.0,<2.0.0"
- name: Install Pydantic v2
if: matrix.pydantic-version == 'pydantic-v2'
run: pip install "pydantic>=2.0.2,<3.0.0"
run: pip install --upgrade "pydantic>=2.0.2,<3.0.0"
- name: Lint
# Do not run on Python 3.7 as mypy behaves differently
if: matrix.python-version != '3.7' && matrix.pydantic-version == 'pydantic-v2'
Expand Down
6 changes: 6 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Latest Changes

## 0.0.15

### Fixes

* 🐛 Fix class initialization compatibility with Pydantic and SQLModel, fixing errors revealed by the latest Pydantic. PR [#807](https://github.com/tiangolo/sqlmodel/pull/807) by [@tiangolo](https://github.com/tiangolo).

### Internal

* ⬆ Bump tiangolo/issue-manager from 0.4.0 to 0.4.1. PR [#775](https://github.com/tiangolo/sqlmodel/pull/775) by [@dependabot[bot]](https://github.com/apps/dependabot).
Expand Down
2 changes: 1 addition & 1 deletion sqlmodel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.14"
__version__ = "0.0.15"

# Re-export from SQLAlchemy
from sqlalchemy.engine import create_engine as create_engine
Expand Down
14 changes: 6 additions & 8 deletions sqlmodel/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def get_fields_set(
) -> Union[Set[str], Callable[[BaseModel], Set[str]]]:
return object.model_fields_set

def set_fields_set(
new_object: InstanceOrType["SQLModel"], fields: Set["FieldInfo"]
) -> None:
object.__setattr__(new_object, "__pydantic_fields_set__", fields)
def init_pydantic_private_attrs(new_object: InstanceOrType["SQLModel"]) -> None:
object.__setattr__(new_object, "__pydantic_fields_set__", set())
object.__setattr__(new_object, "__pydantic_extra__", None)
object.__setattr__(new_object, "__pydantic_private__", None)

def get_annotations(class_dict: Dict[str, Any]) -> Dict[str, Any]:
return class_dict.get("__annotations__", {})
Expand Down Expand Up @@ -401,10 +401,8 @@ def get_fields_set(
) -> Union[Set[str], Callable[[BaseModel], Set[str]]]:
return object.__fields_set__

def set_fields_set(
new_object: InstanceOrType["SQLModel"], fields: Set["FieldInfo"]
) -> None:
object.__setattr__(new_object, "__fields_set__", fields)
def init_pydantic_private_attrs(new_object: InstanceOrType["SQLModel"]) -> None:
object.__setattr__(new_object, "__fields_set__", set())

def get_annotations(class_dict: Dict[str, Any]) -> Dict[str, Any]:
return resolve_annotations( # type: ignore[no-any-return]
Expand Down
6 changes: 3 additions & 3 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
get_model_fields,
get_relationship_to,
get_type_from_field,
init_pydantic_private_attrs,
is_field_noneable,
is_table_model_class,
post_init_field_info,
set_config_value,
set_fields_set,
sqlmodel_init,
sqlmodel_validate,
)
Expand Down Expand Up @@ -686,12 +686,12 @@ class Config:

def __new__(cls, *args: Any, **kwargs: Any) -> Any:
new_object = super().__new__(cls)
# SQLAlchemy doesn't call __init__ on the base class
# SQLAlchemy doesn't call __init__ on the base class when querying from DB
# Ref: https://docs.sqlalchemy.org/en/14/orm/constructors.html
# Set __fields_set__ here, that would have been set when calling __init__
# in the Pydantic model so that when SQLAlchemy sets attributes that are
# added (e.g. when querying from DB) to the __fields_set__, this already exists
set_fields_set(new_object, set())
init_pydantic_private_attrs(new_object)
return new_object

def __init__(__pydantic_self__, **data: Any) -> None:
Expand Down

0 comments on commit 89b7546

Please sign in to comment.