Skip to content

[3.7] bpo-36871: Ensure method signature is used when asserting mock calls to a method (GH13261) #16372

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
Closed
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
29 changes: 29 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,35 @@ def _get_call_signature_from_name(self, name):
return sig


def _get_call_signature_from_name(self, name):
"""
* If call objects are asserted against a method/function like obj.meth1
then there could be no name for the call object to lookup. Hence just
return the spec_signature of the method/function being asserted against.
* If the name is not empty then remove () and split by '.' to get
list of names to iterate through the children until a potential
match is found. A child mock is created only during attribute access
so if we get a _SpecState then no attributes of the spec were accessed
and can be safely exited.
"""
if not name:
return self._spec_signature

sig = None
names = name.replace('()', '').split('.')
children = self._mock_children

for name in names:
child = children.get(name)
if child is None or isinstance(child, _SpecState):
break
else:
children = child._mock_children
sig = child._spec_signature

return sig


def _call_matcher(self, _call):
"""
Given a call (or simply an (args, kwargs) tuple), return a
Expand Down