python / python/mypy

Only allow splatting iterables if function takes at least one positional arg

Open
#12,206 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-calls
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

At runtime the following code snippet:

def f(*, x: int = 9, y:int = 8) -> None:
    pass

kwargs = {"x": 1, "y": 2}
f(*kwargs)

results in TypeError: f() takes 0 positional arguments but 2 were given. However, mypy does not report any issues.

To Reproduce

  1. Execute python interpreter for the code snipped above.
  2. Run mypy for the code snippet above.

Expected Behavior

Mypy should yield an error. For example pytype yields the following error:

Function f expects 0 arg(s), got 2 [wrong-arg-count]
         Expected: (*, x, y)
  Actually passed: (_, _)

Actual Behavior

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None. Simply mypy <FILENAME>.
  • Mypy configuration options from mypy.ini (and other config files): N/A (defaults)
  • Python version used: 3.8.2
  • Operating system and version: Mac OS Big Sur

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.

Research direction

Start by running the Python reproducer and the mypy command described in the issue, then trace how mypy checks a starred iterable passed to a function with only keyword-only parameters. Done means mypy reports an argument-count error for this example and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.