Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix @dataclass to catch default / non-default fields order #12159

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

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

Fix @dataclass to catch default / non-default fields order #12159

wants to merge 4 commits into from

Conversation

sobolevn
Copy link
Member

Closes #12137

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

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).

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@sobolevn
Copy link
Member Author

This is a new regression: #12173
It is not happening on master

b: int = 2

@dataclass
class Wrong1(A): # E: Attributes without a default cannot follow attributes with one
Copy link
Member

Choose a reason for hiding this comment

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

Interestingly the runtime accepts this one. It's pretty weird, though, and I'm OK with mypy giving an error for it.

@sobolevn sobolevn marked this pull request as draft February 15, 2022 05:30
@sobolevn
Copy link
Member Author

This still has a couple of regressions 😢
I will fix them later.

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

Successfully merging this pull request may close these issues.

(🐞) false negative with dataclass "non-default argument follows default argument"
4 participants