Skip to content

⬆️ Update mypy to 1.14.1 - #12970

Merged
tiangolo merged 10 commits into
fastapi:masterfrom
tamird:mypy-bump-pydantic
Sep 20, 2025
Merged

⬆️ Update mypy to 1.14.1#12970
tiangolo merged 10 commits into
fastapi:masterfrom
tamird:mypy-bump-pydantic

Conversation

@tamird

@tamird tamird commented Nov 22, 2024

Copy link
Copy Markdown
Contributor

Depends on #12971.

@github-actions

This comment was marked as outdated.

@tamird
tamird force-pushed the mypy-bump-pydantic branch from 7562573 to fa6ff74 Compare November 22, 2024 13:51
@github-actions

This comment was marked as outdated.

@tamird tamird changed the title Update and fix mypy Upgrade and fix mypy Nov 22, 2024
@tamird tamird changed the title Upgrade and fix mypy ♻️ Update and fix mypy Nov 22, 2024
@tamird
tamird force-pushed the mypy-bump-pydantic branch from fa6ff74 to 2c7401f Compare November 22, 2024 14:30
@tamird tamird changed the title ♻️ Update and fix mypy ♻️ Update mypy to 1.13.0 Nov 22, 2024
@tamird
tamird force-pushed the mypy-bump-pydantic branch from 059e770 to 4af9fab Compare November 22, 2024 14:38
@github-actions

This comment was marked as outdated.

@tamird
tamird force-pushed the mypy-bump-pydantic branch from 4af9fab to a7b407f Compare November 22, 2024 17:11
@github-actions

This comment was marked as outdated.

Comment thread fastapi/encoders.py Outdated
@svlandeg svlandeg added dependencies Pull requests that update a dependency file upgrade labels Nov 25, 2024
@tamird
tamird force-pushed the mypy-bump-pydantic branch from a7b407f to d960729 Compare November 30, 2024 14:06
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@tamird
tamird force-pushed the mypy-bump-pydantic branch from cd52600 to 2f92fca Compare December 16, 2024 15:57
@tamird

tamird commented Dec 16, 2024

Copy link
Copy Markdown
Contributor Author

@alejsdev could you please take a look? This is relatively trivial.

@github-actions

This comment was marked as outdated.

Comment thread pyproject.toml Outdated
@tamird

tamird commented Jan 13, 2025

Copy link
Copy Markdown
Contributor Author

@Kludex thanks for the approval. Are you able to land this please?

@Kludex

Kludex commented Jan 13, 2025

Copy link
Copy Markdown
Member

No. I don't merge PRs.

@svlandeg svlandeg changed the title ♻️ Update mypy to 1.13.0 ⬆️ Update mypy to 1.13.0 Jan 20, 2025
@tamird

tamird commented Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

@tiangolo could you please have a look? you merged #12971 quickly but perhaps forgot about this one.

@tamird
tamird force-pushed the mypy-bump-pydantic branch from 2f92fca to d38dd2f Compare February 5, 2025 21:01
@tamird tamird changed the title ⬆️ Update mypy to 1.13.0 ⬆️ Update mypy to 1.15.0 Feb 5, 2025
@tamird
tamird force-pushed the mypy-bump-pydantic branch from d38dd2f to 744540d Compare February 5, 2025 21:09
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@YuriiMotov YuriiMotov changed the title ⬆️ Update mypy to 1.15.0, ruff to 0.9.4 ⬆️ Update mypy to 1.14.1 (last version compatible with Python 3.8), install pydantic.mypy plugin Jun 30, 2025

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

LGTM

Maybe we should extract the part related to plugin installation into a separate PR, but doesn't seem critical to me

@tamird

tamird commented Jul 10, 2025

Copy link
Copy Markdown
Contributor Author

Thank you @YuriiMotov. Are you planning to merge this?

@YuriiMotov
YuriiMotov requested a review from tiangolo July 14, 2025 06:35
@svlandeg svlandeg changed the title ⬆️ Update mypy to 1.14.1 (last version compatible with Python 3.8), install pydantic.mypy plugin ⬆️ Update mypy to 1.14.1 and install pydantic.mypy plugin Sep 16, 2025
@svlandeg svlandeg assigned svlandeg and unassigned tiangolo Sep 16, 2025
@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 21c8f18 at: https://c3a1493f.fastapitiangolo.pages.dev

@svlandeg svlandeg changed the title ⬆️ Update mypy to 1.14.1 and install pydantic.mypy plugin ⬆️ Update mypy to 1.14.1 Sep 16, 2025

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

Ok, so in an attempt to get this merged, I've moved the pydantic.mypy plugin changes to a separate PR #14081. This makes it easier to review which changes are causing what, as several reviewers had questions about what exactly is needed and what relates to what.

For this PR, the goal is simply to update to mypy 1.14.1. When doing so, we're getting 2 similar errors:

fastapi\encoders.py:244: error: Argument 1 to "asdict" has incompatible type "DataclassInstance | type[DataclassInstance]"; expected "DataclassInstance"  [arg-type]

What happens here is that first, we're calling dataclasses.is_dataclass(obj).

Due to the typing of this method in their dataclasses.pyi, mypy will go and type obj as DataclassInstance | type[DataclassInstance]. This then doesn't align with the signature of the following dataclasses.asdict(obj) statement, which expects a DataclassInstance.

So, these are the possible fixes for this:

  • add an ignore statement
  • add a type() call before calling is_dataclass, as @tamird suggests. This isn't actually necessary, because the implementation of dataclasses.is_dataclass will call type() when necessary. However, by doing this, we trick mypy into assuming that obj is NOT a type[DataclassInstance] and then it won't error on the following asdict call
  • inbetween the original is_dataclass check and the asdict call, add the following: assert not isinstance(obj, type). This again will tell mypy not to assume the obj is a type. To me, this is the most direct/clear solution, but I think it's a matter of taste.

Will run this by Tiangolo to get his opinion.

@svlandeg svlandeg removed their assignment Sep 16, 2025
@tiangolo

Copy link
Copy Markdown
Member

Thanks a lot for the investigation and clarification with all the context @svlandeg! Yep, it makes more sense to me to use assert there instead of using type() inside of it.

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

Nice, thanks! 🚀

And thanks @svlandeg for the analysis, discussion and work on this! 🙌

@tiangolo
tiangolo merged commit a95e91e into fastapi:master Sep 20, 2025
29 checks passed
@tamird
tamird deleted the mypy-bump-pydantic branch September 20, 2025 12:15
@tiangolo tiangolo added internal Internal changes and removed upgrade labels Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file internal Internal changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants