Fix `@dataclass` to catch default / non-default fields order
Closes https://github.com/python/mypy/issues/12137
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
So, here's what happens with overriding kw-only with a regular field.
from dataclasses import dataclass, field
@dataclass
class Some:
x: int = field(kw_only=True)
@dataclass
class Other(Some):
x: int
print(Other(1)) # Other(x=1)
Two fields (there was a difference in the original bug report):
from dataclasses import dataclass, field
@dataclass
class Some:
x: int = field(kw_only=True)
@dataclass
class Other(Some):
x: int
y: int
print(Other(1, 2))
Mypy is also happy with this. So, I've just added a test case to cement this behavior (which I believe is the correct one).
Funny thing
But, while testing this, I found a different problem:
from dataclasses import dataclass, field, KW_ONLY
@dataclass
class Base:
x: int
y: int = field(kw_only=True)
_: KW_ONLY
z: int = 0
@dataclass
class Other(Base): # E: Attributes without a default cannot follow attributes with one
x: int
y: int
z: int
I will open a new issue for it and address in a separate PR (if this is a problem at all, some more analysis is needed).
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
This is a new regression: https://github.com/python/mypy/issues/12173
It is not happening on master
This still has a couple of regressions 😢 I will fix them later.