aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2018-05-30 00:04:08 +0900
committerYury Selivanov <yury@magic.io>2018-05-29 11:04:08 -0400
commita9cab433bbf02f3a1de59d14dc8f583181ffe2d5 (patch)
tree0caa52b281b10d04c350ff0631ac24dd1418162e /Lib/inspect.py
parentbpo-33353: test_asyncio use set_write_buffer_limits() (GH-7200) (diff)
downloadcpython-a9cab433bbf02f3a1de59d14dc8f583181ffe2d5.tar.gz
cpython-a9cab433bbf02f3a1de59d14dc8f583181ffe2d5.tar.bz2
cpython-a9cab433bbf02f3a1de59d14dc8f583181ffe2d5.zip
bpo-33197: Update a error message of invalid inspect.Parameters. (GH-6636)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py47
1 files changed, 31 insertions, 16 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index e5d312eb302..409c05880de 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2402,6 +2402,16 @@ _VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
+_PARAM_NAME_MAPPING = {
+ _POSITIONAL_ONLY: 'positional-only',
+ _POSITIONAL_OR_KEYWORD: 'positional or keyword',
+ _VAR_POSITIONAL: 'variadic positional',
+ _KEYWORD_ONLY: 'keyword-only',
+ _VAR_KEYWORD: 'variadic keyword'
+}
+
+_get_paramkind_descr = _PARAM_NAME_MAPPING.__getitem__
+
class Parameter:
"""Represents a parameter in a function signature.
@@ -2436,15 +2446,14 @@ class Parameter:
empty = _empty
def __init__(self, name, kind, *, default=_empty, annotation=_empty):
-
- if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
- _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
- raise ValueError("invalid value for 'Parameter.kind' attribute")
- self._kind = kind
-
+ try:
+ self._kind = _ParameterKind(kind)
+ except ValueError:
+ raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
if default is not _empty:
- if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
- msg = '{} parameters cannot have default values'.format(kind)
+ if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
+ msg = '{} parameters cannot have default values'
+ msg = msg.format(_get_paramkind_descr(self._kind))
raise ValueError(msg)
self._default = default
self._annotation = annotation
@@ -2453,19 +2462,21 @@ class Parameter:
raise ValueError('name is a required attribute for Parameter')
if not isinstance(name, str):
- raise TypeError("name must be a str, not a {!r}".format(name))
+ msg = 'name must be a str, not a {}'.format(type(name).__name__)
+ raise TypeError(msg)
if name[0] == '.' and name[1:].isdigit():
# These are implicit arguments generated by comprehensions. In
# order to provide a friendlier interface to users, we recast
# their name as "implicitN" and treat them as positional-only.
# See issue 19611.
- if kind != _POSITIONAL_OR_KEYWORD:
- raise ValueError(
- 'implicit arguments must be passed in as {}'.format(
- _POSITIONAL_OR_KEYWORD
- )
+ if self._kind != _POSITIONAL_OR_KEYWORD:
+ msg = (
+ 'implicit arguments must be passed as '
+ 'positional or keyword arguments, not {}'
)
+ msg = msg.format(_get_paramkind_descr(self._kind))
+ raise ValueError(msg)
self._kind = _POSITIONAL_ONLY
name = 'implicit{}'.format(name[1:])
@@ -2736,8 +2747,12 @@ class Signature:
name = param.name
if kind < top_kind:
- msg = 'wrong parameter order: {!r} before {!r}'
- msg = msg.format(top_kind, kind)
+ msg = (
+ 'wrong parameter order: {} parameter before {} '
+ 'parameter'
+ )
+ msg = msg.format(_get_paramkind_descr(top_kind),
+ _get_paramkind_descr(kind))
raise ValueError(msg)
elif kind > top_kind:
kind_defaults = False