[issue28569] mock.attach_mock should work with return value of patch()

2016-10-31 Thread Andrey Fedorov

New submission from Andrey Fedorov:

The attach_mock in the following code sample fails silently:

>>> from mock import patch, Mock
>>> p = patch('requests.get', autospec=True)
>>> manager = Mock()
>>> manager.attach_mock(p.start(), 'requests_get')
>>> import requests
>>> requests.get('https://google.com')

>>> manager.mock_calls
[]
>>> p.stop()
>>> manager.mock_calls
[]

It seems this would be a useful use-case, especially given that this code would 
work as-expected if 'requests.get' in the second line were replaced with a path 
to a class.

--
components: Library (Lib)
messages: 279812
nosy: Andrey Fedorov, michael.foord
priority: normal
severity: normal
status: open
title: mock.attach_mock should work with return value of patch()
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

___
Python tracker 
<http://bugs.python.org/issue28569>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28569] mock.attach_mock should work with any return value of patch()

2016-10-31 Thread Andrey Fedorov

Changes by Andrey Fedorov :


--
title: mock.attach_mock should work with return value of patch() -> 
mock.attach_mock should work with any return value of patch()

___
Python tracker 
<http://bugs.python.org/issue28569>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28569] mock.attach_mock should work with any return value of patch()

2016-11-11 Thread Andrey Fedorov

Andrey Fedorov added the comment:

There's some vagueness on how this is implemented, but I would prefer
manager.attach_mock to also work with whatever the return value of patch()
is.

On Fri, Nov 11, 2016 at 12:08 PM, Syed Suhail Ahmed 
wrote:

>
> Syed Suhail Ahmed added the comment:
>
> So from what I have understood, manager.attach_mock must raise an
> Exception when it is called with a wrong attribute, since the patch is
> called with autospec=True and you cannot call a mock with non existing
> attributes.Is that correct?
>
> --
>
> ___
> Python tracker 
> <http://bugs.python.org/issue28569>
> ___
>

--

___
Python tracker 
<http://bugs.python.org/issue28569>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28569] mock.attach_mock should work with any return value of patch()

2016-11-11 Thread Andrey Fedorov

Andrey Fedorov added the comment:

To clarify, this is how I would expect these two functions to work together
"out of the box"

patches = { 'requests_get': 'requests.get', ... }

root_mock = mock.Mock()
for name, path in patches.items():
m = mock.patch(path, auto_spec=True)
root_mock.attach_mock(m, name)

This works when `path` is referring to a class, and it would be great if it
also worked with functions like `requests.get`, as well.

On Fri, Nov 11, 2016 at 5:16 PM, Andrey Fedorov  wrote:

> There's some vagueness on how this is implemented, but I would prefer
> manager.attach_mock to also work with whatever the return value of patch()
> is.
>
> On Fri, Nov 11, 2016 at 12:08 PM, Syed Suhail Ahmed <
> rep...@bugs.python.org> wrote:
>
>>
>> Syed Suhail Ahmed added the comment:
>>
>> So from what I have understood, manager.attach_mock must raise an
>> Exception when it is called with a wrong attribute, since the patch is
>> called with autospec=True and you cannot call a mock with non existing
>> attributes.Is that correct?
>>
>> --
>>
>> ___
>> Python tracker 
>> <http://bugs.python.org/issue28569>
>> ___
>>
>
>

--

___
Python tracker 
<http://bugs.python.org/issue28569>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28569] mock.attach_mock should work with any return value of patch()

2016-11-14 Thread Andrey Fedorov

Andrey Fedorov added the comment:

Here's the full extension of the example from documentation that doesn't
seem to handle classes and functions consistently:

  import mock

  patches = {
  'requests_get': 'requests.get',
  'mymodule_Class1': 'mymodule.Class1'
  }

  root_mock = mock.Mock()
  for name, path in patches.items():
  m = mock.patch(path, autospec=True)
  root_mock.attach_mock(m.start(), name)

  import requests
  import mymodule

  mymodule.Class1('foo')
  requests.get('bar')

  print root_mock.mock_calls
  # [call.mymodule_Class1('foo')]

Does this working as expected make sense, or is there some reason this is
an undesirable API to behave consistently regardless of what's being
patched?

--

___
Python tracker 
<http://bugs.python.org/issue28569>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com