-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-17185: Add __signature__ to mock that can be used by inspect for signature #11048
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce36d17
Fix partial and partial method signatures in mock
tirkarthi e73a549
Add more calls
tirkarthi bf5289b
Add NEWS entry
tirkarthi 3b3f805
Use assertEquals and fix markup in NEWS
tirkarthi 21bfc2c
Refactor branching and add markup reference for functools
tirkarthi 68bdf07
Revert partial object related changes and fix pr comments
tirkarthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import inspect | ||
| import time | ||
| import types | ||
| import unittest | ||
|
|
@@ -901,6 +902,35 @@ def __getattr__(self, attribute): | |
| self.assertFalse(hasattr(autospec, '__name__')) | ||
|
|
||
|
|
||
| def test_spec_inspect_signature(self): | ||
|
|
||
| def myfunc(x, y): | ||
| pass | ||
|
|
||
| mock = create_autospec(myfunc) | ||
| mock(1, 2) | ||
| mock(x=1, y=2) | ||
|
|
||
| self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(myfunc)) | ||
| self.assertEqual(mock.mock_calls, [call(1, 2), call(x=1, y=2)]) | ||
| self.assertRaises(TypeError, mock, 1) | ||
|
|
||
|
|
||
| def test_spec_inspect_signature_annotations(self): | ||
|
|
||
| def foo(a: int, b: int=10, *, c:int) -> int: | ||
| return a + b + c | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please split this out into its own test case. |
||
|
|
||
| mock = create_autospec(foo) | ||
| mock(1, 2, c=3) | ||
| mock(1, c=3) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(foo)) | ||
| self.assertEqual(mock.mock_calls, [call(1, 2, c=3), call(1, c=3)]) | ||
| self.assertRaises(TypeError, mock, 1) | ||
| self.assertRaises(TypeError, mock, 1, 2, 3, c=4) | ||
|
|
||
|
|
||
| class TestCallList(unittest.TestCase): | ||
|
|
||
| def test_args_list_contains_call_list(self): | ||
|
|
||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2018-12-09-17-04-15.bpo-17185.SfSCJF.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Set ``__signature__`` on mock for :mod:`inspect` to get signature. | ||
| Patch by Karthikeyan Singaravelan. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to see some assertions about
mock_callshere.