Commit Graph

93 Commits

Author SHA1 Message Date
Shantanu 65d180d983
bpo-38250: add version added for FlagBoundary (GH-25820)
* bpo-38250: add version added for FlagBoundary

* Also add versionadded for utilities

Co-authored-by: hauntsaninja <>
2021-05-13 22:59:53 -07:00
Jelle Zijlstra 6fee0835cb
fix enum.property reference in docs (GH-25875) 2021-05-03 20:19:46 -07:00
Ethan Furman 6bd9288b80
bpo-43957: [Enum] Deprecate ``TypeError`` from containment checks. (GH-25670)
In 3.12 ``True`` or ``False`` will be returned for all containment checks,
with ``True`` being returned if the value is either a member of that enum
or one of its members' value.
2021-04-27 13:05:08 -07:00
Ethan Furman a02cb474f9
bpo-38659: [Enum] add _simple_enum decorator (GH-25497)
add:

* `_simple_enum` decorator to transform a normal class into an enum
* `_test_simple_enum` function to compare
* `_old_convert_` to enable checking `_convert_` generated enums

`_simple_enum` takes a normal class and converts it into an enum:

    @simple_enum(Enum)
    class Color:
        RED = 1
        GREEN = 2
        BLUE = 3

`_old_convert_` works much like` _convert_` does, using the original logic:

    # in a test file
    import socket, enum
    CheckedAddressFamily = enum._old_convert_(
            enum.IntEnum, 'AddressFamily', 'socket',
            lambda C: C.isupper() and C.startswith('AF_'),
            source=_socket,
            )

`_test_simple_enum` takes a traditional enum and a simple enum and
compares the two:

    # in the REPL or the same module as Color
    class CheckedColor(Enum):
        RED = 1
        GREEN = 2
        BLUE = 3

    _test_simple_enum(CheckedColor, Color)

    _test_simple_enum(CheckedAddressFamily, socket.AddressFamily)

Any important differences will raise a TypeError
2021-04-21 10:20:44 -07:00
Ethan Furman 503cdc7c12
Revert "bpo-38659: [Enum] add _simple_enum decorator (GH-25285)" (GH-25476)
This reverts commit dbac8f40e8.
2021-04-19 19:12:24 -07:00
Ethan Furman dbac8f40e8
bpo-38659: [Enum] add _simple_enum decorator (GH-25285)
add:

_simple_enum decorator to transform a normal class into an enum
_test_simple_enum function to compare
_old_convert_ to enable checking _convert_ generated enums
_simple_enum takes a normal class and converts it into an enum:

@simple_enum(Enum)
class Color:
    RED = 1
    GREEN = 2
    BLUE = 3

_old_convert_ works much like _convert_ does, using the original logic:

# in a test file
import socket, enum
CheckedAddressFamily = enum._old_convert_(
        enum.IntEnum, 'AddressFamily', 'socket',
        lambda C: C.isupper() and C.startswith('AF_'),
        source=_socket,
        )

test_simple_enum takes a traditional enum and a simple enum and
compares the two:

# in the REPL or the same module as Color
class CheckedColor(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

_test_simple_enum(CheckedColor, Color)

_test_simple_enum(CheckedAddressFamily, socket.AddressFamily)

Any important differences will raise a TypeError
2021-04-19 18:04:53 -07:00
Ethan Furman 49aec1a185
Enum: add (re)import of Flag for doctests (GH-25118)
Fix issue with CI doctest forgetting that ``Flag`` had already been imported.
2021-03-31 09:20:08 -07:00
Ethan Furman b775106d94
bpo-40066: Enum: modify `repr()` and `str()` (GH-22392)
* Enum: streamline repr() and str(); improve docs

- repr() is now ``enum_class.member_name``
- stdlib global enums are ``module_name.member_name``
- str() is now ``member_name``
- add HOW-TO section for ``Enum``
- change main documentation to be an API reference
2021-03-30 21:17:26 -07:00
Ethan Furman 44e580f448
bpo-43162: [Enum] update docs, renable doc tests (GH-24487)
* update docs, renable doc tests
* make deprecation warning active for two releases
2021-03-03 09:54:30 -08:00
Mariatta Wijaya f193874056
Fix grammar in enum documentation. (GH-24689)
There is an extra `s` in the singular word `method`.
Reported in docs mailing list by Steven Nguyen.

Automerge-Triggered-By: GH:Mariatta
2021-03-01 10:56:03 -08:00
Pablo Galindo c92cd0f3c8
Fix minor typo in the rest format in the enum docs (GH-24335) 2021-01-25 23:23:30 +00:00
Ethan Furman 7aaeb2a3d6
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.

Iterating, repr(), and str() show members in definition order.

When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:

>>> class Test(Flag, boundary=CONFORM):
...     ONE = 1
...     TWO = 2
...
>>> Test(5)
<Test.ONE: 1>

Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.

Iteration is now in member definition order.  If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.


``re`` module:

repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.

   re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)

in both of the above terms, the ``value`` is 282.

re's tests have been updated to reflect the modifications to repr().
2021-01-25 14:26:19 -08:00
Andre Delfino 96a09df644
[doc] Fix a few margins due to bad markup (GH-23619) 2020-12-17 09:25:55 -08:00
Ethan Furman efb13be72c
bpo-42385: [Enum] add `_generate_next_value_` to StrEnum (GH-23735)
The default for auto() is to return an integer, which doesn't work for `StrEnum`.  The new `_generate_next_value_` for `StrEnum` returns the member name, lower cased.
2020-12-10 12:20:06 -08:00
Ethan Furman 7cf0aad96d
bpo-42517: [Enum] do not convert private names into members (GH-23722)
private names, such as `_Color__hue` and `_Color__hue_` are now normal attributes, and do not become members nor raise exceptions
2020-12-09 17:12:11 -08:00
Ethan Furman d986d1657e
bpo-41816: `StrEnum.__str__` is `str.__str__` (GH-22362)
use `str.__str__` for `StrEnum` so that `str(StrEnum.member)` is the same as directly accessing the string value of the `StrEnum` member
2020-09-22 13:00:07 -07:00
Ethan Furman 62e40d8450
Enum: add extended AutoNumber example (GH-22349) 2020-09-22 00:05:27 -07:00
Ethan Furman 0063ff4e58
bpo-41816: add `StrEnum` (GH-22337)
`StrEnum` ensures that its members were already strings, or intended to
be strings.
2020-09-21 17:23:13 -07:00
Ethan Furman 7219e27087
Enum: make `Flag` and `IntFlag` members iterable (GH-22221) 2020-09-16 13:01:00 -07:00
Ethan Furman 542e1df2b0
bpo-40721: add note about enum member name case (GH-22231)
* UPPER_CASE preferred as enum members are constants
2020-09-14 13:32:44 -07:00
Victor Stinner d3ded08048
bpo-40204: Add :noindex: in the documentation (GH-21859)
Add :noindex: to duplicated documentation to fix "duplicate object
description" errors.

For example, fix this Sphinx 3 issue:

Doc/library/configparser.rst:1146: WARNING: duplicate object
description of configparser.ConfigParser.optionxform, other instance
in library/configparser, use :noindex: for one of them
2020-08-13 21:41:54 +02:00
E-Paine 1ee5dc1586
Remove trailing >>> in enum docs (GH-21358)
The >>> as the last line serve no purpose and are not colored correctly by Sphinx.
2020-07-09 12:18:34 -07:00
Ethan Onstott d9a43e20fa
bpo-40025: Require _generate_next_value_ to be defined before members (GH-19098)
require `_generate_next_value_` to be defined before members
2020-04-28 10:20:55 -07:00
YoSTEALTH 24bcefcb74 bpo-39234: `enum.auto()` default initial value as 1 (GH-17878)
Updated as Eric mentioned "By default, the initial value starts at 1"


https://bugs.python.org/issue39234



