Skip to content

Fix tuple structuring of keyword-only fields - #808

Open
leeshengis wants to merge 2 commits into
python-attrs:mainfrom
leeshengis:fix/234-kw-only-tuple-structure
Open

leeshengis wants to merge 2 commits into
python-attrs:mainfrom
leeshengis:fix/234-kw-only-tuple-structure

Conversation

@leeshengis

Copy link
Copy Markdown

Closes #234.

structure_attrs_fromtuple currently passes every field to the constructor positionally, so attrs classes and dataclasses with keyword-only fields raise TypeError.

Keep conversion in tuple field order, but pass keyword-only values by their constructor names. Use attrs aliases (including private-field aliases), with the field name as the fallback for dataclasses. Positional arguments, omitted defaults and attribute-converter precedence retain their existing behavior.

Regression coverage includes both converter classes, class-wide and mixed keyword-only fields, inheritance, aliases, defaults and type conversion. The existing tuple property test now also generates keyword-only fields. A changelog entry is included.

Validation against main at 57333a2:

  • New regressions on the original code: 22 failed, 8 passed. With the fix, all 30 pass, along with the existing tuple property test.
  • Full test suite with the locked dependencies and optional test extras: Python 3.10: 1044 passed, 7 skipped, 15 xfailed; 3.11: 1051 passed, 15 xfailed; 3.12 and 3.13: 1089 passed, 15 xfailed each; 3.14: 1116 passed, 15 xfailed, including Mypy/Pyrefly typing tests.
  • The original issue's inheritance example round-trips through in-memory SQLite with both BaseConverter and Converter.
  • Relevant tests also pass with attrs 26.1.0 (57 passed); the locked version is attrs 25.4.0.
  • Repository Ruff lint and format checks, and git diff --check, pass.

Python 3.15, PyPy, performance benchmarks and upstream GitHub CI have not been run locally. This change does not address postponed annotations (#785) or init=False fields.

AI assistance was used for investigation, implementation and testing. I reviewed and approved this patch before submission and take responsibility for the contribution under the project's AI policy. There are no LLM co-author tags.

Preserve tuple field order while using constructor aliases for
keyword-only parameters. Add attrs and dataclass regression tests
and include keyword-only classes in the tuple property test.

Fixes python-attrs#234.

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.

kw_only tuple support?

1 participant