Skip to content

fix: preserve explicitly declared slots - #1638

Open
anshurajbisoyi98-ctrl wants to merge 3 commits into
python-attrs:mainfrom
anshurajbisoyi98-ctrl:fix-1637-preserve-explicit-slots
Open

anshurajbisoyi98-ctrl wants to merge 3 commits into
python-attrs:mainfrom
anshurajbisoyi98-ctrl:fix-1637-preserve-explicit-slots

Conversation

@anshurajbisoyi98-ctrl

Copy link
Copy Markdown

Summary

Fixes #1637.

Problem

When attrs rebuilds a slotted class, explicitly declared slots are omitted from the new class layout, but their descriptors still reference the original class.
Assigning to those slots then raises TypeError.

What I changed

The fix collects the class's own slot names, removes the original descriptors, and includes those names in the rebuilt layout.
Explicit slots stay outside the attrs data model unless they also correspond to fields.

The documentation now explains that values in unmanaged slots are not included in generated serialization.
It includes a version-change note for 26.2.0.

Tests

I added 11 regression cases covering:

  • String, tuple, and dictionary slot declarations.
  • Private-name mangling.
  • Inheritance.
  • __dict__ and weak-reference slots.
  • Overlap between explicit slots and attrs fields.

All 11 cases failed before the fix and passed afterward.

Validation

  • Full local suite: 1,418 passed, 4 skipped, 2 expected failures.
  • Python 3.10 and 3.14 tox test environments passed.
  • Combined statement and branch coverage: 100%.
  • Documentation doctests/build, pre-commit hooks, and Ruff passed.
  • The documentation build passed again after adding the version-change note.

AI assistance

Codex assisted with investigation, implementation, tests, verification and documentation.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing_tests/baseline.py or, if necessary, typing_tests/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      The next version is the second number in the current release + 1.
      The first number represents the current year.
      So if the current version on PyPI is 26.2.0, the next version is gonna be 26.3.0.
      If the next version is the first in the new year, it'll be 27.1.0.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

Type-stub, API-listing, and signature checklist items are not applicable because no public API signatures or exports changed.

Signed-off-by: Anshu Raj Bisoyi <anshurajbisoyi98@gmail.com>
Signed-off-by: Anshu Raj Bisoyi <anshurajbisoyi98@gmail.com>
Signed-off-by: Anshu Raj Bisoyi <anshurajbisoyi98@gmail.com>

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.

slots declared on attrs-decorated class are ignored/lost

1 participant