Confusing error message when calling a function with incorrect *args

Open
#4,771 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

Reproduce the example in bad_starargs.py and compare mypy's current diagnostics with the proposed output. Trace the handling of the unpacked tuple and update the diagnostics so each mismatched element identifies its own argument, with a note showing the unpacked tuple type.

Written by the indexing model from the issue text.

Description

feature priority-1-normal topic-error-reporting topic-usability

When typechecking this:

def foo(x: int, y: str) -> None: pass
t = ('hi', 12)
foo(*t)

mypy reports

bad_starargs.py:3: error: Argument 1 to "foo" has incompatible type "*Tuple[str, int]"; expected "int"
bad_starargs.py:3: error: Argument 1 to "foo" has incompatible type "*Tuple[str, int]"; expected "str"

This is not great, since Tuple[str, int] isn't being compared to something, and while it appears in argument position 1, the type mismatches don't necessarily.

A better error message might be something like:

bad_starargs.py:3: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
bad_starargs.py:3: note: while unpacking "*Tuple[str, int]"
bad_starargs.py:3: error: Argument 2 to "foo" has incompatible type "int"; expected "str"
bad_starargs.py:3: note: while unpacking "*Tuple[str, int]"
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.