8000 Support correct `__post_init__` signatures for dataclasses · Issue #15498 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Support correct __post_init__ signatures for dataclasses #15498

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
sobolevn opened this issue Jun 23, 2023 · 0 comments · Fixed by #15503
Closed

Support correct __post_init__ signatures for dataclasses #15498

sobolevn opened this issue Jun 23, 2023 · 0 comments · Fixed by #15503

Comments

@sobolevn
Copy link
Member

Feature

Right now mypy does not support proper __post_init__ signatures.
For example:

import dataclasses

@dataclasses.dataclass
class My:
    x: int
    y: dataclasses.InitVar[str] = 'a'

For now, mypy will allow a lot of incorrect code to slip in:

  • def __post_init__(self, y: str) -> str: ..., which needs to return None
  • def __post_init__(self, y: int) -> None: ..., which has incorrect type for y
  • def __post_init__(self) -> None: ..., which has no y
  • def __post_init__(self, x: int, y: str) -> None: ..., which has extra x param

Pitch

We can easily support this feature and make sure that __post_init__ signature is always correct for a dataclass.

@sobolevn sobolevn self-assigned this Jun 23, 2023
sobolevn added a commit that referenced this issue Jun 26, 2023
…5503)

Now we use a similar approach to
#14849
First, we generate a private name to store in a metadata (with `-`, so -
no conflicts, ever).
Next, we check override to be compatible: we take the currect signature
and compare it to the ideal one we have.

Simple and it works :)

Closes #15498
Closes #9254

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant
0