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
@overload-ing method of parent class without actual implementation #86978
Comments
Why should @overload need to be followed by an implementation when an implementation already exists in the parent class? Illustrative example: class Parent:
def foo(**kwargs):
"""Argument names of foo vary depending on the child class."""
class Child(Parent):
@overload foo(a, b): ...
Raises: "NotImplementedError: You should not call an overloaded function. A series of @overload-decorated functions outside a stub module should always be followed by an implementation that is not @overload-ed." |
The purpose of Basically, the correct usage of
Please, follow this link for more information https://docs.python.org/3/library/typing.html#typing.overload |
"The purpose of Is there a better way to accomplish this goal? What would you suggest, a new decorator? |
To clarify, this is how it's being done now a dozen times in actual production code: class Child(Parent):
@overload foo(a, b): ...
def overload(**kwargs):
return super().foo(**kwargs) The goal of this proposed enhancement is to remove two extra lines of code per Child class. |
Result
In case if you want to add an overload for method I would recommend to use such pattern:
So signature of |
Interesting. PyCharm has no problem with this code. It also autocompletes the argument names for me, which is very useful, especially since there a dozen different Child classes. Isn't "Simple is better than complex", and doesn't "...practicality beat purity"? |
I think the simplest solution in your case is not to use This code won't produce any error when used with
|
What is better? A. Keeping Python as is, with four separate signature declarations (1x Parent, 2x @overload Child, and 1x actual signature in Child), and a second method call overhead at runtime (to satisfy mypy as it exists now). --or-- B. Simplify Python to allow just two signature declarations and no extra overhead? --or-- C. Something else? Please specify. |
In your example, does Child foo call Parent foo? Because the intent is to use the parent's foo method. |
Sorry, I can't answer this question. I am a regular python user and I just tried to help with your issue. I believe we should wait for someone from core team to answer this question.
Sorry, I made a mistake, it definitely should call a parent method. A correct example will look like this:
But, the example above require more code to write, so a better option will be:
I am not sure is it the perfect solution to solve your issue. Let's wait for someone from core team, so we can hear their opinion. |
Thanks for your perspective. To summarize here is how my proposed enhancement might look in practice: class Parent:
def foo(self, **kwargs):
"""Argument names of foo vary depending on the child class."""
class Child(Parent):
@overload
def foo(self, a, b):
pass |
I hesitate to add anything because you are exposing so much confusion. May I suggest that you ask about this on a user group first before proposing a new feature? One place that makes sense given that this is a type system feature would be this Gitter channel: https://gitter.im/python/typing |
I agree with @gvanrossum. I'm closing this issue, as I don't think this issue tracker is an appropriate place for this conversation to be held. Please consider asking your question at https://github.com/python/typing/discussions or on StackOverflow. Cc. @chaim422 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: