Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3af125a4
Commit
3af125a4
authored
Apr 21, 2012
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments.
parent
2cd48738
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
8 deletions
+31
-8
Doc/library/inspect.rst
Doc/library/inspect.rst
+9
-4
Lib/unittest/mock.py
Lib/unittest/mock.py
+8
-3
Lib/unittest/test/testmock/testhelpers.py
Lib/unittest/test/testmock/testhelpers.py
+14
-1
No files found.
Doc/library/inspect.rst
View file @
3af125a4
...
@@ -440,11 +440,16 @@ Classes and functions
...
@@ -440,11 +440,16 @@ Classes and functions
locals dictionary of the given frame.
locals dictionary of the given frame.
.. function:: formatargspec(args[, varargs, varkw, defaults,
formatarg, formatvarargs, formatvarkw, formatvalue
])
.. function:: formatargspec(args[, varargs, varkw, defaults,
kwonlyargs, kwonlydefaults, annotations, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations
])
Format a pretty argument spec from the four values returned by
Format a pretty argument spec from the values returned by
:func:`getargspec`. The format\* arguments are the corresponding optional
:func:`getargspec` or :func:`getfullargspec`.
formatting functions that are called to turn names and values into strings.
The first seven arguments are (``args``, ``varargs``, ``varkw``,
``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``). The
other five arguments are the corresponding optional formatting functions
that are called to turn names and values into strings. The last argument
is an optional function to format the sequence of arguments.
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])
...
...
Lib/unittest/mock.py
View file @
3af125a4
...
@@ -78,11 +78,15 @@ def _getsignature(func, skipfirst, instance=False):
...
@@ -78,11 +78,15 @@ def _getsignature(func, skipfirst, instance=False):
return
return
try
:
try
:
regargs
,
varargs
,
varkwargs
,
defaults
=
inspect
.
get
argspec
(
func
)
argspec
=
inspect
.
getfull
argspec
(
func
)
except
TypeError
:
except
TypeError
:
# C function / method, possibly inherited object().__init__
# C function / method, possibly inherited object().__init__
return
return
# not using annotations
regargs
,
varargs
,
varkw
,
defaults
,
kwonly
,
kwonlydef
,
ann
=
argspec
# instance methods and classmethods need to lose the self argument
# instance methods and classmethods need to lose the self argument
if
getattr
(
func
,
'__self__'
,
None
)
is
not
None
:
if
getattr
(
func
,
'__self__'
,
None
)
is
not
None
:
regargs
=
regargs
[
1
:]
regargs
=
regargs
[
1
:]
...
@@ -90,8 +94,9 @@ def _getsignature(func, skipfirst, instance=False):
...
@@ -90,8 +94,9 @@ def _getsignature(func, skipfirst, instance=False):
# this condition and the above one are never both True - why?
# this condition and the above one are never both True - why?
regargs
=
regargs
[
1
:]
regargs
=
regargs
[
1
:]
signature
=
inspect
.
formatargspec
(
regargs
,
varargs
,
varkwargs
,
defaults
,
signature
=
inspect
.
formatargspec
(
formatvalue
=
lambda
value
:
""
)
regargs
,
varargs
,
varkw
,
defaults
,
kwonly
,
kwonlydef
,
ann
,
formatvalue
=
lambda
value
:
""
)
return
signature
[
1
:
-
1
],
func
return
signature
[
1
:
-
1
],
func
...
...
Lib/unittest/test/testmock/testhelpers.py
View file @
3af125a4
...
@@ -367,7 +367,7 @@ class SpecSignatureTest(unittest.TestCase):
...
@@ -367,7 +367,7 @@ class SpecSignatureTest(unittest.TestCase):
def
test_create_autospec_unbound_methods
(
self
):
def
test_create_autospec_unbound_methods
(
self
):
# see issue 128
# see
mock
issue 128
# this is expected to fail until the issue is fixed
# this is expected to fail until the issue is fixed
return
return
class
Foo
(
object
):
class
Foo
(
object
):
...
@@ -391,6 +391,19 @@ class SpecSignatureTest(unittest.TestCase):
...
@@ -391,6 +391,19 @@ class SpecSignatureTest(unittest.TestCase):
self
.
assertEqual
(
m
.
a
,
'3'
)
self
.
assertEqual
(
m
.
a
,
'3'
)
def
test_create_autospec_keyword_only_arguments
(
self
):
def
foo
(
a
,
*
,
b
=
None
):
pass
m
=
create_autospec
(
foo
)
m
(
1
)
m
.
assert_called_with
(
1
)
self
.
assertRaises
(
TypeError
,
m
,
1
,
2
)
m
(
2
,
b
=
3
)
m
.
assert_called_with
(
2
,
b
=
3
)
def
test_function_as_instance_attribute
(
self
):
def
test_function_as_instance_attribute
(
self
):
obj
=
SomeClass
()
obj
=
SomeClass
()
def
f
(
a
):
def
f
(
a
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment