Improve mypy typing performance #387
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ref: python/mypy#14627
Trying to run mypy with pydantic
2.0.0.dev.0
hangs when trying to lintThe likely reason for that is the
CoreSchema
type. Mypy can't handle unions with a lot of items that well. Especially for TypedDicts, it tries to find a common base type but when there is none, checks the compatibility of each keys individually.That's even more of an issue as there are also some recursive annotations with
CoreSchema
.pydantic-core/pydantic_core/core_schema.py
Lines 2547 to 2588 in bfb373c
pydantic-core/pydantic_core/core_schema.py
Lines 1724 to 1726 in bfb373c
To improve mypy performance, I added an empty
CoreSchemaBase
TypedDict type from which allCoreSchema
types can inherit and replaced the recursiveCoreSchema
types. With that a mypy run is back down to 15-20s.