Title: [mypyc] Subclass `__mypyc_defaults_setup` drops inherited class-attribute initializers across incremental builds · Issue #21542 · python/mypy · GitHub
Open Graph Title: [mypyc] Subclass `__mypyc_defaults_setup` drops inherited class-attribute initializers across incremental builds · Issue #21542 · python/mypy
X Title: [mypyc] Subclass `__mypyc_defaults_setup` drops inherited class-attribute initializers across incremental builds · Issue #21542 · python/mypy
Description: Bug Report When mypyc rebuilds a subclass and the subclass's base class is loaded from mypy's incremental cache (rather than freshly parsed), the subclass's emitted __mypyc_defaults_setup drops every inherited class-attribute initializer...
Open Graph Description: Bug Report When mypyc rebuilds a subclass and the subclass's base class is loaded from mypy's incremental cache (rather than freshly parsed), the subclass's emitted __mypyc_defaults_setup drops eve...
X Description: Bug Report When mypyc rebuilds a subclass and the subclass's base class is loaded from mypy's incremental cache (rather than freshly parsed), the subclass's emitted __mypyc_defaults_set...
Opengraph URL: https://github.com/python/mypy/issues/21542
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[mypyc] Subclass `__mypyc_defaults_setup` drops inherited class-attribute initializers across incremental builds","articleBody":"**Bug Report**\n\nWhen mypyc rebuilds a subclass and the subclass's base class is loaded from mypy's incremental cache (rather than freshly parsed), the subclass's emitted `__mypyc_defaults_setup` drops every inherited class-attribute initializer. The subclass's slots for those attributes stay at the \"undefined\" sentinel and any access through compiled code raises `AttributeError: attribute '\u003cname>' of '\u003cbase>' undefined`.\n\nThe condition shows up whenever a `mypycify(separate=True)` project gets built twice in a row with a content change to a base class in between. This is what `pip wheel` does when it runs `setup.py` for metadata and then again for the actual build.\n\n**To Reproduce**\n\n`pkg/__init__.py` (empty), `pkg/base.py`, `pkg/sub.py`, `setup.py`:\n\n```python\n# pkg/base.py\nclass Base:\n ATTR_00: bool = False\n ATTR_01: bool = False\n\n def use_attr(self) -> int:\n if self.ATTR_00:\n return 1\n return 0\n```\n\n```python\n# pkg/sub.py\nfrom pkg.base import Base\n\n\nclass Sub(Base):\n # An override forces Sub to emit its own __mypyc_defaults_setup.\n # Without it, Sub falls back to Base's via the type's MRO and the\n # dropped inherited default is silently masked.\n ATTR_01: bool = True\n```\n\n```python\n# setup.py\nfrom setuptools import setup\nfrom mypyc.build import mypycify\n\nsetup(\n name=\"pkg\",\n packages=[\"pkg\"],\n ext_modules=mypycify([\"pkg/base.py\", \"pkg/sub.py\"], separate=True),\n)\n```\n\n```bash\n# 1. clean build\nrm -rf build .mypy_cache pkg/*.so\npython setup.py build_ext --inplace\n\n# 2. any content change to base.py, plus touch all sources\nprintf \"\\n\" >> pkg/base.py\nfind pkg -name '*.py' -exec touch {} +\n\n# 3. two consecutive build_ext invocations\npython setup.py build_ext --inplace\npython setup.py build_ext --inplace\n\n# 4. crash\npython -c \"from pkg.sub import Sub; Sub().use_attr()\"\n```\n\nBoth `setup.py` invocations in step 3 are needed; a single `build_ext` against the same edit produces a correct binary. The `touch` is also needed, because without it distutils doesn't see anything out of date and the second invocation is a no-op.\n\n**Expected Behavior**\n\n`Sub().use_attr()` returns `0`, same as the clean build.\n\n**Actual Behavior**\n\n```\nTraceback (most recent call last):\n File \"\u003cstring>\", line 1, in \u003cmodule>\n from pkg.sub import Sub; Sub().use_attr()\n ~~~~~~~~~~~~~~^^\n File \"pkg/base.py\", line 6, in use_attr\n if self.ATTR_00:\nAttributeError: attribute 'ATTR_00' of 'Base' undefined\n```\n\n**Root cause (Claude-aided)**\n\n`find_attr_initializers` in `mypyc/irbuild/classdef.py` collects class-attribute default initializers by walking the MRO and iterating each entry's `info.defn.defs.body`:\n\n```python\nfor info in reversed(cdef.info.mro):\n if info not in builder.mapper.type_to_ir:\n continue\n for stmt in info.defn.defs.body:\n ...\n attrs_with_defaults.add(name)\n default_assignments.append((stmt, info.module_name))\n```\n\nThis works when the base class is in the same compilation pass as the subclass, because its `ClassDef.defs.body` is the freshly parsed AST and contains the `AssignmentStmt` nodes.\n\nIt does not, however, work when the base class is loaded from mypy's incremental cache. `ClassDef.serialize` explicitly does not serialize the body, and `ClassDef.deserialize` always reconstructs the class with an empty `Block([])`:\n\n```python\ndef serialize(self) -> JsonDict:\n # Not serialized: defs, base_type_exprs, metaclass, decorators,\n # analyzed (for named tuples etc.)\n return {\n \".class\": \"ClassDef\",\n \"name\": self.name,\n \"fullname\": self.fullname,\n \"type_vars\": [v.serialize() for v in self.type_vars],\n }\n\n@classmethod\ndef deserialize(cls, data: JsonDict) -> ClassDef:\n res = ClassDef(\n data[\"name\"],\n Block([]),\n ...\n )\n```\n\nSo for any cache-loaded class, `info.defn.defs.body == []`. `find_attr_initializers` happily iterates zero statements and produces an empty `default_assignments` list. `generate_attr_defaults_init` then emits a `__mypyc_defaults_setup` that only sets the subclass's own overrides; every inherited attribute slot stays at the undefined sentinel.\n\nThe 2-invocation pattern is what flips Base from \"freshly parsed\" to \"cache-loaded\":\n\n1. First `build_ext`: the source change to `base.py` makes mypy recheck `base`, run mypyc against the freshly parsed AST, and write the incremental cache. `sub` is fresh (its content didn't change), so it's loaded from cache and its existing `.so` is left alone.\n2. Second `build_ext`: mypy now considers `base` stable and loads it from the cache instead of re-parsing it, so `base`'s `ClassDef.defs.body` is empty. Meanwhile distutils sees `sub.py`'s newer mtime and triggers a rebuild of `sub` through mypyc. `find_attr_initializers(Sub)` walks Sub's MRO, hits cache-loaded `Base` with an empty body, and produces the broken `__mypyc_defaults_setup`.\n\n`pip wheel \u003csrc> --no-build-isolation` falls into this pattern naturally: it imports `setup.py` once for metadata (which runs `mypycify` because it sits inside `ext_modules=`) and once for the build itself. That's how we hit this in practice: `pip wheel` against a source tree whose base classes had recent edits produces wheels that crash at runtime on any read of an inherited class attribute.\n\nThe reason `Sub` needs its own override in the reproducer is that, without one, `Sub` has no class-body assignments at all, so mypyc emits no `__mypyc_defaults_setup` for it and the instance falls back to Base's via the type's MRO. The bug is still there in that case (Sub's `defaults_setup` is still missing inherited inits) but the masking effect of MRO fallback hides it at runtime.\n\n**Possible fixes**\n\n1. **mypyc — chain the call.** Make every subclass `__mypyc_defaults_setup` start with `super().__mypyc_defaults_setup(self)` and then only emit its own overrides. The subclass no longer needs to know its base's defaults at IR-build time. Inherited defaults are set at runtime by walking up the type chain, regardless of whether the base's AST is in memory. Cleanest fix and mirrors how Python's `__init__` chains already work.\n2. **mypyc — read from `ClassIR` when AST is missing.** When `info.defn.defs.body` is empty but `info in mapper.type_to_ir`, recover inherited defaults from the cached `ClassIR`. Requires the default values to live somewhere on the `ClassIR` (today only the names are stored in `attrs_with_defaults`), so this implies extending the IR serialization.\n3. **mypy — serialize the body assignments.** Persist the `AssignmentStmt` nodes for class-body attribute declarations as part of `ClassDef.serialize`. Smallest invariant change for callers but increases cache size and only addresses this particular consumer of `defn.defs.body`.\n\n**Your Environment**\n\n- Mypy version used: `2.2.0+dev.965dd31224bc2a0694e7343f927ff9c164b4b673` (master)\n- Mypy command-line flags: none directly; invoked via `mypycify([\"pkg/base.py\", \"pkg/sub.py\"], separate=True)` from `setup.py`\n- Mypy configuration options: none\n- Python version used: 3.13.12\n- OS: macOS 26.0 (arm64); also reproduces on Linux x86_64 in CI\n","author":{"url":"https://github.com/georgesittas","@type":"Person","name":"georgesittas"},"datePublished":"2026-05-26T19:02:00Z","url":"https://github.com/python/mypy/issues/21542"}
| 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:8cca5f67-8a28-2f5a-8bdc-1b5f0bd94353 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A176:5537E:1DEACCB:27909ED:6B003451 |
| html-safe-nonce | 7424b2e8376d689533477858e375bdc457b2e3c294bf1867bfd1259e3eeebe3a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTc2OjU1MzdFOjFERUFDQ0I6Mjc5MDlFRDo2QjAwMzQ1MSIsInZpc2l0b3JfaWQiOiIzODgyNTU1ODU3OTA0MzUwMjg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d6a866d2adec27d9be2f918ff806c7533630605d92b4ac70b01158facb5b407f |
| hovercard-subject-tag | issue:4526736759 |
| 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/21542/issue_layout |
| twitter:image | https://opengraph.githubassets.com/402e3c0e2d27f37c3886598d3920472e5c0455460f371454038c050bf0dec980/python/mypy/issues/21542 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/402e3c0e2d27f37c3886598d3920472e5c0455460f371454038c050bf0dec980/python/mypy/issues/21542 |
| og:image:alt | Bug Report When mypyc rebuilds a subclass and the subclass's base class is loaded from mypy's incremental cache (rather than freshly parsed), the subclass's emitted __mypyc_defaults_setup drops eve... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | georgesittas |
| 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