Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-91803: Mock - fix error when using autospec methods with seal #92213

Merged
merged 12 commits into from
Nov 7, 2022

Conversation

akulakov
Copy link
Contributor

@akulakov akulakov commented May 3, 2022

@akulakov akulakov requested a review from cjw296 as a code owner May 3, 2022 05:19
@akulakov akulakov marked this pull request as draft May 3, 2022 05:19
…lakov/cpython into 91803-Fix-seal-with-autospec-methods
@akulakov akulakov marked this pull request as ready for review May 3, 2022 15:36
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label May 4, 2022
Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks correct to me. "Being callable" is clearly part of the contract of a callable that should be replicated by autospec, and seal should not remove that.

Lib/unittest/test/testmock/testsealable.py Outdated Show resolved Hide resolved
@@ -211,8 +212,6 @@ def ban(self):
foo.foo()
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.bar2()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing @sobolevn or @corona10 added this assertion for a reason, perhaps they could comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Nikita is largely on a break from open-source stuff right now FYI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is an incorrect assert since bar2 method is present on Foo and shouldn't raise. Removing the assert statement the AttributeError here asserted is for the exception that return_value is not found due to seal which this PR fixes and not that bar2 is not found.

./python Lib/unittest/test/testmock/testsealable.py
..............EE....
======================================================================
ERROR: test_seal_with_autospec (__main__.TestSealable.test_seal_with_autospec) (spec_set=True)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/test/testmock/testsealable.py", line 215, in test_seal_with_autospec
    foo.bar2()
    ^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1108, in __call__
    return self._mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1112, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1184, in _execute_mock_call
    return self.return_value
           ^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 638, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Mock object has no attribute 'return_value'

======================================================================
ERROR: test_seal_with_autospec (__main__.TestSealable.test_seal_with_autospec) (spec_set=False)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/test/testmock/testsealable.py", line 215, in test_seal_with_autospec
    foo.bar2()
    ^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1108, in __call__
    return self._mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1112, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 1184, in _execute_mock_call
    return self.return_value
           ^^^^^^^^^^^^^^^^^
  File "/home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py", line 638, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Mock object has no attribute 'return_value'

----------------------------------------------------------------------
Ran 19 tests in 0.121s

FAILED (errors=2)

@@ -2731,6 +2731,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
_new_parent=parent,
**kwargs)
mock._mock_children[entry] = new
new.return_value = Mock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use the child_klass to construct the mock object instead of Mock. This can break code that depended on the return value being an instance of child_klass. Example case as below :

from unittest.mock import create_autospec, seal

class Foo:

    def bar(self):
        pass

spec = create_autospec(Foo)
print(spec.bar())
print(len(spec.bar()))

Python 3.10

python3.10 gh92213.py  
<MagicMock name='mock.bar()' id='140499591708000'>
0

With patch

./python gh92213.py
<Mock name='mock.bar()' id='140578691284304'>
Traceback (most recent call last):
  File "/home/karthikeyan/stuff/python/cpython/gh92213.py", line 10, in <module>
    print(len(spec.bar()))
          ^^^^^^^^^^^^^^^
TypeError: object of type 'Mock' has no len()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me, I used Mock originally because I wasn't sure if MagicMock might "evade" seal() if set explicitly, but I tested it and it does not:

class Foo:
    x = 1
    def foo(self) -> int:
        return 0
foo=Foo()
foo = mock.create_autospec(foo)
foo.foo().x

==> AttributeError: mock.foo().x

I've pushed the update to use child_klass()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akulakov - rather than just manually testing this, please can you add a test to the test suite to ensure it doesn't change in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjw296 that makes sense, I've expanded the unit test - thanks for noting this.

Copy link
Member

@tirkarthi tirkarthi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the conflicts in tests. Changes look fine to me.

@akulakov
Copy link
Contributor Author

@tirkarthi thanks for letting me know, I fixed the conflict.

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Copy link
Member

@tirkarthi tirkarthi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @akulakov .

@tirkarthi
Copy link
Member

cc: @mariocj89

@iritkatriel
Copy link
Member

Updated branch to kick off test again.

@tirkarthi - you approved this back in May. Any reason why it was not merged yet?

@cjw296 cjw296 merged commit c6325b1 into python:main Nov 7, 2022
@akulakov
Copy link
Contributor Author

akulakov commented Nov 7, 2022

Thanks for reviewing @tirkarthi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants