python / python/mypy

Errors on multi-variable assignments of types

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

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

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

説明

Bug Report

MyPy seems to have issues when multi-variable assignments of types to their aliases are involved.

To Reproduce
Run mypy on the following two sources:

from typing import Any

typ_a = Any
typ_b = Any
def test1(x: typ_a, y: typ_b): pass
from typing import Any

typ_a, typ_b = Any, Any
def test2(x: typ_a, y: typ_b): pass

typ_a = typ_b = Any
def test3(x: typ_a, y: typ_b): pass

Expected Behavior
MyPy does not show any errors for any of the function's signatures, since they are all essentially the same.

Actual Behavior
MyPy shows the following errors for test2 and test3:

repro2.py:4: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:4: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:4: error: Variable "repro2.typ_b" is not valid as a type
repro2.py:7: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:7: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:7: error: Variable "repro2.typ_b" is not valid as a type
Found 4 errors in 1 file (checked 1 source file)

Confusingly, if one merges the two source files, no errors are shown.

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.1
  • Operating system and version: Ubuntu 20.04

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

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

はじめの一歩

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

調査の方向性

まず repro2.py の3つの再現例に対して mypy を実行し、単一代入、タプル代入、連鎖代入が型エイリアスとしてどのように記録されるかを比較します。これらの代入と関数アノテーションに対する型チェックの経路を追跡します。完了条件は、3つすべてのシグネチャでエラーが発生せず、既存のエイリアスおよび変数のチェックが変更されないことです。

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

評価

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

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

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