Closed
Description
Bug Report
Defining a type that says: "I am a function that takes a function and its arguments" does not get accepted by mypy.
To Reproduce
import contextlib
from concurrent.futures import Future, ThreadPoolExecutor
from typing import Callable, Iterator, ParamSpec, TypeVar
Signature = ParamSpec("Signature")
T = TypeVar("T")
TakesFunctionWithArguments = Callable[
[Callable[Signature, T], Signature.args, Signature.kwargs],
Future[T]
]
@contextlib.contextmanager
def submit_wrapper() -> Iterator[TakesFunctionWithArguments]:
with ThreadPoolExecutor() as pool:
def my_submit(func: Callable[Signature, T], *args: Signature.args, **kwargs: Signature.kwargs) -> Future[T]:
return pool.submit(func, *args, **kwargs)
yield my_submit
def foo(a: int, b: int, c: int) -> int:
return a + b + c
with submit_wrapper() as submit:
submit(foo, a=1, b=2, c=3)
Expected Behavior
This should type check correctly. It runs fine.
Actual Behavior
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:10: error: The first argument to Callable must be a list of types or "..."
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:10: error: Name "Signature.args" is not defined
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:10: error: Name "Signature.kwargs" is not defined
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:29: error: Unexpected keyword argument "a"
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:29: error: Unexpected keyword argument "b"
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:29: error: Unexpected keyword argument "c"
There are two errors here, I am not sure how related they are, so I have them both in this ticket:
- The type alias on top is not accepted, because it can not relate the Signature ParamSpec within the Callable (I assume).
- When calling the function, it does not detect that a,b,c are actually valid keyword arguments to foo.
Your Environment
- Mypy version used: mypy 0.950 (compiled: yes)
- Mypy command-line flags: None, but if you add --strict it adds the error
/home/veith/.config/JetBrains/PyCharm2022.1/scratches/scratch_7.py:16: error: Missing type parameters for generic type "TakesFunctionWithArguments"
- Python version used: 3.10.4
- Operating system and version: Ubuntu 18.04
Possibly related to #12595