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

2019-09-09 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I am closing this in favor of issue21478 as the original report is now fixed. 
Thanks.

--
resolution:  -> fixed
stage: needs patch -> resolved
status: open -> closed
superseder:  -> mock calls don't propagate to parent (autospec)

___
Python tracker 

___
___
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()

2019-07-17 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Is this similar to issue21478? Michael, can you please confirm this behavior 
and the change proposed in issue21478?

I tried out the below program as per initial report

# foo.py

from unittest.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')
print(manager.mock_calls)
p.stop()
print(manager.mock_calls)

# Without patch

$ python.exe foo.py
[]
[]

# With PR 14688 in issue21478

$ python.exe foo.py
[call.requests_get('https://google.com')]
[call.requests_get('https://google.com')]

--
nosy: +cjw296, mariocj89, xtreak
versions: +Python 3.8, Python 3.9

___
Python tracker 

___
___
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 

___
___
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 Michael Foord

Michael Foord added the comment:

Oh, I see what you mean - an attribute that doesn't exist on the original. With 
autospec that should throw an exception (AttributeError) I think, yes.

--

___
Python tracker 

___
___
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 Syed Suhail Ahmed

Syed Suhail Ahmed added the comment:

But since autospec is set as True, then shouldnt attach_mock throw an
exception when called with an attribute that doesn't exist?

On Mon, Nov 14, 2016 at 5:12 PM, Michael Foord 
wrote:

>
> Michael Foord added the comment:
>
> It should be perfectly valid to use attach_mock with an attribute that
> doesn't already exist. Part of it's purpose is to create new attributes.
>
> --
>
> ___
> Python tracker 
> 
> ___
>

--

___
Python tracker 

___
___
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 Michael Foord

Michael Foord added the comment:

It should be perfectly valid to use attach_mock with an attribute that doesn't 
already exist. Part of it's purpose is to create new attributes.

--

___
Python tracker 

___
___
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 
>> 
>> ___
>>
>
>

--

___
Python tracker 

___
___
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 
> 
> ___
>

--

___
Python tracker 

___
___
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 Syed Suhail Ahmed

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 

___
___
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-08 Thread Michael Foord

Michael Foord added the comment:

Sure, go ahead Syed. Feel free to ask any questions you may have.

--

___
Python tracker 

___
___
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-07 Thread Syed Suhail Ahmed

Syed Suhail Ahmed added the comment:

I would like to work on this issue.

--
nosy: +Syed Suhail Ahmed

___
Python tracker 

___
___
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-07 Thread Michael Foord

Michael Foord added the comment:

attach_mock should use function.mock when it is passed a function created by 
autospec. It should also *probably* fail if given a non-mock object (although 
that would prevent people duck-typing and attaching a mock-like object so I'm 
open to discussion on that).

--
stage:  -> needs patch
versions:  -Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6

___
Python tracker 

___
___
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 

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