summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
Commit message (Collapse)AuthorAgeFilesLines
* bpo-46477: [Enum] ensure Flag subclasses have correct bitwise methods (GH-30816)Ethan Furman2022-01-231-39/+41
|
* bpo-45535: [Enum] include special dunders in dir() (GH-30677)Ethan Furman2022-01-181-20/+13
| | | | | | | | | Include the `__dunders__` in `dir()` that make `Enum` special: - `__contains__` - `__getitem__` - `__iter__` - `__len__` - `__members__`
* bpo-40066: [Enum] skip failing doc test (GH-30637)Kumar Aditya2022-01-171-232/+371
|
* Revert "bpo-40066: [Enum] update str() and format() output (GH-30582)" ↵Victor Stinner2022-01-171-371/+232
| | | | | (GH-30632) This reverts commit acf7403f9baea3ae1119fc6b4a3298522188bf96.
* bpo-40066: [Enum] update str() and format() output (GH-30582)Ethan Furman2022-01-161-232/+371
| | | | | | | | | | | | | | | 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`
* bpo-46242: [Enum] better error message for extending `Enum` with members ↵Nikita Sobolev2022-01-141-5/+4
| | | | (GH-30357)
* bpo-46269: [Enum] remove special-casing of `__new__` in `EnumType.__dir__` ↵Nikita Sobolev2022-01-051-4/+0
| | | | (GH-30421)
* bpo-45535: Improve output of Enum ``dir()`` (GH-29316)Alex Waygood2021-12-021-11/+58
| | | | | | Modify the ``EnumType.__dir__()`` and ``Enum.__dir__()`` to ensure that user-defined methods and methods inherited from mixin classes always show up in the output of `help()`. This change also makes it easier for IDEs to provide auto-completion.
* bpo-45417: [Enum] fix quadratic behavior during creation (GH-28907)Carl Friedrich Bolz-Tereick2021-10-141-7/+14
| | | | | | | | | | | | Creating an Enum exhibited quadratic behavior based on the number of members in three places: - `EnumDict._member_names`: a list searched with each new member's name - member creation: a `for` loop checking each existing member to see if new member was a duplicate - `auto()` values: a list of all previous values in enum was copied before being sent to `_generate_next_value()` Two of those issues have been resolved: - `_EnumDict._member_names` is now a dictionary so lookups are fast - member creation tries a fast value lookup before falling back to the slower `for` loop lookup The third issue still remains, as `_generate_next_value_()` can be user-overridden and could corrupt the last values list if it were not copied.
* bpo-44929: [Enum] Fix global repr (GH-27789)Pablo Galindo Salgado2021-08-251-4/+15
| | | | | | | | | | | | * Fix typo in __repr__ code * Add more tests for global int flag reprs * use last module if multi-module string - when an enum's `__module__` contains several module names, only use the last one Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)Ethan Furman2021-06-181-3/+31
| | | | | | | | | | | | | | | | | | | | | * [Enum] reduce scope of new format behavior Instead of treating all Enums the same for format(), only user mixed-in enums will be affected. In other words, IntEnum and IntFlag will not be changing the format() behavior, due to the requirement that they be drop-in replacements of existing integer constants. If a user creates their own integer-based enum, then the new behavior will apply: class Grades(int, Enum): A = 5 B = 4 C = 3 D = 2 F = 0 Now: format(Grades.B) -> DeprecationWarning and '4' 3.12: -> no warning, and 'B'
* bpo-44342: [Enum] sync current docs to 3.10 (GH-26750)Ethan Furman2021-06-161-1/+1
|
* Fix a typo in _make_class_unpicklable() docstring (GH-26729)andrei kulakov2021-06-151-1/+1
|
* bpo-44242: [Enum] improve error messages (GH-26669)Ethan Furman2021-06-111-12/+25
|
* bpo-44342: [Enum] fix data type search (GH-26667)Ethan Furman2021-06-111-1/+1
| | | | | | | In an inheritance chain of int -> my_int -> final_int the data type is now final_int (not my_int)
* bpo-44342: [Enum] changed pickling from by-value to by-name (GH-26658)Ethan Furman2021-06-101-21/+4
| | | | | | | by-value lookups could fail on complex enums, necessitating a check for __reduce__ and possibly sabotaging the final enum; by-name lookups should never fail, and sabotaging is no longer necessary for class-based enum creation.
* bpo-44356: [Enum] allow multiple data-type mixins if they are all the same ↵Ethan Furman2021-06-101-4/+4
| | | | | | | | | | | | | | (GH-26649) This enables, for example, two base Enums to both inherit from `str`, and then both be mixed into the same final Enum: class Str1Enum(str, Enum): # some behavior here class Str2Enum(str, Enum): # some more behavior here class FinalStrEnum(Str1Enum, Str2Enum): # this now works
* bpo-44242: [Enum] remove missing bits test from Flag creation (GH-26586)Ethan Furman2021-06-091-11/+96
| | | | Move the check for missing named flags in flag aliases from Flag creation to a new *verify* decorator.
* bpo-43957: Add a missins space to the new format enum warning (#25770)Pablo Galindo2021-05-011-1/+1
|
* bpo-43957: [Enum] Deprecate ``TypeError`` from containment checks. (GH-25670)Ethan Furman2021-04-271-9/+20
| | | | | 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.
* bpo-43945: [Enum] Deprecate non-standard mixin format() behavior (GH-25649)Ethan Furman2021-04-271-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | In 3.12 the enum member, not the member's value, will be used for format() calls. Format specifiers can be used to retain the current display of enum members: Example enumeration: class Color(IntEnum): RED = 1 GREEN = 2 BLUE = 3 Current behavior: f'{Color.RED}' --> '1' Future behavior: f'{Color.RED}' --> 'RED' Using d specifier: f'{Color.RED:d}' --> '1' Using specifiers can be done now and is future-compatible.
* bpo-38659: [Enum] do not check '_inverted_' during simple test (GH-25566)Ethan Furman2021-04-241-2/+2
| | | Depending on usage, it's possible for Flag members to have the _inverted_ attribute when they are testing, while the Flag being testing against will not have that attribute on its members -- so skip that comparison.
* bpo-38659: [Enum] add _simple_enum decorator (GH-25497)Ethan Furman2021-04-211-7/+318
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* Revert "bpo-38659: [Enum] add _simple_enum decorator (GH-25285)" (GH-25476)Ethan Furman2021-04-201-319/+7
| | | This reverts commit dbac8f40e81eb0a29dc833e6409a1abf47467da6.
* bpo-38659: [Enum] add _simple_enum decorator (GH-25285)Ethan Furman2021-04-201-7/+319
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* bpo-43744: [Enum] fix ``_is_private`` (GH-25349)Ethan Furman2021-04-151-2/+3
| | | | ``_is_private`` now returns ``False`` instead of raising an exception when enum name matches beginning of class name as used in a private variable
* [Enum] fix doc string (GH-25376)Ethan Furman2021-04-151-2/+4
|
* bpo-42248: [Enum] ensure exceptions raised in ``_missing_`` are released ↵Ethan Furman2021-04-121-19/+24
| | | | (GH-25350)
* bpo-40066: Enum: modify `repr()` and `str()` (GH-22392)Ethan Furman2021-03-311-20/+66
| | | | | | | | | * 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
* bpo-43162: [Enum] update docs, renable doc tests (GH-24487)Ethan Furman2021-03-031-1/+1
| | | | * update docs, renable doc tests * make deprecation warning active for two releases
* bpo-43162: [Enum] deprecate enum member.member access (GH-24486)Ethan Furman2021-02-091-4/+14
| | | | | | | In 3.5 (?) a speed optimization made it possible to access members as attributes of other members, i.e. ``Color.RED.BLUE``. This was always discouraged in the docs, and other recent optimizations has made that one no longer necessary. Because some may be relying on it anyway, it is being deprecated in 3.10, and will be removed in 3.11.
* bpo-38250: [Enum] single-bit flags are canonical (GH-24215)Ethan Furman2021-01-251-165/+337
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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().
* bpo-42901: [Enum] move member creation to `__set_name__` (GH-24196)Ethan Furman2021-01-131-107/+190
| | | | | | | | | | | `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.
* bpo-42851: [Enum] remove brittle __init_subclass__ support (GH-24154)Ethan Furman2021-01-071-30/+1
| | | Solution to support calls to `__init_subclass__` with members defined is too brittle and breaks with certain mixins.
* bpo-42727: [Enum] use super() and include **kwds (GH-23927)Ethan Furman2020-12-251-2/+2
| | | | | | | | for multiple inheritance support: use super().new pass **kwds to super().new
* [Enum] EnumMeta.__prepare__ now accepts **kwds (#23917)Ethan Furman2020-12-241-1/+1
|
* bpo-34750: [Enum] add `_EnumDict.update()` support (GH-23725)Ethan Furman2020-12-101-1/+11
| | | | | | | | | | | | | | | | 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()
* bpo-42385: [Enum] add `_generate_next_value_` to StrEnum (GH-23735)Ethan Furman2020-12-101-0/+6
| | | 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.
* bpo-42517: [Enum] do not convert private names into members (GH-23722)Ethan Furman2020-12-101-1/+18
| | | private names, such as `_Color__hue` and `_Color__hue_` are now normal attributes, and do not become members nor raise exceptions
* bpo-42567: [Enum] call __init_subclass__ after members are added (GH-23714)Ethan Furman2020-12-101-2/+30
| | | | | 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.
* [Enum] reformat and add doc strings (GH-23705)Ethan Furman2020-12-081-53/+134
|
* bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497)Ethan Furman2020-12-081-1/+1
|
* bpo-41889: [Enum] fix multiple-inheritance regression (GH-22487)Ethan Furman2020-12-071-3/+8
|
* bpo-41816: `StrEnum.__str__` is `str.__str__` (GH-22362)Ethan Furman2020-09-221-0/+2
| | | use `str.__str__` for `StrEnum` so that `str(StrEnum.member)` is the same as directly accessing the string value of the `StrEnum` member
* bpo-41816: add `StrEnum` (GH-22337)Ethan Furman2020-09-221-2/+30
| | | | `StrEnum` ensures that its members were already strings, or intended to be strings.
* bpo-40084: Enum - dir() includes member attributes (GH-19219)Angelin BOOZ2020-09-211-1/+1
|
* Enum: make `Flag` and `IntFlag` members iterable (GH-22221)Ethan Furman2020-09-161-0/+4
|
* _auto_called cleanup (GH-22285)Ethan Furman2020-09-161-1/+1
|
* bpo-39728: Enum: fix duplicate `ValueError` (GH-22277)Ethan Furman2020-09-161-1/+1
| | | | fix default `_missing_` to return `None` instead of raising a `ValueError` Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com>
* bpo-41517: do not allow Enums to be extended (#22271)Ethan Furman2020-09-161-5/+14
| | | fix bug that let Enums be extended via multiple inheritance