@@ -2402,6 +2402,16 @@ def __str__(self):
2402
2402
_KEYWORD_ONLY = _ParameterKind .KEYWORD_ONLY
2403
2403
_VAR_KEYWORD = _ParameterKind .VAR_KEYWORD
2404
2404
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
+
2405
2415
2406
2416
class Parameter :
2407
2417
"""Represents a parameter in a function signature.
@@ -2436,15 +2446,14 @@ class Parameter:
2436
2446
empty = _empty
2437
2447
2438
2448
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' )
2445
2453
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 ))
2448
2457
raise ValueError (msg )
2449
2458
self ._default = default
2450
2459
self ._annotation = annotation
@@ -2453,19 +2462,21 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
2453
2462
raise ValueError ('name is a required attribute for Parameter' )
2454
2463
2455
2464
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 )
2457
2467
2458
2468
if name [0 ] == '.' and name [1 :].isdigit ():
2459
2469
# These are implicit arguments generated by comprehensions. In
2460
2470
# order to provide a friendlier interface to users, we recast
2461
2471
# their name as "implicitN" and treat them as positional-only.
2462
2472
# 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 {}'
2468
2477
)
2478
+ msg = msg .format (_get_paramkind_descr (self ._kind ))
2479
+ raise ValueError (msg )
2469
2480
self ._kind = _POSITIONAL_ONLY
2470
2481
name = 'implicit{}' .format (name [1 :])
2471
2482
@@ -2736,8 +2747,12 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
2736
2747
name = param .name
2737
2748
2738
2749
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 ))
2741
2756
raise ValueError (msg )
2742
2757
elif kind > top_kind :
2743
2758
kind_defaults = False
0 commit comments