-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Overriding __post_init__ in child dataclass leads to type error #9254
Comments
Interesting. I guess for now you can silence mypy with `# type: ignore` but
you're right, ideally this ought to be special-cased. Truth be told there
are probably some more magical functions in the stdlib for which we
shouldn't insist on Liskov. I'm not sure where to go from here -- maybe you
can find where `__init__` is special-cased in mypy and see how easy it
would be to add `__post_init__`? (Luckily it's a dunder so we can just
assume that it always has this property, not just when the class is
decorated with `@dataclass`.)
|
Can a general solution be added, like a decorator that you can use in a base class method to not require Liskov in the subclass overrides? Because sometimes it is useful to have a special "startup" or "run" method that initializes the object and should be different for each subclass. |
@vnmabus I think that |
Since I am working on #15498 I will also have a look at this one. |
…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>
Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
Please provide more information to help us understand the issue:
Consider the following test file:
Running mypy results in the following error:
I understand from #1237 that generally it is an error to have subclass methods that differ in this way. But it seems like
__post_init__
should not fall into this category, and instead should be treated like__init__
which does not have any type checking error (as the example file also demonstrates with OtherParent/OtherChild).The text was updated successfully, but these errors were encountered: