You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Python 2, the test fails with the following exception:
for attr in assigned:
> setattr(wrapper, attr, getattr(wrapped, attr))
E AttributeError: 'A' object has no attribute '__name__'
/usr/lib/python2.7/functools.py:33: AttributeError
The same test works in Python 3. The reason why the behavior is different between Python versions is the different implementation of functools.wraps in Python 2 and Python 3. In Python 3, the attributes which are supposed to be set on the wrapper object but are missing on the original object are simply skipped:
This prevents the standard call to functools.wraps without extra parameters in Python 2 from being able to wrap the non-function objects that do not have attributes like __name__, which are by default expected to be present on the original (wrapped) object.
The text was updated successfully, but these errors were encountered:
nicoddemus
added a commit
to nicoddemus/pytest-mock
that referenced
this issue
Oct 4, 2019
With
pytest-mock
version1.11.0
, the non-function objects cannot be spied on usingmocker.spy
in Python 2. For example:uut.py
:test_uut.py
:In Python 2, the test fails with the following exception:
The same test works in Python 3. The reason why the behavior is different between Python versions is the different implementation of
functools.wraps
in Python 2 and Python 3. In Python 3, the attributes which are supposed to be set on the wrapper object but are missing on the original object are simply skipped:https://github.com/python/cpython/blob/3.7/Lib/functools.py#L52-L58
However, in Python 2, all the attributes which are supposed to be set on the wrapper object must also be present on the original object:
https://github.com/python/cpython/blob/2.7/Lib/functools.py#L32-L33
This prevents the standard call to
functools.wraps
without extra parameters in Python 2 from being able to wrap the non-function objects that do not have attributes like__name__
, which are by default expected to be present on the original (wrapped) object.The text was updated successfully, but these errors were encountered: