Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix class initialization compatibility with Pydantic and SQLModel, fixing errors revealed by the latest Pydantic #807

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 6 additions & 8 deletions sqlmodel/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def set_config_value(
def get_model_fields(model: InstanceOrType["SQLModel"]) -> Dict[str, "FieldInfo"]:
return model.model_fields

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 @@ -387,10 +387,8 @@ def set_config_value(
def get_model_fields(model: InstanceOrType["SQLModel"]) -> Dict[str, "FieldInfo"]:
return model.__fields__ # type: ignore

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
Loading