monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)
Los mantenedores suelen responder en 1 día
@Maurenz2530 ya está trabajando en esto.
Desde el 26/9/2026.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- python
- Área
- testing-qa
Línea de trabajo
Empieza por la nueva rama en MonkeyPatch.setattr y compárala con el comportamiento anterior descrito en el issue. Añade un caso de regresión a test_proxy.py usando el objeto personalizado setattr/getattr, y ejecuta esa prueba para verificar que undo restaura el valor original y que el valor parcheado no se filtra a pruebas posteriores.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Since #14969 (0c601d510, not released yet), monkeypatch.setattr doesn't restore attributes on objects that store them somewhere other than __dict__ through a custom __setattr__/__getattr__. Undo raises AttributeError, and the patched value leaks into later tests.
class Config:
"""Stores attributes in a private dict instead of __dict__."""
def __init__(self):
object.__setattr__(self, "_data", {"debug": False})
def __getattr__(self, name):
try:
return self._data[name]
except KeyError:
raise AttributeError(name) from None
def __setattr__(self, name, value):
self._data[name] = value
cfg = Config()
def test_patch(monkeypatch):
monkeypatch.setattr(cfg, "debug", True)
assert cfg.debug is True
def test_restored():
assert cfg.debug is False
On main:
ERROR test_proxy.py::test_patch - AttributeError: 'Config' object has no attribute 'debug'
FAILED test_proxy.py::test_restored - assert True is False
1 failed, 1 passed, 1 error
Just before 0c601d510, the same file passes (2 passed).
The new branch in MonkeyPatch.setattr takes the old value from target.__dict__.get(name, NOTSET) whenever target has a __dict__. It assumes that a plain setattr() writes into that dict. With a custom __setattr__ it doesn't, so the old value is recorded as NOTSET. undo() then calls delattr(cfg, "debug") instead of setting it back to False. That delattr fails, and the value stays patched.
@Shriprasad-P pointed out this exact case in a review on #14969, but it wasn't addressed before the merge. I'm opening this issue so it gets fixed before the next release.
One possible direction: only use the __dict__ lookup when name is actually present in the instance __dict__, or when the type uses object.__setattr__. Otherwise fall back to the value from getattr(), as before.
pytest 9.2.0.dev345+g872117358, Python 3.13.5, Windows 11.
- Lenguaje dominante
- Python
- Estrellas
- 14.6k
- Forks
- 3.4k
- Merge medio
- 2 d 7 h
- PR fusionados (30 d)
- 29
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la 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.
Más de pytest-dev/pytest
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
pytest-dev/pytest#15146 ·
Los mantenedores suelen responder en 1 día
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`Quizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
pytest-dev/pytest#14514 · 6 comentarios ·
Los mantenedores suelen responder en 1 día
-
Expose CaptureManager as public APIQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abiertotype: enhancement type: feature-branch
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
pytest-dev/pytest#14186 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Docs on warnings filters should note that `ResourceWarning` is often delayedPosiblemente ocupada @tanishmisra9 la tomó hace 107 días. Abiertotype: docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
pytest-dev/pytest#9825 · 7 comentarios ·
Los mantenedores suelen responder en 1 día
-
conftest.py: support importorskip at import, make every other outcome exception a usage errorPosiblemente ocupada @RonnyPfannschmidt la tomó hace 1 día. Abiertotopic: collection
pytest-dev/pytest#15142 · 1 asignado ·
Los mantenedores suelen responder en 1 día
Todos los issues de pytest-dev/pytest
Issues similares
-
area/install reliability
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
FluidNumerics/fluid-walk-blocker#191 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
TransformerLensOrg/TransformerLens#1868 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
climate-analytics-lab/jax-gcm#1057 ·
Los mantenedores suelen responder en 1 día