AttributeError on `with super()`
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug report
Bug description:
Writing with super() raises an AttributeError. I ran into this implementing context managers inspired by contextlib.contextmnager but which had extra functionality. I can see no reason why with super() should be disallowed, and in fact, super().__enter__() and super().__exit__() work just fine.
Attempting to use an ExitStack as a work around was unsuccessful.
import sys
from contextlib import ExitStack
class Base:
def __enter__(self):
print("Entering base context manager")
def __exit__(self, type, value, traceback):
print("Exiting base context manager")
def __derived_enter__(self):
self.context = self.contextlib_style_context()
next(self.context)
return self.context
def __derived_exit__(self, type, value, traceback):
try:
next(self.context)
except StopIteration:
pass
class DerivedUsingEnterExit(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
super().__enter__()
try:
yield
finally:
super().__exit__(*sys.exc_info())
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
class DerivedUsingWith(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
with super():
yield
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
class DerivedUsingExitStack(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
with ExitStack() as stack:
stack.enter_context(super())
yield
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
# Runs successfully
with DerivedUsingEnterExit() as d:
pass
# Raises AttributeError
with DerivedUsingWith() as d:
pass
# Raises AttributeError
with DerivedUsingExitStack() as d:
pass
CPython versions tested on:
3.13, 3.12
Operating systems tested on:
Windows
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo los ejemplos proporcionados de DerivedUsingWith y DerivedUsingExitStack en CPython 3.13 o 3.12 y, después, rastrea los puntos de entrada de super() y del gestor de contexto implicados. Compáralos con el caso funcional de super().enter() y super().exit(); estar terminado significa que el comportamiento previsto está decidido y que el AttributeError notificado está cubierto por una prueba de regresión.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100