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

Callable does not accept functions/types where call variants match the interface #10518

Open
NiklasRosenstein opened this issue May 21, 2021 · 0 comments
Labels
bug mypy got something wrong

Comments

@NiklasRosenstein
Copy link

Bug Report

MyPy won't allow me to pass built-ins like sorted, set or list to a t.Callable[[T], R] argument.

To Reproduce

import typing as t

T = t.TypeVar('T')
R = t.TypeVar('R')
Collector = t.Callable[[t.Iterable[T]], R]

def collect(it: t.Iterable[T], collector: Collector[T, R]) -> R:
  return collector(it)

collect(['str'], sorted)  # Argument 2 to "collect" has incompatible type overloaded function; expected "Callable[[Iterable[str]], List[SupportsLessThanT]]"
collect(['str'], set)     # Argument 2 to "collect" has incompatible type "Type[Set[Any]]"; expected "Callable[[Iterable[str]], Set[_T]]"
collect(['str'], list)    # Argument 2 to "collect" has incompatible type "Type[List[Any]]"; expected "Callable[[Iterable[str]], List[_T]]"

Expected Behavior

MyPy accepts the above code.

Actual Behavior

MyPy complains about type errors (as shown in the comments in the example)

You can work around this by wrapping the built-in in a lambda:

collect(['str'], lambda x: sorted(x))
collect(['str'], lambda x: set(x))
collect(['str'], lambda x: list(x))

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: mypy test.py
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.8.5
  • Operating system and version: WSL2 Ubuntu 20
@NiklasRosenstein NiklasRosenstein added the bug mypy got something wrong label May 21, 2021
@NiklasRosenstein NiklasRosenstein changed the title Callable should accept functions where call variants match the interface Callable should accept functions and types where call variants match the interface May 21, 2021
@NiklasRosenstein NiklasRosenstein changed the title Callable should accept functions and types where call variants match the interface Callable does not accept functions/types where call variants match the interface May 21, 2021
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

1 participant