Skip to content

Commit a9cab43

Browse files
corona101st1
authored andcommitted
bpo-33197: Update a error message of invalid inspect.Parameters. (GH-6636)
1 parent 9551f77 commit a9cab43

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
@@ -2402,6 +2402,16 @@ def __str__(self):
24022402
_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
24032403
_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
24042404

2405+
_PARAM_NAME_MAPPING = {
2406+
_POSITIONAL_ONLY: 'positional-only',
2407+
_POSITIONAL_OR_KEYWORD: 'positional or keyword',
2408+
_VAR_POSITIONAL: 'variadic positional',
2409+
_KEYWORD_ONLY: 'keyword-only',
2410+
_VAR_KEYWORD: 'variadic keyword'
2411+
}
2412+
2413+
_get_paramkind_descr = _PARAM_NAME_MAPPING.__getitem__
2414+
24052415

24062416
class Parameter:
24072417
"""Represents a parameter in a function signature.
@@ -2436,15 +2446,14 @@ class Parameter:
24362446
empty = _empty
24372447

24382448
def __init__(self, name, kind, *, default=_empty, annotation=_empty):
2439-
2440-
if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2441-
_VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2442-
raise ValueError("invalid value for 'Parameter.kind' attribute")
2443-
self._kind = kind
2444-
2449+
try:
2450+
self._kind = _ParameterKind(kind)
2451+
except ValueError:
2452+
raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
24452453
if default is not _empty:
2446-
if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2447-
msg = '{} parameters cannot have default values'.format(kind)
2454+
if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2455+
msg = '{} parameters cannot have default values'
2456+
msg = msg.format(_get_paramkind_descr(self._kind))
24482457
raise ValueError(msg)
24492458
self._default = default
24502459
self._annotation = annotation
@@ -2453,19 +2462,21 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
24532462
raise ValueError('name is a required attribute for Parameter')
24542463

24552464
if not isinstance(name, str):
2456-
raise TypeError("name must be a str, not a {!r}".format(name))
2465+
msg = 'name must be a str, not a {}'.format(type(name).__name__)
2466+
raise TypeError(msg)
24572467

24582468
if name[0] == '.' and name[1:].isdigit():
24592469
# These are implicit arguments generated by comprehensions. In
24602470
# order to provide a friendlier interface to users, we recast
24612471
# their name as "implicitN" and treat them as positional-only.
24622472
# See issue 19611.
2463-
if kind != _POSITIONAL_OR_KEYWORD:
2464-
raise ValueError(
2465-
'implicit arguments must be passed in as {}'.format(
2466-
_POSITIONAL_OR_KEYWORD
2467-
)
2473+
if self._kind != _POSITIONAL_OR_KEYWORD:
2474+
msg = (
2475+
'implicit arguments must be passed as '
2476+
'positional or keyword arguments, not {}'
24682477
)
2478+
msg = msg.format(_get_paramkind_descr(self._kind))
2479+
raise ValueError(msg)
24692480
self._kind = _POSITIONAL_ONLY
24702481
name = 'implicit{}'.format(name[1:])
24712482

@@ -2736,8 +2747,12 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
27362747
name = param.name
27372748

27382749
if kind < top_kind:
2739-
msg = 'wrong parameter order: {!r} before {!r}'
2740-
msg = msg.format(top_kind, kind)
2750+
msg = (
2751+
'wrong parameter order: {} parameter before {} '
2752+
'parameter'
2753+
)
2754+
msg = msg.format(_get_paramkind_descr(top_kind),
2755+
_get_paramkind_descr(kind))
27412756
raise ValueError(msg)
27422757
elif kind > top_kind:
27432758
kind_defaults = False

Lib/test/test_inspect.py

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

1502+
# bpo-33197
1503+
with self.assertRaisesRegex(ValueError,
1504+
'variadic keyword parameters cannot'
1505+
' have default values'):
1506+
inspect.Parameter("foo", kind=inspect.Parameter.VAR_KEYWORD,
1507+
default=42)
1508+
with self.assertRaisesRegex(ValueError,
1509+
"value 5 is not a valid Parameter.kind"):
1510+
inspect.Parameter("bar", kind=5, default=42)
1511+
1512+
with self.assertRaisesRegex(TypeError,
1513+
'name must be a str, not a int'):
1514+
inspect.Parameter(123, kind=4)
1515+
15021516
class TestGetcallargsMethods(TestGetcallargsFunctions):
15031517

15041518
def setUp(self):
@@ -3099,7 +3113,8 @@ def test_signature_parameter_object(self):
30993113
self.assertIs(p.annotation, p.empty)
31003114
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
31013115

3102-
with self.assertRaisesRegex(ValueError, 'invalid value'):
3116+
with self.assertRaisesRegex(ValueError, "value '123' is "
3117+
"not a valid Parameter.kind"):
31033118
inspect.Parameter('foo', default=10, kind='123')
31043119

31053120
with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
@@ -3189,7 +3204,9 @@ def test_signature_parameter_replace(self):
31893204
self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD)
31903205
self.assertNotEqual(p2, p)
31913206

3192-
with self.assertRaisesRegex(ValueError, 'invalid value for'):
3207+
with self.assertRaisesRegex(ValueError,
3208+
"value <class 'inspect._empty'> "
3209+
"is not a valid Parameter.kind"):
31933210
p2 = p2.replace(kind=p2.empty)
31943211

31953212
p2 = p2.replace(kind=p2.KEYWORD_ONLY)
@@ -3202,7 +3219,9 @@ def test_signature_parameter_positional_only(self):
32023219
@cpython_only
32033220
def test_signature_parameter_implicit(self):
32043221
with self.assertRaisesRegex(ValueError,
3205-
'implicit arguments must be passed in as'):
3222+
'implicit arguments must be passed as '
3223+
'positional or keyword arguments, '
3224+
'not positional-only'):
32063225
inspect.Parameter('.0', kind=inspect.Parameter.POSITIONAL_ONLY)
32073226

32083227
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)