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