python / python/cpython

mock.mock_open __exit__ called as instance method from contextlib.ExitStack context

Ouverte
#150,484 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

3.13 3.14 stdlib type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description:

In Python 3.13 onwards:

from contextlib import ExitStack
from unittest import mock
with mock.patch("__main__.open", mock.mock_open()) as m:
    with ExitStack() as exit_stack:
        with exit_stack.enter_context(open("/tmp/test.txt", "w")):
            pass

raises

TypeError: mock_open.<locals>._exit_side_effect() takes 3 positional arguments but 4 were given

The issue seems to come from https://github.com/python/cpython/commit/3f7c0810f6158a7ff37be432f8d7f9511427489f

It appears that in Python versions 3.12 and earlier, mock_open().__exit__ was always called with 4 args (self, exctype, excinst, exctb) when used with contextlib.ExitStack but that used to be a no-op before the exit handler was added.

I'm not sure whether this should be fixed by:

  1. changing the way ExitStack calls its stack of __exit__ callbacks
  2. making mock_open and open more similar, or
  3. making mock_open's exit handler take *args, **kwargs same as the enter handler??

Thanks

CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-150521
  • gh-150535
  • gh-151829
  • gh-151861

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le reproducer en utilisant contextlib.ExitStack et unittest.mock.mock_open, puis examinez le comportement du callback décrit dans l’issue. Le travail est terminé lorsque le TypeError de Python 3.13+ est résolu, que le comportement attendu du context manager est préservé et qu’une couverture est ajoutée pour le cas signalé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
testing
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.