Skip to content

Commit acf7403

Browse files
authored
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`
1 parent 37eab55 commit acf7403

14 files changed

+2104
-2038
lines changed

Doc/howto/enum.rst

+126-146
Large diffs are not rendered by default.

Doc/library/enum.rst

+173-92
Large diffs are not rendered by default.

Doc/library/ssl.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,7 @@ to speed up repeated connections from the same clients.
20702070
:attr:`SSLContext.verify_flags` returns :class:`VerifyFlags` flags:
20712071

20722072
>>> ssl.create_default_context().verify_flags # doctest: +SKIP
2073-
ssl.VERIFY_X509_TRUSTED_FIRST
2073+
<VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>
20742074

20752075
.. attribute:: SSLContext.verify_mode
20762076

@@ -2082,7 +2082,7 @@ to speed up repeated connections from the same clients.
20822082
:attr:`SSLContext.verify_mode` returns :class:`VerifyMode` enum:
20832083

20842084
>>> ssl.create_default_context().verify_mode
2085-
ssl.CERT_REQUIRED
2085+
<VerifyMode.CERT_REQUIRED: 2>
20862086

20872087
.. index:: single: certificates
20882088

Lib/enum.py

+371-232
Large diffs are not rendered by default.

Lib/inspect.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -2567,30 +2567,28 @@ class _empty:
25672567

25682568

25692569
class _ParameterKind(enum.IntEnum):
2570-
POSITIONAL_ONLY = 0
2571-
POSITIONAL_OR_KEYWORD = 1
2572-
VAR_POSITIONAL = 2
2573-
KEYWORD_ONLY = 3
2574-
VAR_KEYWORD = 4
2570+
POSITIONAL_ONLY = 'positional-only'
2571+
POSITIONAL_OR_KEYWORD = 'positional or keyword'
2572+
VAR_POSITIONAL = 'variadic positional'
2573+
KEYWORD_ONLY = 'keyword-only'
2574+
VAR_KEYWORD = 'variadic keyword'
2575+
2576+
def __new__(cls, description):
2577+
value = len(cls.__members__)
2578+
member = int.__new__(cls, value)
2579+
member._value_ = value
2580+
member.description = description
2581+
return member
25752582

2576-
@property
2577-
def description(self):
2578-
return _PARAM_NAME_MAPPING[self]
2583+
def __str__(self):
2584+
return self.name
25792585

25802586
_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
25812587
_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
25822588
_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
25832589
_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
25842590
_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
25852591

2586-
_PARAM_NAME_MAPPING = {
2587-
_POSITIONAL_ONLY: 'positional-only',
2588-
_POSITIONAL_OR_KEYWORD: 'positional or keyword',
2589-
_VAR_POSITIONAL: 'variadic positional',
2590-
_KEYWORD_ONLY: 'keyword-only',
2591-
_VAR_KEYWORD: 'variadic keyword'
2592-
}
2593-
25942592

25952593
class Parameter:
25962594
"""Represents a parameter in a function signature.

Lib/plistlib.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
from xml.parsers.expat import ParserCreate
6262

6363

64-
PlistFormat = enum.global_enum(enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__))
64+
PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
65+
globals().update(PlistFormat.__members__)
6566

6667

6768
class UID:

Lib/re.py

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class RegexFlag:
155155
# sre extensions (experimental, don't rely on these)
156156
TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
157157
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
158+
__str__ = object.__str__
159+
_numeric_repr_ = hex
158160

159161
# sre exception
160162
error = sre_compile.error

Lib/ssl.py

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
)
120120
from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION
121121

122-
123122
_IntEnum._convert_(
124123
'_SSLMethod', __name__,
125124
lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',

0 commit comments

Comments
 (0)