Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
b907a513
Kaydet (Commit)
b907a513
authored
May 16, 2015
tarafından
Yury Selivanov
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 24190: Add inspect.BoundArguments.apply_defaults() method.
üst
1392f71c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
21 deletions
+91
-21
inspect.rst
Doc/library/inspect.rst
+20
-21
3.5.rst
Doc/whatsnew/3.5.rst
+3
-0
inspect.py
Lib/inspect.py
+30
-0
test_inspect.py
Lib/test/test_inspect.py
+35
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/inspect.rst
Dosyayı görüntüle @
b907a513
...
@@ -667,27 +667,8 @@ function.
...
@@ -667,27 +667,8 @@ function.
Arguments for which :meth:`Signature.bind` or
Arguments for which :meth:`Signature.bind` or
:meth:`Signature.bind_partial` relied on a default value are skipped.
:meth:`Signature.bind_partial` relied on a default value are skipped.
However, if needed, it is easy to include them.
However, if needed, use :meth:`BoundArguments.apply_defaults` to add
them.
::
>>> def foo(a, b=10):
... pass
>>> sig = signature(foo)
>>> ba = sig.bind(5)
>>> ba.args, ba.kwargs
((5,), {})
>>> for param in sig.parameters.values():
... if (param.name not in ba.arguments
... and param.default is not param.empty):
... ba.arguments[param.name] = param.default
>>> ba.args, ba.kwargs
((5, 10), {})
.. attribute:: BoundArguments.args
.. attribute:: BoundArguments.args
...
@@ -703,6 +684,24 @@ function.
...
@@ -703,6 +684,24 @@ function.
A reference to the parent :class:`Signature` object.
A reference to the parent :class:`Signature` object.
.. method:: BoundArguments.apply_defaults()
Set default values for missing arguments.
For variable-positional arguments (``*args``) the default is an
empty tuple.
For variable-keyword arguments (``**kwargs``) the default is an
empty dict.
::
>>> def foo(a, b='ham', *args): pass
>>> ba = inspect.signature(foo).bind('spam')
>>> ba.apply_defaults()
>>> ba.arguments
OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
functions::
functions::
...
...
Doc/whatsnew/3.5.rst
Dosyayı görüntüle @
b907a513
...
@@ -400,6 +400,9 @@ inspect
...
@@ -400,6 +400,9 @@ inspect
picklable and hashable. (Contributed by Yury Selivanov in :issue:`20726`
picklable and hashable. (Contributed by Yury Selivanov in :issue:`20726`
and :issue:`20334`.)
and :issue:`20334`.)
* New method :meth:`inspect.BoundArguments.apply_defaults`. (Contributed
by Yury Selivanov in :issue:`24190`.)
* New class method :meth:`inspect.Signature.from_callable`, which makes
* New class method :meth:`inspect.Signature.from_callable`, which makes
subclassing of :class:`~inspect.Signature` easier. (Contributed
subclassing of :class:`~inspect.Signature` easier. (Contributed
by Yury Selivanov and Eric Snow in :issue:`17373`.)
by Yury Selivanov and Eric Snow in :issue:`17373`.)
...
...
Lib/inspect.py
Dosyayı görüntüle @
b907a513
...
@@ -2443,6 +2443,36 @@ class BoundArguments:
...
@@ -2443,6 +2443,36 @@ class BoundArguments:
return
kwargs
return
kwargs
def
apply_defaults
(
self
):
"""Set default values for missing arguments.
For variable-positional arguments (*args) the default is an
empty tuple.
For variable-keyword arguments (**kwargs) the default is an
empty dict.
"""
arguments
=
self
.
arguments
if
not
arguments
:
return
new_arguments
=
[]
for
name
,
param
in
self
.
_signature
.
parameters
.
items
():
try
:
new_arguments
.
append
((
name
,
arguments
[
name
]))
except
KeyError
:
if
param
.
default
is
not
_empty
:
val
=
param
.
default
elif
param
.
kind
is
_VAR_POSITIONAL
:
val
=
()
elif
param
.
kind
is
_VAR_KEYWORD
:
val
=
{}
else
:
# This BoundArguments was likely produced by
# Signature.bind_partial().
continue
new_arguments
.
append
((
name
,
val
))
self
.
arguments
=
OrderedDict
(
new_arguments
)
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
(
self
is
other
or
return
(
self
is
other
or
(
issubclass
(
other
.
__class__
,
BoundArguments
)
and
(
issubclass
(
other
.
__class__
,
BoundArguments
)
and
...
...
Lib/test/test_inspect.py
Dosyayı görüntüle @
b907a513
...
@@ -3153,6 +3153,41 @@ class TestBoundArguments(unittest.TestCase):
...
@@ -3153,6 +3153,41 @@ class TestBoundArguments(unittest.TestCase):
ba
=
sig
.
bind
(
20
,
30
,
z
=
{})
ba
=
sig
.
bind
(
20
,
30
,
z
=
{})
self
.
assertRegex
(
repr
(
ba
),
r'<BoundArguments \(a=20,.*\}\}\)>'
)
self
.
assertRegex
(
repr
(
ba
),
r'<BoundArguments \(a=20,.*\}\}\)>'
)
def
test_signature_bound_arguments_apply_defaults
(
self
):
def
foo
(
a
,
b
=
1
,
*
args
,
c
:
1
=
{},
**
kw
):
pass
sig
=
inspect
.
signature
(
foo
)
ba
=
sig
.
bind
(
20
)
ba
.
apply_defaults
()
self
.
assertEqual
(
list
(
ba
.
arguments
.
items
()),
[(
'a'
,
20
),
(
'b'
,
1
),
(
'args'
,
()),
(
'c'
,
{}),
(
'kw'
,
{})])
# Make sure that we preserve the order:
# i.e. 'c' should be *before* 'kw'.
ba
=
sig
.
bind
(
10
,
20
,
30
,
d
=
1
)
ba
.
apply_defaults
()
self
.
assertEqual
(
list
(
ba
.
arguments
.
items
()),
[(
'a'
,
10
),
(
'b'
,
20
),
(
'args'
,
(
30
,)),
(
'c'
,
{}),
(
'kw'
,
{
'd'
:
1
})])
# Make sure that BoundArguments produced by bind_partial()
# are supported.
def
foo
(
a
,
b
):
pass
sig
=
inspect
.
signature
(
foo
)
ba
=
sig
.
bind_partial
(
20
)
ba
.
apply_defaults
()
self
.
assertEqual
(
list
(
ba
.
arguments
.
items
()),
[(
'a'
,
20
)])
# Test no args
def
foo
():
pass
sig
=
inspect
.
signature
(
foo
)
ba
=
sig
.
bind
()
ba
.
apply_defaults
()
self
.
assertEqual
(
list
(
ba
.
arguments
.
items
()),
[])
class
TestSignaturePrivateHelpers
(
unittest
.
TestCase
):
class
TestSignaturePrivateHelpers
(
unittest
.
TestCase
):
def
test_signature_get_bound_param
(
self
):
def
test_signature_get_bound_param
(
self
):
...
...
Misc/NEWS
Dosyayı görüntüle @
b907a513
...
@@ -131,6 +131,9 @@ Library
...
@@ -131,6 +131,9 @@ Library
-
Issue
22547
:
Implement
informative
__repr__
for
inspect
.
BoundArguments
.
-
Issue
22547
:
Implement
informative
__repr__
for
inspect
.
BoundArguments
.
Contributed
by
Yury
Selivanov
.
Contributed
by
Yury
Selivanov
.
-
Issue
24190
:
Implement
inspect
.
BoundArgument
.
apply_defaults
()
method
.
Contributed
by
Yury
Selivanov
.
Tests
Tests
-----
-----
...
...
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