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

singledispatch chain gives type errors #3207

Closed
lopuhin opened this issue Apr 21, 2017 · 3 comments
Closed

singledispatch chain gives type errors #3207

lopuhin opened this issue Apr 21, 2017 · 3 comments
Labels
bug mypy got something wrong

Comments

@lopuhin
Copy link

lopuhin commented Apr 21, 2017

I believe this is different from other issues about singledispatch (#2904).

This is the program that does not type-check:

from singledispatch import singledispatch

class A:
    pass

class B(A):
    pass

@singledispatch
def base(x, **kw):
    pass

@base.register(A)
@singledispatch
def a(x):
    print('a')

@a.register(B)
def b(x):
    print('b')

base(A())
# want "base" and "a" both dispatch to "b"
base(B())
a(B())

Expected output from running it:

a
b
b

running mypy 0.501 with Python 3.5 gives the following errors:

t.py:13: error: Argument 1 has incompatible type _SingleDispatchCallable[Any]; expected Callable[..., Any]
t.py:18: error: Callable[..., Any] has no attribute "register"

Next is just the motivation part - why do we need this :)

Why such a complicated program: I want both "base" and "a" dispatch to "b", because "base" is a more generic function that works for any type supported by our library, and "a" is less generic, and all functions that dispatch from "a" share a common signature. So the user can import "a" and get more help from the IDE (and mypy) about supported arguments:

@singledispatch
def base(x, **kwargs):
    pass

@base.register(A)
@singledispatch
def a(x, foo=None):
    print('a')

@a.register(B)
def b(x, foo=None):
    print('b')

Real code that fails to typecheck is here: https://github.com/TeamHG-Memex/eli5/blob/724c63a9b97e44eef8264305e66e808d249810c4/eli5/sklearn/explain_weights.py#L114

@lopuhin
Copy link
Author

lopuhin commented Apr 21, 2017

But there is a workaround that works in almost the same way (for us it's good enough) and typechecks:

from singledispatch import singledispatch

class A:
    pass

class B(A):
    pass

@singledispatch
def base(x, **kwargs):
    print('base')

@singledispatch
def a(x, foo=None):
    print('a')

def register(cls):
    def deco(f):
        return base.register(cls)(a.register(cls)(f))
    return deco

@register(B)
def b(x, foo=None):
    print('b')

base(A())
# want "base" and "a" both dispatch to "b"
base(B())
a(B())

@graingert
Copy link
Contributor

see #2904

@AlexWaygood
Copy link
Member

Cannot reproduce on 0.920+; this appears to now be fixed.

@AlexWaygood AlexWaygood added the bug mypy got something wrong label Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants