Skip to content

Move dataclass kw_only fields to the end of the signature #19018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented May 3, 2025

Fixes #19017. Fixes #17731.

This is a rather naive change: python does that at runtime. kw_only args can be in any order, and non-kwonly args should remain sorted as-is (stable sort). I don't understand why this was only done in presence of a parent dataclass - AFAIC kwonly fields work that way since kw_only was introduced in py3.10.

The test I changed was invalid and asserted a false positive to the best of my knowledge.

Copy link
Contributor

github-actions bot commented May 3, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@sterliakov sterliakov marked this pull request as ready for review May 3, 2025 16:33
@sterliakov sterliakov requested a review from A5rocks May 3, 2025 16:34
Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

I find the found_dataclass_supertype logic confusing -- why was that there?

Because this makes sense.

@sterliakov
Copy link
Collaborator Author

Given that we had a test asserting such behavior, I suspect #13539 was just a rushed implementation with some cases mistreated or misunderstood. I can't find any explanation to sorting the attributes only if there was a parent class - perhaps the author assumed that kw_only fields must be in trailing positions only and consequently skipped sorting as a perf optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants