Title: overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type · Issue #22385 · python/mypy · GitHub
Open Graph Title: overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type · Issue #22385 · python/mypy
X Title: overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type · Issue #22385 · python/mypy
Description: Bug Report For a dataclass with an InitVar, mypy checks the user's __post_init__ against the signature built from the InitVar fields (added in #15503). When __post_init__ is overloaded, every overload item is checked on its own, so an it...
Open Graph Description: Bug Report For a dataclass with an InitVar, mypy checks the user's __post_init__ against the signature built from the InitVar fields (added in #15503). When __post_init__ is overloaded, every overl...
X Description: Bug Report For a dataclass with an InitVar, mypy checks the user's __post_init__ against the signature built from the InitVar fields (added in #15503). When __post_init__ is overloaded, every o...
Opengraph URL: https://github.com/python/mypy/issues/22385
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type","articleBody":"**Bug Report**\n\nFor a dataclass with an `InitVar`, mypy checks the user's `__post_init__` against the signature built from the `InitVar` fields (added in #15503). When `__post_init__` is overloaded, every overload item is checked on its own, so an item that does not take the `InitVar` type is reported as an incompatible override even though another item does. The overloaded method as a whole accepts what the dataclass passes, and CPython runs the program.\n\n**To Reproduce**\n\n```python\nfrom dataclasses import InitVar, dataclass\nfrom typing import overload\n\n\n@dataclass\nclass Record:\n value: InitVar[int]\n\n @overload\n def __post_init__(self, value: int) -> None: ...\n @overload\n def __post_init__(self, value: str) -> None: ...\n def __post_init__(self, value: int | str) -> None:\n print(type(value))\n\n\nRecord(1)\n```\n\n**Actual Behavior**\n\n```\n$ mypy --python-version 3.13 test_bug.py\ntest_bug.py:12: error: Argument 2 of \"__post_init__\" is incompatible with supertype \"dataclass\"; supertype defines the argument type as \"int\" [override]\nFound 1 error in 1 file (checked 1 source file)\n\n$ python test_bug.py\n\u003cclass 'int'>\n```\n\nThe error is on the `str` item (line 12). The `int` item and the implementation (`int | str`) are not reported.\n\n**Expected Behavior**\n\nNo error. The generated call is `__post_init__(value)` with an `int`, which the first overload accepts. For an ordinary method, mypy already accepts an overloaded override of a non-overloaded method when one item is compatible:\n\n```python\nfrom typing import overload\n\n\nclass Base:\n def post(self, value: int) -> None: ...\n\n\nclass Child(Base):\n @overload\n def post(self, value: int) -> None: ...\n @overload\n def post(self, value: str) -> None: ...\n def post(self, value: int | str) -> None:\n print(type(value))\n```\n\n`mypy test_control.py` prints `Success: no issues found`. If no item takes the `InitVar` type (for example overloads for `str` and `bytes` with `InitVar[int]`), an error is right, and mypy reports both items.\n\nI read `checker.py` on master: `check_func_def` calls `dataclasses_plugin.check_post_init(self, defn, defn.info)` for a function named `__post_init__`, and `check_post_init` passes `defn.type` to `check_override`. For an overloaded method `defn` is a single item (the line of the error above) and not the `Overloaded` type, which would explain the difference to the ordinary override. I did not debug it further.\n\n[Playground URL](https://mypy-play.net/?gist=3106034e3045d9c2d249c647d6dba054&mypy=latest&python=3.13)\n**My Environment**\n\n- Mypy version used: 2.4.0, 2.4.1 and master 2.5.0+dev.879da562\n- Mypy command-line flags: `--python-version 3.13`\n- Mypy configuration options from `mypy.ini` (and other config files): none\n- Python version used: 3.13.16\n- Operating system: Linux","author":{"url":"https://github.com/DerekLaymn","@type":"Person","name":"DerekLaymn"},"datePublished":"2026-11-06T15:27:37Z","url":"https://github.com/python/mypy/issues/22385"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bccbb2fe-975b-0b88-0120-dc7bcce20561 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1F8:2AD163:28AA437:376BDF1:6B0048B3 |
| html-safe-nonce | d534ae1dac8dffe758ebec889d4f2aa4666a44d435a4b55c974576b1af6f6a1c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUY4OjJBRDE2MzoyOEFBNDM3OjM3NkJERjE6NkIwMDQ4QjMiLCJ2aXNpdG9yX2lkIjoiNjk0NDY3NDg5Nzc4NDIyODAxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a921ac0706eeb9e5d580bd424782f5210a4335de727bd3339371bf0fcb2e2560 |
| hovercard-subject-tag | issue:6245472690 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/22385/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c6838878ddaf701ef332314f8fe97f1ba5a68c5ed312e13e98796b8587480535/python/mypy/issues/22385 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c6838878ddaf701ef332314f8fe97f1ba5a68c5ed312e13e98796b8587480535/python/mypy/issues/22385 |
| og:image:alt | Bug Report For a dataclass with an InitVar, mypy checks the user's __post_init__ against the signature built from the InitVar fields (added in #15503). When __post_init__ is overloaded, every overl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DerekLaymn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width