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-96127: Fix `inspect.signature` call on mocks by sobolevn · Pull Request #96335 · python/cpython

gh-96127: Fix inspect.signature call on mocks #96335

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
cjw296 merged 1 commit into python:main from sobolevn:issue-96127
Jan 7, 2023

Uh oh!

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

Merged

gh-96127: Fix inspect.signature call on mocks #96335

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
d9c74a3
gh-96127: Fix `inspect.signature` call on mocks
sobolevn Aug 27, 2022
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

19 changes: 19 additions & 0 deletions 19 Lib/test/test_inspect.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,25 @@ def test_signature_on_lambdas(self):
((('a', 10, ..., "positional_or_keyword"),),
...))

def test_signature_on_mocks(self):
# https://github.com/python/cpython/issues/96127
for mock in (
unittest.mock.Mock(),
unittest.mock.AsyncMock(),
unittest.mock.MagicMock(),
):
with self.subTest(mock=mock):
self.assertEqual(str(inspect.signature(mock)), '(*args, **kwargs)')

def test_signature_on_noncallable_mocks(self):
for mock in (
unittest.mock.NonCallableMock(),
unittest.mock.NonCallableMagicMock(),
):
with self.subTest(mock=mock):
with self.assertRaises(TypeError):
inspect.signature(mock)

def test_signature_equality(self):
def foo(a, *, b:int) -> float: pass
self.assertFalse(inspect.signature(foo) == 42)
Expand Down
10 changes: 9 additions & 1 deletion 10 Lib/unittest/mock.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,15 @@ def __init__(self, /, *args, **kwargs):
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
code_mock.__dict__["_spec_class"] = CodeType
code_mock.__dict__["_spec_signature"] = _CODE_SIG
code_mock.co_flags = inspect.CO_COROUTINE
code_mock.co_flags = (
Comment thread
cjw296 marked this conversation as resolved.
Resolved

Uh oh!

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

inspect.CO_COROUTINE
+ inspect.CO_VARARGS
+ inspect.CO_VARKEYWORDS
)
code_mock.co_argcount = 0
code_mock.co_varnames = ('args', 'kwargs')
code_mock.co_posonlyargcount = 0
code_mock.co_kwonlyargcount = 0
Comment thread
cjw296 marked this conversation as resolved.
Resolved

Uh oh!

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

self.__dict__['__code__'] = code_mock
self.__dict__['__name__'] = 'AsyncMock'
self.__dict__['__defaults__'] = tuple()
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2022-08-27-10-35-50.gh-issue-96127.8RdLre.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``inspect.signature`` was raising ``TypeError`` on call with mock objects.
Now it correctly returns ``(*args, **kwargs)`` as infered signature.
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.