@@ -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
24032413class 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
0 commit comments