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-85934: Use getattr_static when adding mock spec #22209

Merged
merged 13 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
17 changes: 17 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def cmeth(cls, a, b, c, d=None): pass
def smeth(a, b, c, d=None): pass


class SomethingElse(object):
def __init__(self):
self._instance = None

@property
def instance(self):
if not self._instance:
self._instance = 'object'
return self._instance


class Typos():
autospect = None
auto_spec = None
Expand Down Expand Up @@ -2293,6 +2304,12 @@ class Foo():
f'{__name__}.Typos', autospect=True, set_spec=True, auto_spec=True):
pass

def test_property_not_called_with_spec_mock(self):
obj = SomethingElse()
self.assertIsNone(obj._instance, msg='before mock')
mock = Mock(spec=obj)
self.assertIsNone(obj._instance, msg='after mock')
self.assertEqual('object', obj.instance)

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
spec_list = dir(spec)

for attr in spec_list:
if iscoroutinefunction(getattr(spec, attr, None)):
if iscoroutinefunction(inspect.getattr_static(spec, attr, None)):
_spec_asyncs.append(attr)

spec = spec_list
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unittest.mock speccing no longer calls class properties
melwitt marked this conversation as resolved.
Show resolved Hide resolved