Skip to content

Check overloaded __post_init__ against the dataclass signature as a whole - #22508

Open
zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/post-init-overload-22385
Open

zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/post-init-overload-22385

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #22385

For a dataclass with an InitVar, mypy checks the user __post_init__ against the signature built from the InitVar fields. When __post_init__ is overloaded, every overload item was checked on its own, so an item that does not take the InitVar type was reported as an incompatible override even though another item accepts what the dataclass passes and CPython runs the program.

This makes check_post_init check an overloaded __post_init__ once, as a whole: when the definition is an OverloadedFuncDef with an Overloaded type, only the first item proceeds and check_override is called once with the full Overloaded type; 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 the check_override call site and keeps the fallback.)

Tests: new testPostInitOverloadCompatible (issue repro, no errors) and testPostInitOverloadIncompatible (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.

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
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type

1 participant