Skip to content

Match filter attributes by identity#1559

Open
Sean-Kenneth-Doherty wants to merge 1 commit into
python-attrs:mainfrom
Sean-Kenneth-Doherty:fix-filter-attribute-identity
Open

Match filter attributes by identity#1559
Sean-Kenneth-Doherty wants to merge 1 commit into
python-attrs:mainfrom
Sean-Kenneth-Doherty:fix-filter-attribute-identity

Conversation

@Sean-Kenneth-Doherty
Copy link
Copy Markdown

Summary

Fixes #864 by making attrs.filters.include() and attrs.filters.exclude() match attrs.Attribute objects by identity instead of equality.

Root Cause

attrs.Attribute equality does not carry owning-class identity, so fields with the same name and otherwise equal metadata on different classes compare equal. Passing attrs.fields(SomeClass).field to a filter could therefore include or exclude same-named fields from unrelated classes during recursive asdict() conversion.

Changes

  • Preserve Attribute filter arguments as an ordered tuple instead of a frozenset.
  • Match Attribute arguments with is identity checks.
  • Keep string-name filtering as the explicit way to match fields by name across classes.
  • Add regression tests for both include() and exclude().
  • Update the examples docs and add a changelog fragment.

Validation

  • .venv/bin/python -m pytest -q tests/test_filters.py -> 35 passed
  • .venv/bin/python -m pytest -q tests/test_filters.py -k "identity or split_what" -> 2 passed, 33 deselected
  • .venv/bin/python -m pytest -q -> 1387 passed, 5 skipped, 1 xfailed
  • .venv/bin/ruff check src/attr/filters.py tests/test_filters.py docs/examples.md
  • .venv/bin/ruff format --check src/attr/filters.py tests/test_filters.py
  • .venv/bin/python -m compileall -q src/attr/filters.py tests/test_filters.py
  • git diff --check origin/main...HEAD

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