Commit Graph

149 Commits

Author SHA1 Message Date
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
Ethan Furman c314e60388
bpo-42901: [Enum] move member creation to `__set_name__` (GH-24196)
`type.__new__` calls `__set_name__` and `__init_subclass__`, which means
that any work metaclasses do after calling `super().__new__()` will not
be available to those two methods.  In particular, `Enum` classes that
want to make use of `__init_subclass__` will not see any members.

Almost all customization is therefore moved to before the
`type.__new__()` call, including changing all members to a proto member
descriptor with a `__set_name__` that will do the final conversion of a
member to be an instance of the `Enum` class.
2021-01-12 23:47:57 -08:00
Ethan Furman a581a868d9
bpo-42851: [Enum] remove brittle __init_subclass__ support (GH-24154)
Solution to support calls to `__init_subclass__` with members defined is too brittle and breaks with certain mixins.
2021-01-07 13:17:55 -08:00
Ethan Furman 786d97a66c
bpo-42727: [Enum] use super() and include **kwds (GH-23927)
for multiple inheritance support:

use super().new
pass **kwds to super().new
2020-12-24 19:31:10 -08:00
Ethan Furman 6ec0adefad
[Enum] EnumMeta.__prepare__ now accepts **kwds (#23917) 2020-12-24 10:05:02 -08:00
Ethan Furman a658287179
bpo-34750: [Enum] add `_EnumDict.update()` support (GH-23725)
This allows easier Enum construction in unusual cases, such as including dynamic member definitions into a class definition:

# created dynamically
foo_defines = {'FOO_CAT': 'aloof', 'BAR_DOG': 'friendly', 'FOO_HORSE': 'big'}

class Foo(Enum):
    vars().update({
            k: v
            for k, v in foo_defines.items()
            if k.startswith('FOO_')
            })
    def upper(self):
        # example method
        return self.value.upper()
2020-12-10 13:07:00 -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 6bd94de168
bpo-42567: [Enum] call __init_subclass__ after members are added (GH-23714)
When creating an Enum, type.__new__ calls __init_subclass__, but at that point the members have not been added.

This patch suppresses the initial call, then manually calls the ancestor __init_subclass__ before returning the new Enum class.
2020-12-09 16:41:22 -08:00
Ethan Furman 6d3dfee271
[Enum] reformat and add doc strings (GH-23705) 2020-12-08 12:26:56 -08:00
Ethan Furman 37440eef7f
bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) 2020-12-08 11:14:10 -08:00
Ethan Furman c266736ec1
bpo-41889: [Enum] fix multiple-inheritance regression (GH-22487) 2020-12-07 00:17:31 -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 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
Angelin BOOZ 68526fe258
bpo-40084: Enum - dir() includes member attributes (GH-19219) 2020-09-21 06:11:06 -07:00
Ethan Furman 7219e27087
Enum: make `Flag` and `IntFlag` members iterable (GH-22221) 2020-09-16 13:01:00 -07:00
Ethan Furman fc23a9483e
_auto_called cleanup (GH-22285) 2020-09-16 12:37:54 -07:00
Ethan Furman c95ad7a91f
bpo-39728: Enum: fix duplicate `ValueError` (GH-22277)
fix default `_missing_` to return `None` instead of raising a `ValueError`
Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com>
2020-09-16 10:26:50 -07:00
Ethan Furman 3064dbf5df
bpo-41517: do not allow Enums to be extended (#22271)
fix bug that let Enums be extended via multiple inheritance
2020-09-16 07:11:57 -07:00
Ethan Furman 22415ad625
bpo-41789: honor object overrides in Enum classes (GH-22250)
EnumMeta double-checks that `__repr__`, `__str__`, `__format__`, and `__reduce_ex__` are not the same as `object`'s, and replaces them if they are -- even if that replacement was intentionally done in the Enum being constructed.  This patch fixes that.

Automerge-Triggered-By: @ethanfurman
2020-09-15 16:28:25 -07:00
Ethan Furman bff01f3a3a
bpo-39587: Enum - use correct mixed-in data type (GH-22263) 2020-09-15 15:56:26 -07:00
Zackery Spytz 2ec67526a6
bpo-38967: Improve the error msg for reserved _sunder_ names in enum (GH-18370) 2020-09-13 13:27:51 -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
HongWeipeng 0b41a922f9 bpo-38045: Improve the performance of _decompose() in enum.py (GH-16483)
* Improve the performance of _decompose() in enum.py

Co-Authored-By: Brandt Bucher <brandtbucher@gmail.com>
2019-11-26 14:36:02 -08:00
Pablo Galindo 293dd23477
Remove binding of captured exceptions when not used to reduce the chances of creating cycles (GH-17246)
Capturing exceptions into names can lead to reference cycles though the __traceback__ attribute of the exceptions in some obscure cases that have been reported previously and fixed individually. As these variables are not used anyway, we can remove the binding to reduce the chances of creating reference cycles.

See for example GH-13135
2019-11-19 21:34:03 +00:00
HongWeipeng bb16fb2cb8 Doc: Fix spelling errors of 'initial' in enum.py (GH-16314) 2019-09-21 07:22:54 +02:00
Walter Dörwald 323842c279 bpo-34443: Use __qualname__ instead of __name__ in enum exception messages. (GH-14809)
* Use __qualname__ instead of __name__ in enum exception messages.
2019-07-18 11:37:13 -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
Zachary Ware 19a1e1eb86 bpo-34282: Remove deprecated enum _convert method (GH-13823) 2019-06-04 23:03:10 +02:00
Brennan D Baraban 8b914d2767 bpo-35899: Fix Enum handling of empty and weird strings (GH-11891)
Co-authored-by: Maxwell <maxwellpxt@gmail.com>
Co-authored-by: Stéphane Wirtel <stephane@wirtel.be>





https://bugs.python.org/issue35899
2019-03-03 14:09:11 -08:00
Rémi Lapeyre 1fd06f1eca bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
https://bugs.python.org/issue17467
2019-01-24 11:43:13 -08:00
Andrew Svetlov 34ae04f74d Speed-up building enums by value, e.g. http.HTTPStatus(200) (#11318)
bpo-35585: Speed up enum by-value lookup
2018-12-26 10:45:33 -08:00
Ethan Furman cd45385ffa
bpo-34909: keep searching mixins until base class is found (GH-9737) 2018-10-05 23:29:36 -07: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 019f0a0cb8
bpo-34536: raise error for invalid _missing_ results (GH-9147)
* raise exception if _missing_ returns None or invalid type
2018-09-12 11:43:34 -07:00
orlnub123 0fb9fadd3b bpo-34282: Fix Enum._convert shadowing members named _convert (GH-8568)
* Fix enum members getting shadowed by parent attributes
* Move Enum._convert to EnumMeta._convert_
* Deprecate _convert
2018-09-12 10:28:53 -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
INADA Naoki e57f91a0f0
bpo-33866: enum: Stop using OrderedDict (GH-7698) 2018-06-19 01:14:26 +09:00
anentropic b8e21f1289 bpo-31947: remove None default for names param in Enum._create_ (GH-4288) 2018-04-15 20:40:35 -07: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
Serhiy Storchaka 81108375d9 bpo-30152: Reduce the number of imports for argparse. (#1269) 2017-09-26 00:55:55 +03:00
Ville Skyttä 49b2734bf1 Spelling fixes (#2902) 2017-08-03 09:00:59 +03:00
Dong-hee Na dcc8ce44c7 bpo-30616: Functional API of enum allows to create empty enums. (#2304)
* bpo-30616: Functional API of enum allows to create empty enums.

* Update NEWS

move addition to avoid conflict
2017-06-21 09:52:32 -07:00
Ethan Furman 28cf663ff0 closes issue29167: fix race condition in (Int)Flag 2017-01-24 12:12:06 -08:00
Raymond Hettinger 381dc6c22f Issue #29103: Remove unused import. Noticed by Jean-Sebastien Bevilacqua. 2016-12-29 23:57:12 -07: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 06339e7493 Enum._convert: sort by value, then by name 2016-09-11 13:25:26 -07:00
Ethan Furman c16595e567 issue23591: add auto() for auto-generating Enum member values 2016-09-10 23:36:59 -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 0443953067 issue23591: optimize _high_bit() 2016-09-02 15:50:21 -07:00
Ethan Furman 65a5a47d79 issue23591: add docs; code cleanup; more tests 2016-09-01 23:55:19 -07:00
Ethan Furman ee47e5cf8a issue23591: add Flags, IntFlags, and tests 2016-08-31 00:12:15 -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
Ethan Furman 73fc586d9f Add AutoEnum: automatically provides next value if missing. Issue 26988. 2016-08-05 16:03:16 -07:00
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) 6f20bd6063 signal, socket, and ssl module IntEnum constant name lookups now return a
consistent name for values having multiple names.  Ex: signal.Signals(6)
now refers to itself as signal.SIGALRM rather than flipping between that
and signal.SIGIOT based on the interpreter's hash randomization seed.

This helps finish issue27167.
2016-06-03 19:14:52 +00:00
Ethan Furman 354706915c issue26893: use mro() to examine class heirarchy 2016-05-01 10:04:21 -07:00
Ethan Furman 3803ad47bb issue26893: use mro() to examine class heirarchy 2016-05-01 10:03:53 -07:00
Ethan Furman 0fe7978c68 Issue26748: Enum classes should evaluate as True 2016-04-13 23:55:40 -07:00
Ethan Furman de4e079d3d Issue26748: Enum classes should evaluate as True 2016-04-13 23:53:45 -07:00
Ethan Furman 5de67b106a Issue26748: Enum classes should evaluate as True 2016-04-13 23:52:09 -07:00
Ethan Furman 60255b67b9 revert change 87a9dff5106c: pure Enum members again evaluate to True;
update Finer Points section of docs to cover boolean evaluation;
add more tests for pure and mixed boolean evaluation
2016-01-15 15:01:33 -08:00
Martin Panter 19e69c5a20 Issue #23883: Add missing APIs to __all__; patch by Jacek Kołodziej 2015-11-14 12:46:42 +00:00
Ethan Furman c791507e1f Issue 25147: add reason for using _collections 2015-09-17 22:55:40 -07:00
Ethan Furman e5754ab0c4 Close issue25147: use C implementation of OrderedDict 2015-09-17 22:03:52 -07:00
Ethan Furman 6db1fd5fb8 Close issue24840: Enum._value_ is queried for bool(); original patch by Mike Lundy 2015-09-17 21:49:12 -07:00
Ethan Furman 48a724fa33 Close issue23900: add default __doc__ to new enumerations that do not specify one. 2015-04-11 23:23:06 -07:00
Ethan Furman 24e837f231 issue23673
add private method to enum to support replacing global constants with Enum members:
- search for candidate constants via supplied filter
- create new enum class and members
- insert enum class and replace constants with members via supplied module name
- replace __reduce_ex__ with function that returns member name, so previous Python versions can unpickle
modify IntEnum classes to use new method
2015-03-18 17:27:57 -07:00
Ethan Furman 354ecf1ef2 Close issue23486: performance boost for enum member lookup 2015-03-11 08:43:12 -07:00
Ethan Furman ec5f8eb85f Issue22506: remove name & value from __dir__ as they now show up automatically 2014-10-21 13:40:35 -07:00
Ethan Furman 987f3dd161 Issue22506: merge from 3.4 2014-10-14 08:59:14 -07:00
Ethan Furman 0ae550bdde Issue22506: added methods on base Enum class now show up in dir of Enum subclass (3.4) 2014-10-14 08:58:32 -07:00
Ethan Furman d9925a18ec Close issue21706: add 'start' parameter to functional API 2014-09-16 20:35:55 -07:00
Ethan Furman 0081f23a08 consistently use _name_ and _value_; patch from Kiss Gyorgy 2014-09-16 17:31:23 -07:00
Ethan Furman 482fe0477e issue23673
add private method to enum to support replacing global constants with Enum members:
- search for candidate constants via supplied filter
- create new enum class and members
- insert enum class and replace constants with members via supplied module name
- replace __reduce_ex__ with function that returns member name, so previous Python versions can unpickle
modify IntEnum classes to use new method
2015-03-18 18:19:30 -07:00
Ethan Furman 2da950460d Close issue20653: improve functional API docs; minor code changes 2014-03-03 12:42:52 -08:00
Ethan Furman dc87052c0c Close issue20653: allow Enum subclasses to override __reduce_ex__ 2014-02-18 12:37:12 -08:00
Ethan Furman ca1b794dac Close issue20534: all pickle protocols now supported. 2014-02-08 11:36:27 -08:00
Ethan Furman be3c2fea35 removed Enum.__eq__ as it added nothing 2013-11-13 14:25:45 -08:00
Ethan Furman 648f860c22 Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM. 2013-10-06 17:19:54 -07:00
Ethan Furman 33918c128b Issue19030: fixed comment that was still referring to a changed descriptor. 2013-09-27 23:02:02 -07:00
Ethan Furman e03ea37a7b Close #19030: improvements to inspect and Enum.
inspect.getmembers and inspect.classify_class_attrs now search the metaclass
mro for types.DynamicClassAttributes (what use to be called
enum._RouteClassAttributeToGetattr); in part this means that these two
functions no longer rely solely on dir().

Besides now returning more accurate information, these improvements also
allow a more helpful help() on Enum classes.
2013-09-25 07:14:41 -07:00
Ethan Furman 64a9972b40 Close #19025: Better error message when trying to delete an Enum member.
Also slight code reorg for PEP 8 guidelines.
2013-09-22 16:18:19 -07:00
Ethan Furman c850f34220 Close #18693: Enum is now more help() friendly. 2013-09-15 16:59:35 -07:00
Ethan Furman 101e0746d3 Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them. 2013-09-15 12:34:36 -07:00
Ethan Furman 2131a4a2fc Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order). 2013-09-14 18:11:24 -07:00
Ethan Furman f203f2d51d Close #18924: Block naive attempts to change an Enum member. 2013-09-06 07:16:48 -07:00
Ethan Furman ec15a826ce Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum). 2013-08-31 19:17:41 -07:00
Ezio Melotti 9a3777e525 #18705: merge with 3.3. 2013-08-17 15:53:55 +03:00
Ethan Furman 388a3921cf Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`. 2013-08-12 06:51:41 -07:00
Ethan Furman 53f58dbd12 Close #18693: __dir__ removed from Enum; help() now helpful. 2013-08-10 13:07:49 -07:00
Ethan Furman 5e5a8230c2 Close #18635: Move class level private attribute from instance to class. 2013-08-04 08:42:23 -07:00
Ethan Furman b41803e3ef Close #18545: now only executes member_type if no _value_ is assigned in __new__. 2013-07-25 13:50:45 -07:00
Ethan Furman 520ad57916 Change _names to _names_ since the latter is reserved for Enum use.
Before this change only the methods were _single_underscored_; now
the attributes are as well.
2013-07-19 19:47:21 -07:00
Ethan Furman 2aa2732eaf Close #18508 -- fix _value2member_map to always have the member's value 2013-07-19 19:35:56 -07:00
Ethan Furman f24bb35a69 closes issue18042 -- a `unique` decorator is added to enum.py
The docs also clarify the 'Interesting Example' duplicate-free enum is for
demonstration purposes.
2013-07-18 17:05:39 -07:00
Ethan Furman 6b3d64ab5d Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Missing files added.
News entry added.
2013-06-14 16:55:46 -07:00