unittest.mock.patch of a subclass poisons base class
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
this is an entirely silly example boiled down from a much more complex one to demonstrate the behaviour. the actual code here is nonsensical (Test2 will always fail) but Test1 should work just fine -- the decoration of the subclass Test2 seems to also decorate Test1's methods unexpectedly
from unittest import mock
s = 0
@mock.patch(f"{__name__}.s", 5)
class Test1:
def test_1(self):
assert s == 5, s
@mock.patch(f"{__name__}.s", 2)
class Test2(Test1): pass
Test1().test_1()
$ python3.12 t2.py
Traceback (most recent call last):
File "/Users/asottile/workspace/sentry/t2.py", line 14, in <module>
Test1().test_1()
File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/unittest/mock.py", line 1387, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/asottile/workspace/sentry/t2.py", line 8, in test_1
assert s == 5, s
AssertionError: 2
the problem appears to be here: https://github.com/python/cpython/blob/ea94b3b149eeadf33c2f7c46f16dcda0adc7cf4e/Lib/unittest/mock.py#L1412
whereas I think it should always return a new callable instead of mutating a potentially unrelated method
CPython versions tested on:
3.12
Operating systems tested on:
macOS
Linked PRs
- gh-117665
- gh-117667
- gh-117687
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führen Sie den bereitgestellten Python 3.12-Reproducer aus und untersuchen Sie Lib/unittest/mock.py um Zeile 1412, wo das Issue das problematische Verhalten identifiziert. Sehen Sie sich vor den Änderungen die verknüpften PRs gh-117665, gh-117667 und gh-117687 an; als erledigt gilt die Aufgabe, wenn das Patchen einer Subklasse nicht mehr das Verhalten der geerbten Methode ändert, das über Test1 beobachtet wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- testing-qa
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 25/100