Skip to content

Include inherited fields in slotted dataclass formatting - #181

Open
rupayon123 wants to merge 2 commits into
samuelcolvin:mainfrom
rupayon123:fix/inherited-slotted-dataclass-fields-20261029
Open

rupayon123 wants to merge 2 commits into
samuelcolvin:mainfrom
rupayon123:fix/inherited-slotted-dataclass-fields-20261029

Conversation

@rupayon123

@rupayon123 rupayon123 commented Oct 29, 2026 •

Copy link
Copy Markdown

Formatting a fully slotted dataclass subclass only enumerates its leaf __slots__, so inherited dataclass fields disappear. A child with no new slots can print as empty despite holding inherited values.

Add missing dataclass field names to the existing no-__dict__ fallback, then retain the leaf slots in their original order. This preserves manually declared auxiliary slots, explicit weakref slots and existing ordering; string-form slot metadata is treated as one name. Classes with an instance __dict__ keep their current behavior. Genuinely absent slot attributes are omitted, so inherited field(init=False, repr=False) values are safe before assignment. Assigned values, including the formatter’s own MISSING object, remain visible.

Validation on CPython 3.13.14 / macOS arm64:

  • The final 53 formatter cases reproduce five failures on pristine source and all pass with this change.
  • Complete suites pass with extras (162 passed, four existing skips) and without extras (155 passed, 11 existing skips).
  • Ruff, strict basedpyright and strict docs pass.
  • Public API controls cover multilevel inheritance, overridden fields, empty child slots, manual slot order/auxiliary values, InitVar/ClassVar, dynamic attributes and the unchanged hybrid-class path.

Other supported runtime/OS combinations remain unverified locally. This only changes the existing fully slotted fallback; it does not add previously hidden inherited non-field auxiliary slots.


Summary by cubic

Fixes formatting of fully slotted dataclass subclasses so inherited fields no longer disappear from pformat() output.

Bug Fixes

  • Adds dataclass field names missing from the leaf __slots__ to the no-__dict__ fallback, matching dataclass field ordering.
  • Skips slots whose attributes are genuinely unset instead of raising AttributeError.
  • Preserves manually declared auxiliary slots and explicit weakref slots; string-form slot metadata is treated as one name.
  • Classes with an instance __dict__ keep their current behavior.

Written for commit 40698a1. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread devtools/prettier.py Resolved

This branch has not been deployed

No deployments
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.

1 participant