python / python/mypy

mutable-override does not understand unions

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

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

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

説明

Bug Report
If I enable mutable-override it fails as it does not understand simple unions.

To Reproduce
Turn on mutable-override

class Base:
    val: bool|None = True
    
class Derived(Base):
    val = False

Gist: https://gist.github.com/mypy-play/aeec67e97957ea9e60431ce8466d8af3
Playground: https://mypy-play.net/?mypy=latest&python=3.12&enable-error-code=mutable-override&gist=aeec67e97957ea9e60431ce8466d8af3

Expected Behavior
no error

Actual Behavior

main.py:5: error: Covariant override of a mutable attribute (base class "Base" defined the type as "bool | None", expression has type "bool")  [mutable-override]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: enable-error-code="mutable-override"
  • Python version used: 3.12

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

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

はじめの一歩

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

調査の方向性

mutable-override を有効にした mypy 1.18.2 で、提供された Base/Derived の例を使って報告を再現し、その後、union アノテーションに対する mutable-override の型チェックのエントリポイントを追跡します。完了条件は、その例でエラーが発生せず、既存の mutable-attribute override チェックが引き続き機能することです。

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

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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