René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:8cca5f67-8a28-2f5a-8bdc-1b5f0bd94353
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA176:5537E:1DEACCB:27909ED:6B003451
html-safe-nonce7424b2e8376d689533477858e375bdc457b2e3c294bf1867bfd1259e3eeebe3a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTc2OjU1MzdFOjFERUFDQ0I6Mjc5MDlFRDo2QjAwMzQ1MSIsInZpc2l0b3JfaWQiOiIzODgyNTU1ODU3OTA0MzUwMjg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacd6a866d2adec27d9be2f918ff806c7533630605d92b4ac70b01158facb5b407f
hovercard-subject-tagissue:4526736759
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/21542/issue_layout
twitter:imagehttps://opengraph.githubassets.com/402e3c0e2d27f37c3886598d3920472e5c0455460f371454038c050bf0dec980/python/mypy/issues/21542
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/402e3c0e2d27f37c3886598d3920472e5c0455460f371454038c050bf0dec980/python/mypy/issues/21542
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegeorgesittas
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/21542#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F21542
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F21542
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/21542
Reloadhttps://github.com/python/mypy/issues/21542
Reloadhttps://github.com/python/mypy/issues/21542
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests546 (546)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/21542
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/21542
#21547https://github.com/python/mypy/pull/21547
[mypyc] Subclass __mypyc_defaults_setup drops inherited class-attribute initializers across incremental buildshttps://github.com/python/mypy/issues/21542#top
#21547https://github.com/python/mypy/pull/21547
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
topic-mypycmypyc bugshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-mypyc%22
https://github.com/georgesittas
georgesittashttps://github.com/georgesittas
on May 26, 2026https://github.com/python/mypy/issues/21542#issue-4526736759
georgesittashttps://github.com/georgesittas
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
on May 26, 2026https://github.com/python/mypy/issues/21542#event-25982093317
https://github.com/georgesittas
georgesittashttps://github.com/georgesittas
on May 26, 2026https://github.com/python/mypy/issues/21542#issuecomment-4547718340
@p-sawickihttps://github.com/p-sawicki
georgesittashttps://github.com/georgesittas
on May 26, 2026https://github.com/python/mypy/issues/21542#event-25982391043
https://github.com/p-sawicki
p-sawickihttps://github.com/p-sawicki
on May 27, 2026https://github.com/python/mypy/issues/21542#issuecomment-4553916058
p-sawickihttps://github.com/p-sawicki
topic-mypycmypyc bugshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-mypyc%22
on May 27, 2026https://github.com/python/mypy/issues/21542#event-26011335079
https://github.com/georgesittas
georgesittashttps://github.com/georgesittas
on May 27, 2026https://github.com/python/mypy/issues/21542#issuecomment-4553990855
p-sawickihttps://github.com/p-sawicki
on Jun 3, 2026https://github.com/python/mypy/issues/21542#event-26296465395
[mypyc] Preserve inherited attribute defaults under separate=True (#2…https://github.com/python/mypy/commit/52de0c739d2f066ea06fa9a886bc8fcc9fa079c6
#2…https://github.com/python/mypy/pull/21547
https://docs.github.com/articles/closing-issues-via-commit-messages
52de0c7https://github.com/python/mypy/commit/52de0c739d2f066ea06fa9a886bc8fcc9fa079c6
p-sawickihttps://github.com/p-sawicki
completedhttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#21547https://github.com/python/mypy/pull/21547
on Jun 3, 2026https://github.com/python/mypy/issues/21542#event-26296465501
georgesittashttps://github.com/georgesittas
on Jun 4, 2026https://github.com/python/mypy/issues/21542#event-26338911249
[mypyc] Preserve inherited attribute defaults under separate=True (py…https://github.com/VaggelisD/sqlglot-mypy/commit/7500bf1364934690588a6cac13861ca4bc14e4cb
py…https://github.com/python/mypy/pull/21547
https://docs.github.com/articles/closing-issues-via-commit-messages
7500bf1https://github.com/VaggelisD/sqlglot-mypy/commit/7500bf1364934690588a6cac13861ca4bc14e4cb
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/21542
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/21542
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
topic-mypycmypyc bugshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-mypyc%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.