python / python/mypy

Wrong "Too many arguments" errors with tuple unpacking

オープン
#9,710 コメント 4 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug topic-calls
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

I'm getting wrong or misleading "Too many arguments" errors when using tuple unpacking in function calls.

To Reproduce

from typing import Tuple

def x(x: int, y: int) -> None:
    print(x, y)

def foo1(t: tuple) -> None:
    x(*t[:1], 1)

def foo2(t: tuple) -> None:
    x(*t[:2])

def foo3(t: Tuple[int]) -> None:
    x(*t[:1], 1)

mypy reports:

$ mypy sandbox.py 
sandbox.py:7: error: Too many arguments for "x"
Found 1 error in 1 file (checked 1 source file)

(Line 7 is the call to x in foo1)

Expected Behavior

The call to x in foo1 will never have too many arguments -- it has either 1 (if t is empty) or 2 (if t is not empty). I would have expected an error regarding the possibility of t being empty (i.e. "Potentially not enough arguments").

Also I think that foo1 and foo2 share the same issues regarding their calls of x, so I would have expected to get the same errors for both of them.

Actual Behavior

The call to x in foo1 is reported, but the call to x in foo2 isn't. Interestingly, fixing the tuple length (as in foo3) seems to fix the issue.

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.5
  • Operating system and version: Ubuntu 18.04.5

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

報告された Python 3.8.5 環境で mypy を使って sandbox.py の例を再現し、foo1、foo2、foo3 の診断を比較します。失敗する呼び出しからタプルアンパッキングの引数チェックを追跡し、その後、各例の期待される処理に対するリグレッションカバレッジを追加して、診断が一貫していることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。