python / python/mypy

`*args` and `**kwargs` are allowed even to methods with no arguments

オープン
#13,380 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

bug topic-calls
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

I am working on removing dict and **kwargs hack from mypy after https://github.com/python/typeshed/pull/8517 is merged.

But, I found a very confusing thing in how *args and **kwargs are checked.

Simple repro (all of the examples below work):

def some() -> None: ...

# args
some(*[1, 2])
args = [1, 2]
some(*args)

# kwargs
some(**{'a': 1})
kw = {'a': 2}
some(**kw)

All of these would raise TypeError in runtime.
I think that the main idea was to allow calls like some(*[]) and some(**{}) which are fine in runtime.

This affects how overloads are selected in complex cases like:

class dict2(Generic[KT, VT]):
    @overload
    def __init__(self, __iterable: Iterable[Tuple[KT, VT]]) -> None: pass
    @overload
    def __init__(self: "dict2[str, VT]", __iterable: Iterable[Tuple[str, VT]], **kwargs: VT) -> None: pass

it = [(1, 'x')]
kw = {'x': 'y'}
reveal_type(dict2(it, **kw))

It looks like the second @overload will raise an error: Iterable[Tuple[str, VT]] is required, but Iterable[Tuple[int, str]] is given.

But it does not, because of how **kwargs are silently ignored. So, first @overload always matches.

This is broken, if you ask me 😢

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

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

はじめの一歩

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

調査の方向性

reproducer に記載されている *args と **kwargs の呼び出しチェックおよびオーバーロード選択の動作から始めます。引数のない関数の呼び出しで、空でないアンパック引数とキーワード引数がどのように処理されるかを確認し、有効な空のアンパックは維持します。無効な実行時呼び出しが拒否され、dict2 のオーバーロード例が正しく選択されるかエラーを報告すれば完了です。

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

評価

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

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

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