python / python/mypy

Bug - Type aliasing typing.Tuple is treated as if the empty Tuple is being aliased

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

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

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

説明

This is a bug report.

Type aliasing typing.Tuple is treated as if the empty tuple (typing.Tuple[()]) is being aliased.

Here is some sample code that converts Polar coordinates into Cartesian:

import math
import typing

Tuple = typing.Tuple


def cartesian(r: int, θ: int) -> Tuple[float, float]:
    rad = math.radians(θ)
    return r*math.cos(rad), r*math.sin(rad)


r = 12
θ = 195

x,y = cartesian(r, θ)
print(f"({r}, {θ}°) = ({x:.2f}, {y:.2f})")

$ python3 example.py 
(12, 195°) = (-11.59, -3.11)
$ mypy example.py 
example.py:7: error: Bad number of arguments for type alias, expected: 0, given: 2
Found 1 error in 1 file (checked 1 source file)

What is the actual behavior/output?

example.py:7: error: Bad number of arguments for type alias, expected: 0, given: 2
Found 1 error in 1 file (checked 1 source file)

What is the behavior/output you expect?

Success: no issues found in 1 source file

Such as when from typing import Tuple is used instead.

What are the versions of mypy and Python you are using?

I'm using mypy version 0.77 installed using pip and Python 3.7.7 instead via homebrew on macOS Catalina 10.15.4.

Do you see the same issue after installing mypy from Git master?

Yes.

What are the mypy flags you are using? (For example --strict-optional)

None.

Additional details

It might be related to #8273. I know Tuple and Callable are both mentioned in PEP 484 as being different.

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

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

はじめの一歩

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

調査の方向性

example.py の再現から始め、追加の flags を付けずに mypy を実行し、レポートで説明されている aliased typing.Tuple アノテーションと直接インポートを比較します。aliased 形式が期待される “Success: no issues found” の出力で正常に型チェックされれば、作業は完了です。

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

評価

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

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

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