python / python/mypy

Incorrect mapping of actual to formal arguments.

Open
#19,692 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-calls
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

def takes_at_least3(a1: str, a2: str, a3: str, *args: str) -> None: ...

def test(
    x0: tuple[str, ...],
    x1: tuple[str, *tuple[str, ...]],
    x2: tuple[str, str, *tuple[str, ...]],
    x3: tuple[str, str, str, *tuple[str, ...]],
) -> None:
    takes_at_least3(*x0)  # no error
    takes_at_least3(*x1)  # Missing positional arguments "x2", "x3"
    takes_at_least3(*x2)  # no error
    takes_at_least3(*x3)  # no error

https://mypy-play.net/?mypy=latest&python=3.12&gist=34662974552c517610691a6b5a792ba2

There are only 2 logically sensible results here:

  1. All of x0-x3 should be accepted
  2. Only x3 should be accepted, since it is the only type that is guaranteed to provide at least 3 items

The choice depends on whether we consider tuple[str, ...] as equal to

  1. The union type tuple[()] | tuple[str] | tuple[str, str] | ...
  2. The gradual AnyOf[tuple[()], tuple[str], tuple[str, str], ... ] (https://github.com/python/typing/issues/566)

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 with the linked mypy-play reproduction, focusing on how the calls to takes_at_least3 are checked against x0 through x3. Review the issue's two proposed interpretations of tuple[str, ...] and determine which argument-mapping behavior should be specified. Done means the chosen behavior is implemented consistently and covered by a regression case for the example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.