Skip to content

bpo-33197: Add description property for _ParameterKind #7206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,23 @@ function.
... print('Parameter:', param)
Parameter: c

.. attribute:: Parameter.kind.description

Describes a enum value of Parameter.kind.

Example: print all descriptions of arguments::

>>> def foo(a, b, *, c, d=10):
... pass

>>> sig = signature(foo)
>>> for param in sig.parameters.values():
... print(param.kind.description)
positional or keyword
positional or keyword
keyword-only
keyword-only

.. method:: Parameter.replace(*[, name][, kind][, default][, annotation])

Create a new Parameter instance based on the instance replaced was invoked
Expand Down
13 changes: 7 additions & 6 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2395,6 +2395,9 @@ class _ParameterKind(enum.IntEnum):
def __str__(self):
return self._name_

@property
def description(self):
return _PARAM_NAME_MAPPING[self]

_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
Expand All @@ -2410,8 +2413,6 @@ def __str__(self):
_VAR_KEYWORD: 'variadic keyword'
}

_get_paramkind_descr = _PARAM_NAME_MAPPING.__getitem__


class Parameter:
"""Represents a parameter in a function signature.
Expand Down Expand Up @@ -2453,7 +2454,7 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
if default is not _empty:
if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
msg = '{} parameters cannot have default values'
msg = msg.format(_get_paramkind_descr(self._kind))
msg = msg.format(self._kind.description)
raise ValueError(msg)
self._default = default
self._annotation = annotation
Expand All @@ -2475,7 +2476,7 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
'implicit arguments must be passed as '
'positional or keyword arguments, not {}'
)
msg = msg.format(_get_paramkind_descr(self._kind))
msg = msg.format(self._kind.description)
raise ValueError(msg)
self._kind = _POSITIONAL_ONLY
name = 'implicit{}'.format(name[1:])
Expand Down Expand Up @@ -2751,8 +2752,8 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
'wrong parameter order: {} parameter before {} '
'parameter'
)
msg = msg.format(_get_paramkind_descr(top_kind),
_get_paramkind_descr(kind))
msg = msg.format(top_kind.description,
kind.description)
raise ValueError(msg)
elif kind > top_kind:
kind_defaults = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add description property for _ParameterKind