Skip to content

Collect class-body attributes when these= is passed - #1673

Open
adityawrk wants to merge 2 commits into
python-attrs:mainfrom
adityawrk:these-class-body
Open

adityawrk wants to merge 2 commits into
python-attrs:mainfrom
adityawrk:these-class-body

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

attr.s(these=...) ignored attr.ib() attributes written on the class and left the private sentinel on the class. Reading the attribute then returned that sentinel instead of the default.

Class-body attributes are now fields, after the these dictionary. A name that appears in both places keeps the these definition. Attributes on base classes are still ignored. The sentinels are removed. With auto_attribs=True, annotated class-body attributes are collected too.

@attr.s(these=dict(a=attr.ib()))
class test:
    b: int = attr.ib(default=0)

test(5).b  # 0

Fixes #621

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.

attr.s(these=...) ignored attr.ib() on the class and left the private
sentinel in place, so reading the attribute returned that sentinel
instead of the default. Collect those attributes after these, let these
win on a name clash, and remove the sentinels.

Fixes python-attrs#621
CI requires 100% coverage. Those branches were not exercised, and the sentinel cleanup checked a base-class condition it can never hit.
@adityawrk

Copy link
Copy Markdown
Author

The Read the Docs failure is in the uv install, before the docs build runs.

asdf install uv latest printed an empty redirect URL and No compatible versions available (uv [0-9]). asdf global uv latest then exited 1 because 0.13.10 was not installed. The previous commit on this pull request, 0ade4d5, built the docs successfully about nine minutes earlier, and this change does not touch .readthedocs.yaml.

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.

Passing these=d to attr.s skips attributes defined in class body

1 participant