python / python/mypy

Errors from class decorators returning descriptors

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

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

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

説明

Bug Report

When using a decorator on a nested class that returns a descriptor, mypy is not able to properly distinguish between the class and the instance. The full code is in the gist with the snippets below trying to highlight the problem.

https://gist.github.com/adam-ring/86dc9239fd54e2eafe07829329f39a0d

@overload
def add_register(cls: None = ...) -> RegDescpr[Register]:
    ...
@overload
def add_register(cls: type[T]) -> RegDescpr[T]:
    ...
def add_register(cls: type[T] | None = None) -> RegDescpr[T]:
    return RegDescpr[T](cls)


class FooBlock(RegBlock):

    BAR = add_register()

    @add_register
    class BAZ(Register):
        A = FieldDescpr()
        B = FieldDescpr()

    class BOO_CLS(Register):
        C = FieldDescpr()
    BOO = add_register(BOO_CLS)

foo = FooBlock()

Both foo.BAR and foo.BOO show no errors and run correctly. foo.BAZ lists errors with attribute or method access as will be shown below.

To Reproduce

The method access tests are shown here. Python runs as expected from all three, but mypy complains about an unbound self when accessing foo.BAZ.

foo = FooBlock()
print(f'{foo.BAR.reg_func()=}')  # no error
print(f'{foo.BAZ.reg_func()=}')  # ERROR: reg_func has unbound self
print(f'{foo.BOO.reg_func()=}')  # no error

Expected Behavior

Python runs with all test cases. Here is the output when the gist is run with Python.

$ python3 test_class_dec.py 
Register(name=BAR, RegBlock=FooBlock())
BAZ(name=BAZ, RegBlock=FooBlock())
BOO_CLS(name=BOO, RegBlock=FooBlock())
RegField(name=B, Register=BAZ(name=BAZ, RegBlock=FooBlock()))
RegField(name=C, Register=BOO_CLS(name=BOO, RegBlock=FooBlock()))
foo.BAR.reg_func()=42
foo.BAZ.reg_func()=42
foo.BOO.reg_func()=42
foo.BAZ.A()=5
foo.BOO.C()=5

Actual Behavior

Here is the output from mypy. Only foo.BAZ shows any errors in mypy, the class with the descriptor. The errors are present with method access and with the lower lever Field Descriptor access.

$ mypy test_class_dec.py 
test_class_dec.py:121: error: No overload variant of "__get__" of "FieldDescpr" matches argument types "None", "Type[BAZ]"
test_class_dec.py:121: note: Possible overload variants:
test_class_dec.py:121: note:     def __get__(self, obj: None, objtype: None) -> FieldDescpr
test_class_dec.py:121: note:     def __get__(self, obj: Register, objtype: Type[Register]) -> RegField
test_class_dec.py:125: error: Missing positional argument "self" in call to "reg_func" of "Register"
test_class_dec.py:127: error: No overload variant of "__get__" of "FieldDescpr" matches argument types "None", "Type[BAZ]"
test_class_dec.py:127: note: Possible overload variants:
test_class_dec.py:127: note:     def __get__(self, obj: None, objtype: None) -> FieldDescpr
test_class_dec.py:127: note:     def __get__(self, obj: Register, objtype: Type[Register]) -> RegField

Your Environment

  • Python 3.10.4
  • mypy 0.981 (compiled: no)

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

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

はじめの一歩

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

調査の方向性

まず gist の test_class_dec.py 例に対して mypy を実行し、ネストされたデコレート済みクラス BAZ を BAR および BOO と比較します。mypy が descriptor と decorator の型をどのように処理するかを追跡し、その後、修正によって報告されたエラーが取り除かれ、示されている実行時の動作が維持されることを確認します。

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

評価

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

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

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