python / python/mypy

Class definition scope using walrus assignment operator raises [name-defined] error

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

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

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

説明

Bug Report

When using the assignment operator (aka "walrus") inside the class definition scope, I get a [name-defined] error.

To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=367470bba328790329e68ce08bd7eb59

class TestTuple:
    t = (a := 1, a)


class TestExpression:
    (b := 2, b)


def add(x: int, y: int):
    return x + y


class TestAdd:
    result = add(c := 3, c)


class TestOverride:
    d = add(d := 4, d)


TestTuple.a      # output: 1
TestTuple.t      # output: (1, 1)
TestExpression.b # output: 2
TestAdd.c        # output: 3
TestAdd.result   # output: 6
TestOverride.d   # output: 8

Expected Behavior
I expected mypy to not raise any error as these all execute in python.

Actual Behavior

main.py:2: error: Name "a" is not defined  [name-defined]
main.py:6: error: Name "b" is not defined  [name-defined]
main.py:14: error: Name "c" is not defined  [name-defined]
main.py:18: error: Name "d" is not defined  [name-defined]
Found 4 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

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

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

はじめの一歩

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

調査の方向性

リンクされている mypy-play の再現例と、issue 内の Python 3.11 の例から始め、その後、クラススコープの walrus 代入がどのように解析されるかを追跡します。報告された例が、示されている推論値を維持したまま、4 件の name-defined エラーなしで型チェックを通れば完了です。

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

評価

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

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

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