Skip to content

How can the stub for functools.partial be made to work? #1104

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
gvanrossum opened this issue Jan 8, 2016 · 2 comments
Closed

How can the stub for functools.partial be made to work? #1104

gvanrossum opened this issue Jan 8, 2016 · 2 comments
Labels
bug mypy got something wrong

Comments

@gvanrossum
Copy link
Member

The 2.7 stub for functools contains this elaborate definition for functools.partial:

class partial(Generic[_T]):
    func = ...  # Callable[..., _T]
    args = ...  # type: Tuple[Any, ...]
    keywords = ...  # type: Dict[str, Any]
    def __init__(self, func: Callable[..., _T], *args: Any, **kwargs: Any) -> None: ...
    def __call__(self, *args: Any, **kwargs: Any) -> _T: ...

However, despite the presence of a __call__ method, this isn't compatible with Callable -- e.g. this gives an error:

from typing import *
from functools import partial
def foo(a: int) -> int:
    return a
bar = partial(foo, 42)  # type: Callable[[], int]

The error is on the last line:

t.py:5: error: Incompatible types in assignment (expression has type partial[int], variable has type Callable[[], int])

I can shut it up with a cast but that's ugly. I'd like the checker to at least understand that class partial as given is callable.

@JukkaL JukkaL added the bug mypy got something wrong label Jan 8, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 8, 2016

The reason for this is that mypy doesn't consider partial to be a subtype of Callable, even though it obviously should be. This is a bug in mypy, and there already is an open issue for this: #797

@gvanrossum
Copy link
Member Author

Duplicate of #797.

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

2 participants