[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-25 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Thank you all for the review. Closing it as fixed.

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-25 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:


New changeset 71d2b3344f4560ffee14fccd320b20e7add50fec by Karthikeyan 
Singaravelan (Miss Islington (bot)) in branch '3.7':
bpo-38473: Handle autospecced functions and methods used with attach_mock 
(GH-16784) (#18166)
https://github.com/python/cpython/commit/71d2b3344f4560ffee14fccd320b20e7add50fec


--

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-25 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:


New changeset a5906b2bfce9560568dee1dcc3550e74e742dd34 by Karthikeyan 
Singaravelan (Miss Islington (bot)) in branch '3.8':
bpo-38473: Handle autospecced functions and methods used with attach_mock 
(GH-16784) (GH-18167)
https://github.com/python/cpython/commit/a5906b2bfce9560568dee1dcc3550e74e742dd34


--

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-24 Thread miss-islington


Change by miss-islington :


--
pull_requests: +17552
pull_request: https://github.com/python/cpython/pull/18166

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-24 Thread miss-islington


Change by miss-islington :


--
pull_requests: +17553
pull_request: https://github.com/python/cpython/pull/18167

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2020-01-24 Thread Chris Withers


Chris Withers  added the comment:


New changeset 66b00a9d3aacf6ed49412f48743e4913104a2bb3 by Chris Withers 
(Karthikeyan Singaravelan) in branch 'master':
bpo-38473: Handle autospecced functions and methods used with attach_mock 
(GH-16784)
https://github.com/python/cpython/commit/66b00a9d3aacf6ed49412f48743e4913104a2bb3


--

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2019-10-14 Thread Gregory P. Smith


Change by Gregory P. Smith :


--
type:  -> behavior

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2019-10-14 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
keywords: +patch
pull_requests: +16344
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/16784

___
Python tracker 

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



[issue38473] AttributeError on asserting autospecced mock object added using attach_mock

2019-10-14 Thread Karthikeyan Singaravelan

New submission from Karthikeyan Singaravelan :

The following program causes AttributeError while retrieving the spec signature 
of a call. It seems that not all mocks specced should have _spec_signature 
where if autospec is used and the mock is attached with attach_mock then the 
"mock" attribute has the correct object from which _spec_signature has to be 
derived. On the attribute being not present we can fallback to the sig being 
None. This can be workaround by disabling autospec but since this is present in 
3.7.5RC1 and 3.8.0RC1 I am tagging it as regression. 

I am also attaching a patch with script to reproduce this that should pass with 
the patch. I will try to make a PR tonight. Sorry for the last minute report I 
just stumbled upon this while debugging 
https://bugs.python.org/issue21478#msg354489. This change was introduced as 
part of https://bugs.python.org/issue36871 by me. I am tagging the nosy list 
from issue for review of the patch.


import unittest
from unittest.mock import patch, Mock, call, ANY


class Foo:
def set_foo(self, val): pass


class FooTest(unittest.TestCase):
@patch(f"{__name__}.Foo.set_foo", autospec=True)
def test_autospec_attach_mock_assert(self, mock_set_foo):
manager = Mock()
manager.attach_mock(mock_set_foo, "set_foo_func")
obj = Foo()
obj.set_foo(3)
manager.assert_has_calls([call.set_foo_func(ANY, 3)])

if __name__ == "__main__":
unittest.main()


➜  Python-3.7.5rc1 ./python autospec_regression.py
E
==
ERROR: test_autospec_attach_mock_assert (__main__.FooTest)
--
Traceback (most recent call last):
  File "/home/xtreak/Python-3.7.5rc1/Lib/unittest/mock.py", line 1255, in 
patched
return func(*args, **keywargs)
  File "autospec_regression.py", line 16, in test_autospec_attach_mock_assert
manager.assert_has_calls([call.set_foo_func(ANY, 3)])
  File "/home/xtreak/Python-3.7.5rc1/Lib/unittest/mock.py", line 897, in 
assert_has_calls
expected = [self._call_matcher(c) for c in calls]
  File "/home/xtreak/Python-3.7.5rc1/Lib/unittest/mock.py", line 897, in 

expected = [self._call_matcher(c) for c in calls]
  File "/home/xtreak/Python-3.7.5rc1/Lib/unittest/mock.py", line 812, in 
_call_matcher
sig = self._get_call_signature_from_name(_call[0])
  File "/home/xtreak/Python-3.7.5rc1/Lib/unittest/mock.py", line 798, in 
_get_call_signature_from_name
sig = child._spec_signature
AttributeError: 'function' object has no attribute '_spec_signature'

--
Ran 1 test in 0.003s

FAILED (errors=1)

Patch : 


➜  Python-3.7.5rc1 diff -u Lib/unittest/mock.py Lib/unittest/mock_patched.py
--- Lib/unittest/mock.py2019-10-01 22:53:17.0 +0530
+++ Lib/unittest/mock_patched.py2019-10-14 19:18:00.038416294 +0530
@@ -795,7 +795,16 @@
 break
 else:
 children = child._mock_children
-sig = child._spec_signature
+# If an autospecced object is attached using attach_mock the
+# child would be a function with mock object as attribute from
+# which signature has to be derived. If there is no signature
+# attribute then fallback to None to ensure old signature is 
+# not used.
+child = _extract_mock(child)
+try:
+sig = child._spec_signature
+except AttributeError:
+sig = None
 
 return sig

--
components: Library (Lib)
keywords: 3.7regression, 3.8regression
messages: 354635
nosy: cjw296, gregory.p.smith, lisroach, lukasz.langa, mariocj89, 
michael.foord, ned.deily, xtreak
priority: normal
severity: normal
status: open
title: AttributeError on asserting autospecced mock object added using 
attach_mock
versions: Python 3.7, 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