Skip to content
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

Improve error handling for dataclass inheritance #13531

Merged
merged 2 commits into from Aug 27, 2022

Conversation

Michael0x2a
Copy link
Collaborator

@Michael0x2a Michael0x2a commented Aug 27, 2022

This pull request:

  1. Fixes Crash when overriding field as property/method on frozen derived class #8334. Overriding a dataclass attribute with a method or property now results in an error message, not a crash.

    (Overriding an attribute with a non-attribute at runtime will result in either inconsistent behavior or an exception, so I think unconditionally disallowing this is fine.)

  2. Makes mypy report an error if you try subclassing a frozen dataclass with a non-frozen one or vice versa. Attempting to do this subclassing at runtime will raise a TypeError.

This pull request:

1.  Fixes python#8334. Overriding a dataclass attribute with a method or
    property now results in an error message, not a crash.

    (Overriding an attribute with a non-attribute at runtime will
    result in either inconsistent behavior or an exception, so
    I think unconditionally disallowing this is fine.)

2.  Makes mypy report an error if you try subclassing a frozen
    dataclass with a non-frozen one or vice versa. Attempting to
    do this subclassing at runtime will raise a TypeError.
@Michael0x2a
Copy link
Collaborator Author

Michael0x2a commented Aug 27, 2022

I'm not too enthused about the new error message for (1) -- it's a bit cryptic imo. I'm open to bikeshedding there.

@github-actions

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Aug 27, 2022

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

JukkaL
JukkaL approved these changes Aug 27, 2022
Copy link
Collaborator

@JukkaL JukkaL left a comment

Thanks for the PR! Looks good. The error message is a little cryptic, but I couldn't quickly think of a clearly better one. We can update the error message later if somebody comes up with a better idea.

@JukkaL JukkaL merged commit 3f0ac21 into python:master Aug 27, 2022
18 checks passed
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.

2 participants