python / python/mypy

Optional strict checking of *args and/or multiple assignment

Open
#7,408 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion topic-calls
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Currently mypy does optimistic checking of *args -- we don't generate error if it could be valid. This seems like a reasonable default, since it avoids false positive errors. However, some users might prefer stricter checking as an option. When this option is enabled, mypy would only allow variable-length (non-Any) *args in a call if the callee also accepts *args.

Example:

def f(a: int) -> None: pass

x = [1, 2]
f(*x)  # Currently accepted, since mypy doesn't know the length of x

PR #7392 relaxed the rules so that we assume that any list *args argument could be empty and thus we don't complain about even if a caller *args argument is only valid when it's empty.

Similarly this option (or a similar option) could reject variable-length rvalues in multiple assignment like this:

x = [1, 2]
a, b, c = x  # Currently accepted

If we'd have support for enabling/disabling specific error codes, these checks could easily be implemented through error codes that are disabled by default.

Refactoring code such as the above (if correct) to pass mypy could be somewhat painful, however. It could be written like this:

def f(a: int, b: int) -> None: pass

def g(x: List[int]) -> None:
    assert len(x) == 2
    f(x[0], x[1])

(Of course, a # type: ignore would also work.)

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

No source files or tests are named. Start by reviewing PR #7392 and the existing handling of variable-length *args and multiple assignment; done would mean a decided, optional strict-checking design for these cases, including how the checks are enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.