Skip to content
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

bpo-40066: [Enum] update str() and format() output #30582

Merged
merged 26 commits into from
Jan 16, 2022
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
272 changes: 126 additions & 146 deletions Doc/howto/enum.rst

Large diffs are not rendered by default.

265 changes: 173 additions & 92 deletions Doc/library/enum.rst

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Doc/library/ssl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ to speed up repeated connections from the same clients.
:attr:`SSLContext.verify_flags` returns :class:`VerifyFlags` flags:

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

.. attribute:: SSLContext.verify_mode

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

>>> ssl.create_default_context().verify_mode
ssl.CERT_REQUIRED
<VerifyMode.CERT_REQUIRED: 2>

.. index:: single: certificates

Expand Down
603 changes: 371 additions & 232 deletions Lib/enum.py

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,30 +2567,28 @@ class _empty:


class _ParameterKind(enum.IntEnum):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that inheritting correct where the values are changed to str?

Copy link
Member Author

@ethanfurman ethanfurman Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gryznar Good question: only because the __new__ uses integers as the values, and the "value" on the assignment line is saved in the description attribute. Without the custom __new__ the enum creation would have failed, since strings are not integers.

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
_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.
Expand Down
3 changes: 2 additions & 1 deletion Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
from xml.parsers.expat import ParserCreate


PlistFormat = enum.global_enum(enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__))
PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
globals().update(PlistFormat.__members__)


class UID:
Expand Down
2 changes: 2 additions & 0 deletions Lib/re.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class RegexFlag:
# sre extensions (experimental, don't rely on these)
TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
__str__ = object.__str__
_numeric_repr_ = hex

# sre exception
error = sre_compile.error
Expand Down
1 change: 0 additions & 1 deletion Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
)
from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION


_IntEnum._convert_(
'_SSLMethod', __name__,
lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
Expand Down
Loading