summaryrefslogtreecommitdiff
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2014-01-29 10:58:16 -0500
committerYury Selivanov <yselivanov@sprymix.com>2014-01-29 10:58:16 -0500
commit3ffd50767fb0caf1f1127be3dfd153776bac6ec0 (patch)
tree23e4b90ca28c23212799b2a68e04285081d30ad1 /Lib/inspect.py
parent1ac96f5891cbf0b73b7b13c8eaad1714e62078af (diff)
downloadcpython-3ffd50767fb0caf1f1127be3dfd153776bac6ec0.tar.gz
inspect.Signature: ensure that non-default params don't follow default ones #20427
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index f0c12479ed..f06138c7a8 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1924,6 +1924,7 @@ class Signature:
if __validate_parameters__:
params = OrderedDict()
top_kind = _POSITIONAL_ONLY
+ kind_defaults = False
for idx, param in enumerate(parameters):
kind = param.kind
@@ -1933,9 +1934,27 @@ class Signature:
msg = 'wrong parameter order: {} before {}'
msg = msg.format(top_kind, kind)
raise ValueError(msg)
- else:
+ elif kind > top_kind:
+ kind_defaults = False
top_kind = kind
+ if (kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD) and
+ not param._partial_kwarg):
+ # If we have a positional-only or positional-or-keyword
+ # parameter, that does not have its default value set
+ # by 'functools.partial' or other "partial" signature:
+ if param.default is _empty:
+ if kind_defaults:
+ # No default for this parameter, but the
+ # previous parameter of the same kind had
+ # a default
+ msg = 'non-default argument follows default ' \
+ 'argument'
+ raise ValueError(msg)
+ else:
+ # There is a default for this parameter.
+ kind_defaults = True
+
if name in params:
msg = 'duplicate parameter name: {!r}'.format(name)
raise ValueError(msg)