python / python/cpython

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

Aperta
#150,484 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.13 3.14 stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il reproducer usando contextlib.ExitStack e unittest.mock.mock_open, quindi esamina il comportamento del callback descritto nell’issue. Il lavoro è completato quando il TypeError di Python 3.13+ è risolto, il comportamento previsto del context manager è preservato e viene aggiunta la coverage per il caso segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
testing
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.