Skip to content

bpo-45756: Fix mock triggers dynamic lookup via the descriptor protocol. #31348

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
_spec_signature = None
_spec_asyncs = []

for attr in dir(spec):
if iscoroutinefunction(getattr(spec, attr, None)):
_spec_asyncs.append(attr)
for attr, _ in inspect.getmembers_static(spec, iscoroutinefunction):
Copy link
Member

Choose a reason for hiding this comment

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

What about async classmethods? Would those still get recognized as coroutines?

_spec_asyncs.append(attr)

if spec is not None and not _is_list(spec):
if isinstance(spec, type):
Expand Down
14 changes: 14 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ def test_call(self):
"call_args_list not set")


def test_not_called_descriptor_protocol(self):
import types
class A:
@property
def name(self):
raise NotImplementedError
@types.DynamicClassAttribute
def eggs(self):
raise NotImplementedError

self.assertIsInstance(Mock(spec=A), A)
self.assertIsInstance(Mock(spec=A()), A)


def test_call_args_comparison(self):
mock = Mock()
mock()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`unittest.mock` triggers dynamic lookup via the descriptor
protocol. Patch by Weipeng Hong.