Skip to content

fix: compare Attribute instances by identity in include/exclude - #1616

Open
TrueFurina wants to merge 2 commits into
python-attrs:mainfrom
TrueFurina:fix/filter-attribute-identity
Open

fix: compare Attribute instances by identity in include/exclude#1616
TrueFurina wants to merge 2 commits into
python-attrs:mainfrom
TrueFurina:fix/filter-attribute-identity

Conversation

@TrueFurina

Copy link
Copy Markdown

Summary

Fixes #864: exclude(fields(A).foo) / include(fields(A).foo) matched Attribute instances by equality, so two classes sharing a same-named attribute (e.g. repeated_field_name) would both be affected, even though only one was passed to the filter.

Attribute instances are independent from the classes they are defined on, and == compares their contents — so a same-named attribute on another class compared equal. The fix compares by identity (is), matching exactly the attribute instance the caller passed.

Changes

  • src/attr/filters.py: attribute in attrsany(attribute is a for a in attrs) in both include_ and exclude_
  • tests/test_filters.py: add TestSameNameAcrossClasses with C2/D2 sharing a repeated attribute, asserting exclusion/inclusion of one class's attribute does not affect the other

Verification

  • Reproduced locally against attrs 26.1.0: before the fix, exclude(fields(C2).repeated) also rejected fields(D2).repeated (cross-class false positive)
  • After the fix: exclude(fields(C2).repeated) rejects C2.repeated but allows D2.repeated; include behaves symmetrically
  • ast.parse passes on both files; existing TestInclude/TestExclude parametrized cases are unaffected (single-class scenarios compare identically)

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.

asdict filter exclude list matches on name instead of attribute

1 participant