Unpacking a NamedTuple subclass uses superclass' types
オープン
まだ誰も着手していません。
bug
topic-inheritance
topic-named-tuple
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
If you subclass an existing NamedTuple to provide more specific type declarations for some of the fields, and then unpack an instance of that NamedTuple, mypy 0.770 gets its type information from the superclass, not the subclass. Minimal reproduction:
from typing import NamedTuple, Optional
class BaseInventory(NamedTuple):
number: Optional[int]
name: str
class Inventory(BaseInventory):
number: int
item = Inventory(42, 'parrot')
item.number % 10 # mypy allows this
number, _ = item
number % 10 # mypy does not allow this
The last line reports:
error: Unsupported operand types for % ("None" and "int") [operator]
note: Left operand is of type "Optional[int]"
I expected no error, since accessing the field by name works fine.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、mypy 0.770 で最小限の Python 再現コードを実行し、名前付きフィールドへのアクセスとタプルのアンパックを比較します。アンパック中に NamedTuple の型情報がどのように使用されるかを追跡します。アンパックされた number が int と推論され、最後の modulo の行でエラーが報告されなくなり、この再現コードに対するリグレッションカバレッジが追加されたら完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100