Skip to content

Implementation for validation checks on functions that can only be called on dataclasses #14286

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

Closed
wants to merge 2 commits into from

Conversation

awutis
Copy link

@awutis awutis commented Dec 12, 2022

Fixes #14215
Description:
As mentioned in the issue, methods such as replace() and field() should only be called on dataclass instances. In this implementation, I added a callback to detect such issues.

Testing:
I did not edit the test file in this commit - one of my teammates will in the near future.
For local testing, when I tested my implementation against

[[case testNoCrashOnReplace]
from dataclasses import dataclass
from dataclasses import replace

@dataclass
class D:
    pass
class C:
    pass

replace(D())
[builtins fixtures/dataclasses.pyi]]

I got the output of:
image

When I tested my implementation against

[case testNoCrashOnReplace]
from dataclasses import dataclass
from dataclasses import replace

@dataclass
class D:
    pass
class C:
    pass

replace(C())  # replace() should be called on dataclass instances
[builtins fixtures/dataclasses.pyi]

I got this output, as desired:
image

@awutis awutis changed the title implemented error check for functions not called on dataclass instances Implementation for validation checks on functions that can only be called on dataclasses Dec 12, 2022
@awutis awutis marked this pull request as ready for review December 13, 2022 22:25
@AlexWaygood
Copy link
Member

AlexWaygood commented Dec 14, 2022

Thanks for the PR! Unfortunately it looks like we've already had a PR filed to fix this issue (#14263) ☹️

It's always best to check that before you start work on a PR -- you can see linked PRs in the "Development" tab on the right-hand side when you're looking at a GitHub issue (assuming you're on a desktop). The other PR looks more complete (it also deals with astuple and asdict), and was also filed at an earlier date, so I'll close this in favour of that one. (There's also discussion in that PR about whether it might be better to fix this in typeshed.)

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.

(🎁) Validate arguments to dataclasses utility functions replace and fields
2 participants