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

💚 Fix linting in CI - #1340

Merged
tiangolo merged 11 commits into
fastapi:mainfrom
svlandeg:fix/ci
Apr 26, 2025
Merged

tiangolo merged 11 commits into
fastapi:mainfrom
svlandeg:fix/ci

Conversation

svlandeg commented Apr 9, 2025 •
edited
Loading

Copy link
Copy Markdown
Member

Recent PRs have been failing due to an issue with the linting step in the CI - I think caused by the recent release of Pydantic 2.11.

Overview of issues and proposed fixes:

  • Elaborate type warning for get_model_fields in _compat.py.
sqlmodel/_compat.py:106: error: Argument 1 to "__get__" of "deprecated_instance_property" has incompatible type "BaseModel | type[BaseModel]"; expected "BaseModel"  [arg-type]
sqlmodel/_compat.py:106: error: Argument 2 to "__get__" of "deprecated_instance_property" has incompatible type "type[BaseModel] | type[type[BaseModel]]"; expected "type[BaseModel]"  [arg-type]

I think it's fine to ignore this one?

  • model_fields in SQLModelMetaclass on the other hand, now can do without an ignore warning
  • SQLModel.model_validate() is supposed to overwrite BaseModel.model_validate() but an error gets raised because of the update input parameter, which is not present in BaseModel.model_validate(). The proposed solution here is to work via **kwargs, which I'm generally not a fan of, but the only other option I would see is adding yet another ignore, which feels wrong in this case.
  • SQLModel.model_dump() is supposed to overwrite BaseModel.model_dump() but an error gets raised because of context and alias having a wrong type, and fallback not being present. fallback was introduced in Pydantic v2.11. So instead of restricting Pydantic, I suggest to (again) work via **kwargs.

The linting test is failing for Python 3.8 in different ways than for all other Python versions, probably because Python 3.8 support was removed for Pydantic 2.11. Accordingly, I've updated the test.yml to skip the linting test for Python 3.8. This doesn't feel like a clean solution either, and I guess the other option is to (also) drop support for Python 3.8.

svlandeg added the internal Internal changes label Apr 9, 2025
svlandeg self-assigned this Apr 9, 2025

tiangolo left a comment

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

Thank you @svlandeg! 🚀

I tweaked it a bit mainly to keep autocompletion and inline errors for final users. Although this reveals some areas that probably deserve some tweaking in the future. 🤔

I added some inline comments to share my mental process.

Thanks! 🚀

Comment thread sqlmodel/_compat.py
Comment on lines +109 to +113
if isinstance(model, type):
use_model = model
else:
use_model = model.__class__
return use_model.model_fields

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.

Comment thread sqlmodel/main.py
__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.

Comment thread sqlmodel/main.py

@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.

Comment thread sqlmodel/main.py
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.

Comment thread sqlmodel/main.py
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. 😅

Comment thread sqlmodel/main.py
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.

tiangolo merged commit 4b5ad42 into fastapi:main Apr 26, 2025
svlandeg deleted the fix/ci branch April 28, 2025 07:12
spazm added a commit to spazm/sqlmodel that referenced this pull request Oct 8, 2025
…. v2.10

  + The else block added to fix linting errors (4b5ad42) for fastapi#1340 was
    obliterating the 'context' and 'serialize_as_any' attributes for
    versions >= 2.7 and < 2.11.
  + Fix by initializing extra_kwargs and then setting on a per-version level
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Internal changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL