GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-85934: Use getattr_static when adding mock spec by melwitt · Pull Request #22209 · python/cpython

gh-85934: Use getattr_static when adding mock spec #22209

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.

Sign up for GitHub

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

Merged
carljm merged 13 commits into python:main from melwitt:unittest.mock-getattr
May 23, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-85934: Use getattr_static when adding mock spec #22209

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
0a61770
bpo-41768: Use getattr_static when adding mock spec
melwitt Sep 12, 2020
943f8d3
📜🤖 Added by blurb_it.
blurb-it[bot] Sep 16, 2020
4bdc642
Return object from SomethingElse.instance property
melwitt Sep 19, 2020
2e05daa
Apply suggestions from code review
melwitt Sep 19, 2020
a916d4b
Clean up whitespace damage and adjust assert
melwitt Sep 19, 2020
fad2710
Merge branch 'main' of github.com:python/cpython into unittest.mock-g…
melwitt Nov 1, 2022
d953168
Add newline at the end of NEWS entry
melwitt Nov 1, 2022
3f7a9f2
Merge branch 'main' into unittest.mock-getattr
terryjreedy Apr 27, 2023
fcb1fc8
Merge branch 'main' of https://github.com/python/cpython into unittes…
melwitt May 4, 2023
698856c
Move getattr_static call into existing loop
melwitt May 4, 2023
9baa71f
Update Misc/NEWS.d/next/Library/2020-09-16-16-53-06.bpo-41768.8_fWkC.rst
melwitt May 4, 2023
0ab362e
Merge branch 'python:main' into unittest.mock-getattr
melwitt May 16, 2023
a20b360
Handle async methods decorated with @classmethod or @staticmethod
melwitt May 16, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

31 changes: 31 additions & 0 deletions 31 Lib/test/test_unittest/testmock/testmock.py
Show comments
View file
Open in desktop
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,26 @@ 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)

def test_decorated_async_methods_with_spec_mock(self):
class Foo():
@classmethod
async def class_method(cls):
pass
@staticmethod
async def static_method():
pass
async def method(self):
pass
mock = Mock(spec=Foo)
for m in (mock.method, mock.class_method, mock.static_method):
self.assertIsInstance(m, AsyncMock)

if __name__ == '__main__':
unittest.main()
8 changes: 7 additions & 1 deletion 8 Lib/unittest/mock.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,13 @@ 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)):
static_attr = inspect.getattr_static(spec, attr, None)
unwrapped_attr = static_attr
try:
unwrapped_attr = inspect.unwrap(unwrapped_attr)
except ValueError:
pass
if iscoroutinefunction(unwrapped_attr):
_spec_asyncs.append(attr)

spec = spec_list
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2020-09-16-16-53-06.bpo-41768.8_fWkC.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`unittest.mock` speccing no longer calls class properties.
Patch by Melanie Witt.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.