`mock.patch(..., autospec=True)` on a `functools.partialmethod` attribute produces a completely uncallable mock
Personne n'a encore pris cette issue.
- 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:
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue identifie Lib/unittest/mock.py et _patch.get_original() ; commencez par exécuter la reproduction de partialmethod et retracer la manière dont autospec gère le descripteur brut. C’est terminé lorsque le patch de l’attribut avec autospec produit un mock appelable et que l’appel client montré ne lève plus TypeError, avec une couverture de régression pour ce cas.
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é
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 35/100