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

No error for identical overloaded functions #1879

Closed
shreydesai opened this issue Jul 15, 2016 · 5 comments
Closed

No error for identical overloaded functions #1879

shreydesai opened this issue Jul 15, 2016 · 5 comments

Comments

@shreydesai
Copy link
Contributor

shreydesai commented Jul 15, 2016

Mypy doesn't raise any errors for this set up:

from typing import overload

class AbstractClass(object):
    @overload
    def f(self) -> None:
        pass

    @overload
    def f(self) -> None:
        pass

By definition, overloaded methods follow the "same name, different parameters" mantra, so allowing two identical overloaded methods should not be allowed.

This could get ugly when a base class inherits from the abstract class; there would be no guarantee the right function would be called.

@gvanrossum
Copy link
Member

Actually mypy's attitude towards overloading is more subtle -- it allows overlapping argument types as long as the return types match. PEP 484 is unclear about this. If you want to, you could help by adding some clarifying text to PEP 484 to the github.com/python/peps repo. We could also use some more words about this in mypy's own docs.

@shreydesai
Copy link
Contributor Author

Glad to help! I'll add the clarifying text soon.

@shreydesai
Copy link
Contributor Author

shreydesai commented Jul 16, 2016

@gvanrossum I'm a bit confused about this; could you clarify the result of this scenario? Let's say I have this setup, where B is the base class of the class A:

from typing import overload

class A(object):
  @overload
  def f(self) -> None:
    print('Hello world 1')

  @overload
  def f(self) -> None:
    print('Hello world 2')

class B(A):
  def __init__(self) -> None:
    self.f()

b = B()

When I call self.f() in __init__ for B, which output message would I get – the first or the second? And, why does this happen; how does mypy know which function to call?

@gvanrossum
Copy link
Member

Overloading is only for stubs.

On Saturday, July 16, 2016, Shrey Desai notifications@github.com wrote:

@gvanrossum https://github.com/gvanrossum I'm a bit confused about
this; could you clarify the result of this scenario? Let's say I have this
setup, where B is the base class of the class A:

from typing import overload
class A(object):
@overload
def f(self) -> None:
print('Hello world 1')

@overload
def f(self) -> None:
print('Hello world 2')
class B(A):
def init(self) -> None:
self.f()

When I call self.f() in init for B, which output message would I get
– the first or the second? And, why does this happen; how does mypy
know which function to call?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1879 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACwrMp7bbYSJy08qFQ8z1RZIk-lvWGa6ks5qWVMCgaJpZM4JN2Dl
.

--Guido (mobile)

@Michael0x2a
Copy link
Collaborator

As of f61c2ba, mypy will now report the following error message for the above code snippet: "Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader".

We could maybe improve the error message so it gives more customized feedback when the signatures are literally identical, but tbh I think this is clear enough. Feel free to reopen if anybody disagrees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants