python / python/mypy

New semantic analyzer: some assignments aren't recognized as types if incomplete

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

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

false-positive priority-1-normal semantic-analyzer
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

In the following test case the forward reference to In in the class body doesn't work:

[case testNewAnalyzerNamedTupleCallNested]
from typing import NamedTuple
o: C.Out
i: C.In
reveal_type(o)  # E: Revealed type is 'Tuple[Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]'
reveal_type(o.x)  # E: Revealed type is 'Tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]'
reveal_type(o.y)  # E: Revealed type is '__main__.C.Other'
reveal_type(o.x.t)  # E: Revealed type is '__main__.C.Other'
reveal_type(i.t)  # E: Revealed type is '__main__.C.Other'

class C:
    Out = NamedTuple('Out', [('x', In), ('y', Other)])   # 'In' undefined  <<--- should be ok?
    In = NamedTuple('In', [('s', str), ('t', Other)])
    class Other: pass

The root cause is that if the rvalue of an assignment is incomplete, we add a PlaceholderNode that doesn't have becomes_typeinfo set. This may also affect other things such as assignment-based TypedDicts, type aliases, type variables and NewTypes (haven't checked).

Not sure what would be the best way to fix this. One idea would be to treat every placeholder as a possible type, at least if it's generated from an incomplete assignment. A second idea would be to recognize possible types by looking at an incomplete rvalue, but this doesn't seem always possible.

This is follow-up to #6983.

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

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

はじめの一歩

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

調査の方向性

まず、提供されている testNewAnalyzerNamedTupleCallNested のテストケースを実行し、不完全な代入によって PlaceholderNode がどのように作成されるかを調べます。issue に記載されているように、同じ挙動が代入ベースの TypedDict、型エイリアス、型変数、または NewTypes にも影響するか確認します。フォワード参照が解決され、関連するケースを退行させることなく、一覧にある reveal_type の期待値を満たせば完了です。

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

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

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

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