python / python/mypy

False positive errors with anonymous and inline declaration of namedtuple types

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

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

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

説明

Bug Report

Mypy raises false positive errors with anonymous and inline declaration of namedtuple types.

To Reproduce

from collections import namedtuple

dim = namedtuple("Dimension", ["x", "y"])(2, 3)

This should be equivalent to the following, which mypy gets correct:

from collections import namedtuple

Dimension = namedtuple("Dimension", ["x", "y"])
dim = Dimension(2, 3)

(Example taken from here: https://stackoverflow.com/a/26405216)

Expected Behavior

No error raised.

Actual Behavior

$ mypy namedtuple.py 
namedtuple.py:3: error: Too many arguments for "tuple"  [call-arg]
namedtuple.py:3: error: Argument 1 to "tuple" has incompatible type "int"; expected "Iterable[Any]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

My Environment

$ mypy --version
mypy 1.7.1 (compiled: yes)
$ python --version
Python 3.11.6

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

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

はじめの一歩

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

調査の方向性

まず、namedtuple.py と issue に示されているインラインの無名宣言で報告を再現し、その後、別個の Dimension 宣言と比較します。mypy が collections.namedtuple の呼び出しをどのように処理しているかを追跡します。インライン形式で誤検知エラーが発生せず、既存の別個の宣言の例が正しいままであれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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