Skip to content

Commit 5e9e9db

Browse files
miss-islingtoncorona10
authored andcommitted
bpo-33197: Update a error message of invalid inspect.Parameters. (GH-6636) (#7205)
(cherry picked from commit a9cab43) Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
1 parent e151f83 commit 5e9e9db

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

‎Lib/inspect.py‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,16 @@ def __str__(self):
23992399
_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
24002400
_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
24012401

2402+
_PARAM_NAME_MAPPING = {
2403+
_POSITIONAL_ONLY: 'positional-only',
2404+
_POSITIONAL_OR_KEYWORD: 'positional or keyword',
2405+
_VAR_POSITIONAL: 'variadic positional',
2406+
_KEYWORD_ONLY: 'keyword-only',
2407+
_VAR_KEYWORD: 'variadic keyword'
2408+
}
2409+
2410+
_get_paramkind_descr = _PARAM_NAME_MAPPING.__getitem__
2411+
24022412

24032413
class Parameter:
24042414
"""Represents a parameter in a function signature.
@@ -2433,15 +2443,14 @@ class Parameter:
24332443
empty = _empty
24342444

24352445
def __init__(self, name, kind, *, default=_empty, annotation=_empty):
2436-
2437-
if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2438-
_VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2439-
raise ValueError("invalid value for 'Parameter.kind' attribute")
2440-
self._kind = kind
2441-
2446+
try:
2447+
self._kind = _ParameterKind(kind)
2448+
except ValueError:
2449+
raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
24422450
if default is not _empty:
2443-
if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2444-
msg = '{} parameters cannot have default values'.format(kind)
2451+
if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2452+
msg = '{} parameters cannot have default values'
2453+
msg = msg.format(_get_paramkind_descr(self._kind))
24452454
raise ValueError(msg)
24462455
self._default = default
24472456
self._annotation = annotation
@@ -2450,19 +2459,21 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
24502459
raise ValueError('name is a required attribute for Parameter')
24512460

24522461
if not isinstance(name, str):
2453-
raise TypeError("name must be a str, not a {!r}".format(name))
2462+
msg = 'name must be a str, not a {}'.format(type(name).__name__)
2463+
raise TypeError(msg)
24542464

24552465
if name[0] == '.' and name[1:].isdigit():
24562466
# These are implicit arguments generated by comprehensions. In
24572467
# order to provide a friendlier interface to users, we recast
24582468
# their name as "implicitN" and treat them as positional-only.
24592469
# See issue 19611.
2460-
if kind != _POSITIONAL_OR_KEYWORD:
2461-
raise ValueError(
2462-
'implicit arguments must be passed in as {}'.format(
2463-
_POSITIONAL_OR_KEYWORD
2464-
)
2470+
if self._kind != _POSITIONAL_OR_KEYWORD:
2471+
msg = (
2472+
'implicit arguments must be passed as '
2473+
'positional or keyword arguments, not {}'
24652474
)
2475+
msg = msg.format(_get_paramkind_descr(self._kind))
2476+
raise ValueError(msg)
24662477
self._kind = _POSITIONAL_ONLY
24672478
name = 'implicit{}'.format(name[1:])
24682479

@@ -2730,8 +2741,12 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
27302741
name = param.name
27312742

27322743
if kind < top_kind:
2733-
msg = 'wrong parameter order: {!r} before {!r}'
2734-
msg = msg.format(top_kind, kind)
2744+
msg = (
2745+
'wrong parameter order: {} parameter before {} '
2746+
'parameter'
2747+
)
2748+
msg = msg.format(_get_paramkind_descr(top_kind),
2749+
_get_paramkind_descr(kind))
27352750
raise ValueError(msg)
27362751
elif kind > top_kind:
27372752
kind_defaults = False

‎Lib/test/test_inspect.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,20 @@ def f6(a, b, c):
13901390
with self.assertRaisesRegex(TypeError, "'a', 'b' and 'c'"):
13911391
inspect.getcallargs(f6)
13921392

1393+
# bpo-33197
1394+
with self.assertRaisesRegex(ValueError,
1395+
'variadic keyword parameters cannot'
1396+
' have default values'):
1397+
inspect.Parameter("foo", kind=inspect.Parameter.VAR_KEYWORD,
1398+
default=42)
1399+
with self.assertRaisesRegex(ValueError,
1400+
"value 5 is not a valid Parameter.kind"):
1401+
inspect.Parameter("bar", kind=5, default=42)
1402+
1403+
with self.assertRaisesRegex(TypeError,
1404+
'name must be a str, not a int'):
1405+
inspect.Parameter(123, kind=4)
1406+
13931407
class TestGetcallargsMethods(TestGetcallargsFunctions):
13941408

13951409
def setUp(self):
@@ -2979,7 +2993,8 @@ def test_signature_parameter_object(self):
29792993
self.assertIs(p.annotation, p.empty)
29802994
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
29812995

2982-
with self.assertRaisesRegex(ValueError, 'invalid value'):
2996+
with self.assertRaisesRegex(ValueError, "value '123' is "
2997+
"not a valid Parameter.kind"):
29832998
inspect.Parameter('foo', default=10, kind='123')
29842999

29853000
with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
@@ -3069,7 +3084,9 @@ def test_signature_parameter_replace(self):
30693084
self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD)
30703085
self.assertNotEqual(p2, p)
30713086

3072-
with self.assertRaisesRegex(ValueError, 'invalid value for'):
3087+
with self.assertRaisesRegex(ValueError,
3088+
"value <class 'inspect._empty'> "
3089+
"is not a valid Parameter.kind"):
30733090
p2 = p2.replace(kind=p2.empty)
30743091

30753092
p2 = p2.replace(kind=p2.KEYWORD_ONLY)
@@ -3082,7 +3099,9 @@ def test_signature_parameter_positional_only(self):
30823099
@cpython_only
30833100
def test_signature_parameter_implicit(self):
30843101
with self.assertRaisesRegex(ValueError,
3085-
'implicit arguments must be passed in as'):
3102+
'implicit arguments must be passed as '
3103+
'positional or keyword arguments, '
3104+
'not positional-only'):
30863105
inspect.Parameter('.0', kind=inspect.Parameter.POSITIONAL_ONLY)
30873106

30883107
param = inspect.Parameter(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update error message when constructing invalid inspect.Parameters
2+
Patch by Dong-hee Na.

0 commit comments

Comments
 (0)