Misleading errors and confusing behavior with zero-argument `super()` in inner functions
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Métriques de merge des PR
- Métriques de PR en attente
Description
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
selffrom the same scope__class__was defined in rather than the immediate scope wheresuper()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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire les trois exemples de fonctions imbriquées de l’issue et comparer leur comportement avec super() sans argument. Lisez la documentation de super() sans argument et étudiez les interactions mentionnées entre class, MAKE_CELL et C-API. La tâche est considérée comme terminée lorsqu’un comportement convenu, des erreurs cohérentes ou une sémantique de recherche prise en charge, ainsi qu’une couverture de régression ou une documentation correspondante, sont disponibles.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100