Uh oh!
There was an error while loading. Please reload this page.
Move dataclass kw_only fields to the end of the signature - #19018
Conversation
This comment has been minimized.
This comment has been minimized.
A5rocks
left a comment
There was a problem hiding this comment.
I find the found_dataclass_supertype logic confusing -- why was that there?
Because this makes sense.
sterliakov
commented
May 3, 2025
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 |
NeilGirdhar
commented
May 9, 2025
Will this PR also fix #17564? |
No, it won't, because # Follows the "jumping" logic of kw_onlydef__init__(self, y: str, *, x: int): ...
# Definition order, reverse MRO order, no keywordsdef__post_init__(self, x: int, y: str, /): ...(a bit more difficult with defaults, but similar still)
|
NeilGirdhar
commented
May 9, 2025
Thanks for explaining! |
sterliakov
commented
May 9, 2025
#17564 is an easy fix, let's merge this and I will open a follow-up PR. |
…9017-dataclass-fields-kwonly-order
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes#19017. Fixes#17731.
This is a rather naive change: python does that at runtime.
kw_onlyargs 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 sincekw_onlywas introduced in py3.10.The test I changed was invalid and asserted a false positive to the best of my knowledge.