FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

💚 Fix linting in CI by svlandeg · Pull Request #1340 · fastapi/sqlmodel · GitHub

Repository navigation

2 changes: 1 addition & 1 deletion .github/workflows/test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
if: matrix.pydantic-version == 'pydantic-v2'
run: uv pip install --upgrade "pydantic>=2.0.2,<3.0.0"
- name: Lint
if: matrix.pydantic-version == 'pydantic-v2'
if: matrix.pydantic-version == 'pydantic-v2' && matrix.python-version != '3.8'
run: bash scripts/lint.sh
- run: mkdir coverage
- name: Test
Expand Down
9 changes: 8 additions & 1 deletion sqlmodel/_compat.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ def set_config_value(
model.model_config[parameter] = value # type: ignore[literal-required]

def get_model_fields(model: InstanceOrType[BaseModel]) -> Dict[str, "FieldInfo"]:
return model.model_fields
# TODO: refactor the usage of this function to always pass the class
# not the instance, and then remove this extra check
# this is for compatibility with Pydantic v3
if isinstance(model, type):
use_model = model
else:
use_model = model.__class__
return use_model.model_fields
Comment on lines +109 to +113

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I was checking the error from Pydantic and I saw that they will deprecate accessing the model_fields from the instance in Pydantic v3, they should be accessed from the class (which makes sense).

I updated this logic here to handle that future use case. In a subsequent PR we can refactor the internal usage of this function to only pass the class and not the instance.


def get_fields_set(
object: InstanceOrType["SQLModel"],
Expand Down
15 changes: 10 additions & 5 deletions sqlmodel/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def Relationship(
class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
__sqlmodel_relationships__: Dict[str, RelationshipInfo]
model_config: SQLModelConfig
model_fields: Dict[str, FieldInfo] # type: ignore[assignment]
model_fields: ClassVar[Dict[str, FieldInfo]]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

As the idea is that this should now be only in the class and not instances, I think it's good to type it right away. It won't affect at runtime if people are using it, but will start warning them in their linters, so they can start preparing for this change in Pydantic v3.

__config__: Type[SQLModelConfig]
__fields__: Dict[str, ModelField] # type: ignore[assignment]

Expand Down Expand Up @@ -839,7 +839,7 @@ def __tablename__(cls) -> str:
return cls.__name__.lower()

@classmethod
def model_validate(
def model_validate( # type: ignore[override]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I changed this to not use kwargs and instead ignore the type here, that way people will keep having autocompletion for the params, and kwargs won't swallow extra / invald params.

Seeing this, I realize it was probably not the best idea to include the extra update here. 🤔

Also, it's probably not needed for most use cases, maybe it would make sense to remove those docs, explain how to extend data to validate an object using just dicts, then deprecate using it, and finally at some point dropping the update...

But anyway, for now, it probably works to just # type: ignore and handle it on our side so that final users get the best developer experience / editor support.

cls: Type[_TSQLModel],
obj: Any,
*,
Expand All @@ -863,20 +863,25 @@ def model_dump(
mode: Union[Literal["json", "python"], str] = "python",
include: Union[IncEx, None] = None,
exclude: Union[IncEx, None] = None,
context: Union[Dict[str, Any], None] = None,
by_alias: bool = False,
context: Union[Any, None] = None,
by_alias: Union[bool, None] = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I like to keep the signature as close to the latest Pydantic, and then do the extra work internally to support the older versions, but this way people can get used to the new syntax and/or use it if they can already upgrade to the latest version.

exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: Union[bool, Literal["none", "warn", "error"]] = True,
fallback: Union[Callable[[Any], Any], None] = None,
serialize_as_any: bool = False,
) -> Dict[str, Any]:
if PYDANTIC_MINOR_VERSION < (2, 11):
by_alias = by_alias or False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

With this we can handle older Pydantic versions but keep the most recent signature.

if PYDANTIC_MINOR_VERSION >= (2, 7):
extra_kwargs: Dict[str, Any] = {
"context": context,
"serialize_as_any": serialize_as_any,
}
if PYDANTIC_MINOR_VERSION >= (2, 11):
extra_kwargs["fallback"] = fallback
else:
extra_kwargs = {}
if IS_PYDANTIC_V2:
Expand All @@ -896,7 +901,7 @@ def model_dump(
return super().dict(
include=include,
exclude=exclude,
by_alias=by_alias,
by_alias=by_alias or False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

With this trick, we avoid the type when by_alias is None. 😅

exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
Expand Down

Back | FazBrowse Home | New Git URL