[issue41915] unittest.mock.create_autospec(Obj, instance=True) has self keyword in _spec_signature if Obj implements __call__

2020-12-04 Thread Pierre Ossman


Pierre Ossman  added the comment:

autospec's behaviour for methods is currently needed to work around Issue42556, 
so be careful with any fixes here so they don't break that workaround.

--
nosy: +CendioOssman

___
Python tracker 

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



[issue41915] unittest.mock.create_autospec(Obj, instance=True) has self keyword in _spec_signature if Obj implements __call__

2020-12-03 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

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



[issue41915] unittest.mock.create_autospec(Obj, instance=True) has self keyword in _spec_signature if Obj implements __call__

2020-10-02 Thread Ethan Tang US


New submission from Ethan Tang US :

When an `unittest.mock.create_autospec(Obj, instance=True)` is used, and said 
Obj implements the `__call__(self, ...)` function, the mocked instance of it 
will take the full function signature including the `self` argument.

This will then cause an issue when `assert_called_once_with()` or 
`assert_called_with()` is used. Those two assertions will fail regardless if 
the arguments used were correct or not. The error message will contain the 
error: `TypeError: missing a required argument: 'self'`

Here an example of this issue happening

```
from unittest import mock

def Foo(object):

def __call__(self, a):
pass

def bar(self, b):
```This is to just compare it with a regular function.```
pass

foo_mock = mock.create_autospec(Foo, instance=True)
foo_mock(a=1)

foo_mock.assert_called_once_with(a=1)
```

In the example above, the assertion will then fail and raise an error even 
though the assertion should be correct.

upon inspecting further to understand the issue, this is because 
`foo_mock._spec_signature` will be ``.

The existence of `self` in `foo_mock._spec_signature` will cause this error.

As compared to the method `foo_mock.bar._spec_signature`, it will only be 
``.

The reason for me posting this issue is that development with the Keras library 
heavily uses __call__ in their API and therefore it is hard to mock the APIs if 
such issue exists. One work around would be iterating through its 
`called_args_list`.

--
components: Library (Lib)
messages: 377846
nosy: ettang
priority: normal
severity: normal
status: open
title: unittest.mock.create_autospec(Obj, instance=True) has self keyword in 
_spec_signature if Obj implements __call__
versions: Python 3.6

___
Python tracker 

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