Suggest using Protocol when user tries to use keyword args with Callable

Open
#1,655 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start by reproducing the two examples in sample_python.py and inspect how mypy handles Callable annotations and keyword arguments. Review the issue's proposed Protocol direction and establish expected behavior for both examples; done means the agreed behavior is implemented and covered by tests for these cases.

Written by the indexing model from the issue text.

Description

false-positive priority-1-normal topic-usability

If I define a passed in function using the Callable syntax, mypy complains if I then call a keyword argument on that function. This is makes writing things like decorators problematic

Sample code:

from typing import Callable

def test_func(a, b, c=0, d=0):
    # type: (int, int, int, int) -> int
    return a + b + c + d

def check_func(method):
    # type: (Callable[[int, int, int], int]) -> int
    return method(1, 2, d=5)

print(check_func(test_func))
$ mypy sample_python.py
sample_python.py: note: In function "check_func":
sample_python.py:9: error: Unexpected keyword argument "d"

As an aside here, I the following function code also raises mypy errors:

from typing import Callable

def test_func(a, b, *, c, d):
    # type: (int, int, int, int) -> int
    return a + b + c + d

def check_func(method):
    # type: (Callable[[int, int, int, int], int]) -> int
    return method(1, 2, c=5, d=10)

print(check_func(test_func))
Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.