Repository navigation
Check overloaded __post_init__ against the dataclass signature as a whole - #22508
Open
zhengdaodeguL wants to merge 1 commit into
Open
zhengdaodeguL wants to merge 1 commit into
zhengdaodeguL wants to merge 1 commit into
Conversation
check_post_init ran once per overload item, so an item that does not take the InitVar type was reported as an incompatible override even when another item accepts what the dataclass passes. Check the overload as a whole once, from its first item, and fall back to the per-item check when no aggregate overload type exists. Fixes python#22385
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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
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.
Fixes #22385
For a dataclass with an
InitVar, mypy checks the user__post_init__against the signature built from theInitVarfields. When__post_init__is overloaded, every overload item was checked on its own, so an item that does not take theInitVartype was reported as an incompatible override even though another item accepts what the dataclass passes and CPython runs the program.This makes
check_post_initcheck an overloaded__post_init__once, as a whole: when the definition is anOverloadedFuncDefwith anOverloadedtype, only the first item proceeds andcheck_overrideis called once with the fullOverloadedtype; the remaining items and the implementation return early. If there is no aggregate type, it falls back to the previous per-item checking instead of silently skipping. (The earlier #22390 took a similar approach and was closed unmerged without any review or comment; the primer run on it showed zero impact. This version additionally unifies thecheck_overridecall site and keeps the fallback.)Tests: new
testPostInitOverloadCompatible(issue repro, no errors) andtestPostInitOverloadIncompatible(no compatible item, still reports the override error for the whole overload).pytest mypy/test/testcheck.py -k dataclass: 213 passed;pytest mypy/test/testfinegrained.py -k Dataclass: 14 passed.