Automerge-Triggered-By: @ericvsmith
2020-01-06 14:04:43 -08:00
YoSTEALTH 2e9012a3e1 bpo-39234: Doc: `enum.auto()` incrementation value not specified. (GH-17872)
* `enum.auto()` initial value is now specified as being `1`.
2020-01-06 11:53:36 -08:00
Antoine d3c8d73514 Minor documentation fixes on library/enum (GH-15234)
* Minor documentation fixes on library/enum
2019-08-19 18:41:31 -07:00
thatneat 2f19e82fbe bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__ (GH-14545)
* bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__
2019-07-04 11:28:37 -07:00
nu_no dfc8bb987d Fix typo: class declaration (GH-11678) 2019-01-27 14:07:47 -08:00
Ethan Furman 5bdab641da
bpo-29577: Enum: mixin classes don't mix well with already mixed Enums (GH-9328)
* bpo-29577: allow multiple mixin classes
2018-09-21 19:03:09 -07:00
Ethan Furman f52237400b bpo-33437: add __new__ vs __init__ example (GH-9145)
Improve Enum docs.


https://bugs.python.org/issue33437
2018-09-12 10:00:30 -07:00
Rahul Jha 9430652535 bpo-33217: Raise TypeError for non-Enum lookups in Enums (GH-6651)
* bpo-33217: Raise TypeError for non-Enum lookups in Enums
2018-09-10 11:21:04 -07:00
Andrés Delfino 2d748389dc Fix moduleauthor/sectionauthor directives in Enum (GH-8117) 2018-07-07 22:01:25 +03:00
INADA Naoki e57f91a0f0
bpo-33866: enum: Stop using OrderedDict (GH-7698) 2018-06-19 01:14:26 +09:00
Ethan Furman a4b1bb4801
bpo-31801: Enum: add _ignore_ as class option (#5237)
* bpo-31801:  Enum:  add _ignore_ as class option

_ignore_ is a list, or white-space seperated str, of names that will not
be candidates for members; these names, and _ignore_ itself, are removed
from the final class.

* bpo-31801:  Enum:  add documentation for _ignore_

* bpo-31801: Enum: remove trailing whitespace

* bpo-31801: Enum: fix bulleted list format

* bpo-31801: add version added for _ignore_
2018-01-22 07:56:37 -08:00
Julian Kahnert 0f31c74fcf Improve enum.Flag code example (GH-5167)
The code example that demonstrate how to use enum.Flag was missing
the import of enum.auto.
2018-01-12 19:35:57 -08:00
Kartik Anand 626584284e correct documentation for enum.html (#358) 2017-02-28 21:07:18 +01:00
Berker Peksag 2a267a12ee Issue #29129: Fix typo in "Using auto" section 2017-01-02 05:51:04 +03:00
Ethan Furman 23bb6f48ea close issue28172: Change all example enum member names to uppercase, per Guido; patch by Chris Angelico. 2016-11-21 09:22:05 -08:00
Ethan Furman 3515dcce80 issue23591: fix flag decomposition and repr 2016-09-18 13:15:41 -07:00
Ethan Furman c16595e567 issue23591: add auto() for auto-generating Enum member values 2016-09-10 23:36:59 -07:00
Ethan Furman 54924df2b1 improve Enum docs 2016-09-07 23:40:31 -07:00
Ethan Furman 6a137e8d50 add recipes for pseudo-valueless enums 2016-09-07 08:17:15 -07:00
Ethan Furman 27682d2698 issue23591: more docs; slight change to repr 2016-09-04 11:39:01 -07:00
Ethan Furman 25d94bbf05 issue23591: bool(empty_flags) == False; more docs & tests 2016-09-02 16:32:32 -07:00
Ethan Furman 65a5a47d79 issue23591: add docs; code cleanup; more tests 2016-09-01 23:55:19 -07:00
Ethan Furman e8e61277ff issue26981: add _order_ compatibility shim to enum.Enum 2016-08-20 07:19:31 -07:00
Ethan Furman 332dbc7325 Issue26988: remove AutoEnum 2016-08-20 00:00:52 -07:00
Berker Peksag 43b586b951 Silence warnings from 'make suspicious' to make the docs buildbot happy 2016-08-06 13:37:22 +03:00
Ethan Furman 73fc586d9f Add AutoEnum: automatically provides next value if missing. Issue 26988. 2016-08-05 16:03:16 -07:00
Martin Panter 2fec611a70 Issue #24314: Merge doc links from 3.5 2016-06-18 08:20:22 +00:00