Skip to content

Commit bb67682

Browse files
committed
adds subclassing test case
To account for the reordering of keyword only parameters `all_attrs` is sorted based on the attr.kw_only value
1 parent 1515be7 commit bb67682

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mypy/plugins/dataclasses.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def serialize(self) -> JsonDict:
7272
'line': self.line,
7373
'column': self.column,
7474
'type': self.type.serialize(),
75+
'kw_only': self.kw_only,
7576
}
7677

7778
@classmethod
@@ -335,6 +336,7 @@ def collect_attributes(self) -> Optional[List[DataclassAttribute]]:
335336
super_attrs.append(attr)
336337
break
337338
all_attrs = super_attrs + all_attrs
339+
all_attrs.sort(key=lambda a: a.kw_only)
338340

339341
# Ensure that arguments without a default don't follow
340342
# arguments that have a default.

test-data/unit/check-dataclasses.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ class Application:
368368

369369
[builtins fixtures/list.pyi]
370370

371+
[case testDataclassesOrderingKwOnlyWithSentinelAndSubclass]
372+
# flags: --python-version 3.10
373+
from dataclasses import dataclass, field, KW_ONLY
374+
375+
@dataclass
376+
class Base:
377+
x: str
378+
_: KW_ONLY
379+
y: int = 0
380+
w: int = 1
381+
382+
@dataclass
383+
class D(Base):
384+
z: str
385+
a: str = "a"
386+
387+
[builtins fixtures/list.pyi]
388+
371389
[case testDataclassesClassmethods]
372390
# flags: --python-version 3.7
373391
from dataclasses import dataclass

0 commit comments

Comments
 (0)