summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2022-01-16 06:41:43 (GMT)
committerGitHub <noreply@github.com>2022-01-16 06:41:43 (GMT)
commitacf7403f9baea3ae1119fc6b4a3298522188bf96 (patch)
treefcffbb83c601353ac1fce9b35b0f2424c8ce9899 /Lib/inspect.py
parent37eab55ac9da6b6361f136a1da15bfcef12ed954 (diff)
downloadcpython-acf7403f9baea3ae1119fc6b4a3298522188bf96.zip
cpython-acf7403f9baea3ae1119fc6b4a3298522188bf96.tar.gz
cpython-acf7403f9baea3ae1119fc6b4a3298522188bf96.tar.bz2
bpo-40066: [Enum] update str() and format() output (GH-30582)
Undo rejected PEP-663 changes: - restore `repr()` to its 3.10 status - restore `str()` to its 3.10 status New changes: - `IntEnum` and `IntFlag` now leave `__str__` as the original `int.__str__` so that str() and format() return the same result - zero-valued flags without a name have a slightly changed repr(), e.g. `repr(Color(0)) == '<Color: 0>'` - update `dir()` for mixed-in types to return all the methods and attributes of the mixed-in type - added `_numeric_repr_` to `Flag` to control display of unnamed values - enums without doc strings have a more comprehensive doc string added - `ReprEnum` added -- inheriting from this makes it so only `__repr__` is replaced, not `__str__` nor `__format__`; `IntEnum`, `IntFlag`, and `StrEnum` all inherit from `ReprEnum`
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py30
1 files changed, 14 insertions, 16 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 5d33f0d..8236698 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2567,15 +2567,21 @@ class _empty:
class _ParameterKind(enum.IntEnum):
- POSITIONAL_ONLY = 0
- POSITIONAL_OR_KEYWORD = 1
- VAR_POSITIONAL = 2
- KEYWORD_ONLY = 3
- VAR_KEYWORD = 4
+ POSITIONAL_ONLY = 'positional-only'
+ POSITIONAL_OR_KEYWORD = 'positional or keyword'
+ VAR_POSITIONAL = 'variadic positional'
+ KEYWORD_ONLY = 'keyword-only'
+ VAR_KEYWORD = 'variadic keyword'
+
+ def __new__(cls, description):
+ value = len(cls.__members__)
+ member = int.__new__(cls, value)
+ member._value_ = value
+ member.description = description
+ return member
- @property
- def description(self):
- return _PARAM_NAME_MAPPING[self]
+ def __str__(self):
+ return self.name
_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
@@ -2583,14 +2589,6 @@ _VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
-_PARAM_NAME_MAPPING = {
- _POSITIONAL_ONLY: 'positional-only',
- _POSITIONAL_OR_KEYWORD: 'positional or keyword',
- _VAR_POSITIONAL: 'variadic positional',
- _KEYWORD_ONLY: 'keyword-only',
- _VAR_KEYWORD: 'variadic keyword'
-}
-
class Parameter:
"""Represents a parameter in a function signature.