python / python/mypy

Inconsistent "Invalid Metaclass" error for classes defined within functions

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

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

bug topic-metaclasses topic-type-alias
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

When a class:

  • is defined inside a function, and
  • uses a metaclass

then it mypy will inconsistently give an Invalid metaclass error based on whether the metaclass is specified via an alias name, or the original name from when the class is defined. This behaviour occurs regardless of whether the metaclass is defined inside or outside of the function. I would have expected no error.

To Reproduce

See on mypy playground

class M(type):
    pass


def f() -> None:
    _M = M

    class C(metaclass=_M): # Error
        pass
    
    class D(metaclass=M): # OK
        pass

Actual Behavior

main.py:8: error: Invalid metaclass "_M"  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.1.1
  • Python version used: 3.11

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

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

はじめの一歩

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

調査の方向性

リンクされた mypy playground とネストされた関数の再現例から始め、エイリアスされたメタクラス _M と元の名前 M を比較します。無効なメタクラスのチェックを追跡し、同等のエイリアスで一貫性のない診断が生成されなくなり、例がエラーを報告しないことを確認します。

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

評価

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

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

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