python / python/cpython

Misleading errors and confusing behavior with zero-argument `super()` in inner functions

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

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

interpreter-core type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

See also: #90333.

super() exhibits behavior subtly broken in different ways when used in inner functions with zero arguments:

class Foo:
    def __init__(self):
        def inner():
            print(str(super()))

        inner()

>>> Foo()
...
RuntimeError: super(): no arguments

and in inner functions that do take arguments:

class Foo:
    def __init__(self):
        def inner(_=1):
            print(str(super()))

        inner()

>>> Foo()
...
TypeError: super(type, obj): obj must be an instance or subtype of type

...unless the first argument is None:

class Foo:
    def __init__(self):
        def inner(_=None):
            print(str(super()))

        inner()

>>> Foo()
<super: <class 'Foo'>, NULL>

but then method lookup doesn't work as expected (raises AttributeError trying to access any method not defined on NoneType).

__class__ gets injected into the code properly, it just seems that super() assumes the first argument of the immediately enclosing function is self.

It seems to me that:

  • The best case would be for this behavior to look up self from the same scope __class__ was defined in rather than the immediate scope where super() was invoked, making it possible to use zero-argument super in nested functions
  • If that is not possible, a consistent error should be raised regardless of the arguments to the immediately-enclosing function; all three of those examples should communicate that zero-argument super in nested functions is not supported. Especially the first error message is misleading, since it describes two-argument super.
  • If zero-argument super() is not made to work in nested functions, this quirk should be clearly documented. Right now the documentation only vaguely hints at problems:

    Also note that, aside from the zero argument form, super() is not limited to use inside methods.

I'd be happy to work on this but am concerned there might be a lot of edge-cases that are not obvious to me (like the workaround for super() being called from the C-API before a MAKE_CELL).

Your environment

  • CPython versions tested on: 3.10.7, 3.12 dev @ eae7dad40255bad42e4abce53ff8143dcbc66af5
  • Operating system and architecture: Linux x86_64

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

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

はじめの一歩

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

調査の方向性

まず、issue にある 3 つのネストした関数の例を再現し、引数なしの super() の挙動を比較します。引数なしの super() のドキュメントを読み、言及されている class、MAKE_CELL、C-API の相互作用を調査します。合意された挙動、一貫したエラーまたはサポートされる lookup セマンティクス、および対応するリグレッションカバレッジまたはドキュメントが揃えば完了です。

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

評価

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

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

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