This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Mock `_mock_wraps` is undocumented and inconsistently named
Type: Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Woodz, terry.reedy
Priority: normal Keywords:

Created on 2021-02-20 08:44 by Woodz, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg387397 - (view) Author: Richard Wise (Woodz) Date: 2021-02-20 08:44
I am trying to use wraps to delegate a call to a decorated patch mock to another method. By examining the source code, I was able to achieve this using the (apparently undocumented) `Mock._mock_wraps` attribute instead of the `wraps` attribute which would be expected given the constructor parameter names. I find this behaviour very confusing and inconsistent. Can we either expose `Mock.wraps` attribute or document `_mock_wraps` accordingly?

Example:

class MockRepro(unittest.TestCase)

@patch('foo')
def test_side_effect(self, mock_foo):
  # Set side effect in constructor as per https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock 
  Mock(side_effect = [1, 2])
  # Or can set on decorated patch
  foo.side_effect = [1, 2]

@patch('foo')
def test_wraps(self, mock_foo):
  def wrapped_method():
    return 3
  # Set wraps in constructor as per https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock 
  Mock(wraps=wrapped_method)
  # Or can set on decorated patch
  foo.wraps = wrapped_method # This silently fails
  foo._mock_wraps = wrapped_method # Where does `_mock_wraps` come from?
msg387760 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-27 02:33
Private attributes are not documented because they are private and subject to change.  Their use is at one's own risk.  I don't know mock well enough to understand 'inconsistent' or comment on the change proposal.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87439
2021-02-27 02:33:48terry.reedysetnosy: + terry.reedy
messages: + msg387760
2021-02-20 08:44:25Woodzcreate