Skip to content

[ty] Respect third-party keyword-only fields before Python 3.10 - #27699

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/fix-attrs-kw-only-field-order
Aug 12, 2026
Merged

[ty] Respect third-party keyword-only fields before Python 3.10#27699
charliermarsh merged 2 commits into
mainfrom
charlie/fix-attrs-kw-only-field-order

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

Previously, we ignored kw_only=True on every dataclass field specifier when targeting Python 3.9, even though libraries such as attrs support keyword-only fields independently of the standard library:

import attr

@attr.s(auto_attribs=True)
class Base:
    optional: float = attr.ib(default=1.0, kw_only=True)

@attr.s(auto_attribs=True)
class Child(Base):
    required: str

We now restrict the Python 3.10 requirement to dataclasses.field, preserving keyword-only behavior for third-party field specifiers on earlier Python versions. Inherited keyword-only fields no longer trigger dataclass-field-order, and synthesized constructors retain the correct argument ordering.

Closes astral-sh/ty#4242.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Aug 12, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 96.98%. The percentage of expected errors that received a diagnostic held steady at 93.62%. The number of fully passing files held steady at 106/133.

@astral-sh-bot

astral-sh-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@charliermarsh
charliermarsh marked this pull request as ready for review August 12, 2026 18:05
@charliermarsh
charliermarsh requested a review from a team as a code owner August 12, 2026 18:05
@astral-sh-bot
astral-sh-bot Bot requested a review from carljm August 12, 2026 18:05
@codspeed-hq

codspeed-hq Bot commented Aug 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 111 untouched benchmarks
⏩ 84 skipped benchmarks1


Comparing charlie/fix-attrs-kw-only-field-order (d73a8ed) with main (4d261dd)

Open in CodSpeed

Footnotes

  1. 84 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@carljm carljm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! We could consider an actual-attrs integration test, but I don't think it's necessary, assuming you validated this does actually fix the OP case with attrs under Python 3.9.

@carljm

carljm commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Codex observes that we actually don't currently have any test using dataclasses.field(kw_only=True) on Python 3.9, which would cover one possible case of this conditional that is otherwise not covered.

@charliermarsh
charliermarsh merged commit 5b2ed0d into main Aug 12, 2026
64 checks passed
@charliermarsh
charliermarsh deleted the charlie/fix-attrs-kw-only-field-order branch August 12, 2026 18:40
George-Ogden pushed a commit to George-Ogden/ruff that referenced this pull request Aug 16, 2026
…al-sh#27699)

## Summary

Previously, we ignored `kw_only=True` on every dataclass field specifier
when targeting Python 3.9, even though libraries such as `attrs` support
keyword-only fields independently of the standard library:

```python
import attr

@attr.s(auto_attribs=True)
class Base:
    optional: float = attr.ib(default=1.0, kw_only=True)

@attr.s(auto_attribs=True)
class Child(Base):
    required: str
```

We now restrict the Python 3.10 requirement to `dataclasses.field`,
preserving keyword-only behavior for third-party field specifiers on
earlier Python versions. Inherited keyword-only fields no longer trigger
`dataclass-field-order`, and synthesized constructors retain the correct
argument ordering.

Closes astral-sh/ty#4242.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spurious dataclass-field-order failure with attrs kw_only

2 participants