New submission from Pierre Ossman <oss...@cendio.se>:

unittest.mock.patch() as it currently works cannot properly mock a method as it 
currently replaces it with something more mimicking a function. I.e. the 
descriptor magic that includes "self" isn't properly set up.

In most cases this doesn't really matter, but there are a few use cases where 
this is important:

1. Calling base classes where you need to make sure it works regardless of 
super() or direct reference to the base class.

2. Multiple objects calling the same base class using super(). Without the self 
argument you can't tell the calls apart.

3. Setting up a side_effect that needs access to the object. In some cases you 
can pass the object using some side channel, but not all. E.g. not when mocking 
a base class' __init__(). (already reported as Issue35577).

Right now you can work around this by using autospec, as that has the 
undocumented side-effect of properly setting up methods. So don't fix 
Issue41915 before this one or we lose that workaround. :)

----------
components: Library (Lib)
messages: 382415
nosy: CendioOssman
priority: normal
severity: normal
status: open
title: unittest.mock.patch() cannot properly mock methods
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42556>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to