python / python/cpython

`mock.patch(..., autospec=True)` on a `functools.partialmethod` attribute produces a completely uncallable mock

Offen
#153,581 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

Autospeccing a class attribute that is a functools.partialmethod (either via mock.patch.object(cls, name, autospec=True) or mock.create_autospec(cls, ...) combined with a direct
mock.patch.object on that one attribute) produces a unittest.mock.NonCallableMagicMock. Calling it then raises TypeError: 'NonCallableMagicMock' object is not callable, even though the real attribute is perfectly callable (instance.method(...) works fine on the un-mocked class).

import functools
from unittest import mock

class Client:
    def _send_request(self, method, url):
        return method, url

    _send_post_request = functools.partialmethod(_send_request, "POST")

with mock.patch.object(Client, "_send_post_request", autospec=True) as m:
    print(type(m))  # <class 'unittest.mock.NonCallableMagicMock'>
    client = Client()
    client._send_post_request("https://example.com")
    # TypeError: 'NonCallableMagicMock' object is not callable
Root cause:

_patch.get_original() (Lib/unittest/mock.py) reads the attribute via target.__dict__[name] first, deliberately bypassing the descriptor protocol (this is needed to support patching descriptors like @property correctly). For a functools.partialmethod attribute, this yields the
raw partialmethod instance rather than the bound-callable object that getattr(instance, name) would produce.

functools.partialmethod objects are not themselves callable (they have no __call__; only __get__, since they are non-data descriptors meant to be resolved through attribute access):

>>> import functools, inspect
>>> p = functools.partialmethod(lambda self, a: None, 1)
>>> callable(p)
False
>>> inspect.signature(p)
Traceback (most recent call last):
  ...
TypeError: functools.partialmethod(<function <lambda> at 0x...>, 1, ) is not a callable object
CPython versions tested on:

3.12, but tested on main as well.

Operating systems tested on:

Linux

Linked PRs
  • gh-153582

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue identifiziert Lib/unittest/mock.py und _patch.get_original(); beginne damit, die partialmethod-Reproduktion auszuführen und nachzuverfolgen, wie autospec den rohen Deskriptor behandelt. Erledigt ist dies, wenn das Patchen des Attributs mit autospec einen aufrufbaren Mock erzeugt und der gezeigte Client-Aufruf nicht mehr TypeError auslöst, mit einer Regressionstestabdeckung für diesen Fall.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.