Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Mocks created by mocker.patch.multiple are not handled like other patches

Aperta
#638 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@patrickswedish ci sta già lavorando.

Dal 17/10/2026.

  • #648 di @patrickswedish — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
testing

Direzione di ricerca

Start by tracing mocker.patch.multiple() and the registration and warning hooks used by mocker.stop(). Reproduce both failures with the examples in the issue, then add regression coverage showing that each returned mock can be used as a context manager and stopped individually. Done means the existing mocker.stop(mocks) behavior and the other patch APIs continue to work.

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

Descrizione

bug

mocker.patch.multiple() returns a dict of the mocks it created, and pytest-mock only registers that dict. #629 made resetall() reset the mocks inside it, but two other features still treat the dict as the mock:

  1. No context-manager warning. The PytestMockWarning hook is only installed on the dict (where it does nothing), not on the mocks:

    def test_multiple_warning(mocker):
        mocks = mocker.patch.multiple(Target, a=mocker.DEFAULT)
        with pytest.warns(PytestMockWarning):  # fails: DID NOT WARN
            with mocks["a"]:
                pass
    

    with mocker.patch.object(Target, "a"): does warn.

  2. mocker.stop() can't stop a single mock.

    def test_multiple_stop_one(mocker):
        mocks = mocker.patch.multiple(Target, a=mocker.DEFAULT, b=mocker.DEFAULT)
        mocker.stop(mocks["a"])  # ValueError: This mock object is not registered
    

    Only mocker.stop(mocks) works, which undoes all of them. Supporting single mocks would need each attribute's patcher to be stopped on its own.

Both reproduced on 3.16.1 (Target is any class with attributes a and b).

Lingua principale
Python
Stelle
2k
Fork
181
Merge medio
1g 22h
PR unite (30g)
25

Preparare l'ambiente

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.

Altre issue di pytest-dev/pytest-mock

Tutte le issue di pytest-dev/pytest-mock

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.