summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
Commit message (Collapse)AuthorAgeFilesLines
* [3.11] gh-111181: Fix enum doctests (GH-111180) (GH-111617)Ethan Furman2023-11-031-6/+5
| | | | | | | | gh-111181: Fix enum doctests (GH-111180) Co-authored-by: Ethan Furman <ethan@stoneleaf.us> (cherry picked from commit c4dc5a6ae8aa13abb743182df088f1a3526d1bcd) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
* [3.11] gh-108682: [Enum] raise TypeError if super().__new__ called in custom ↵Ethan Furman2023-09-081-0/+7
| | | | | | | | | | | | | __new__ (GH-108704) (GH-108739) When overriding the `__new__` method of an enum, the underlying data type should be created directly; i.e. . member = object.__new__(cls) member = int.__new__(cls, value) member = str.__new__(cls, value) Calling `super().__new__()` finds the lookup version of `Enum.__new__`, and will now raise an exception when detected. (cherry picked from commit d48760b2f1e28dd3c1a35721939f400a8ab619b8)
* [3.11] gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106694)Miss Islington (bot)2023-07-121-0/+6
| | | | | | gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666) (cherry picked from commit 357e9e9da3929cb9d55ea31896e66f488e44e8f2) Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
* [3.11] gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist ↵Miss Islington (bot)2023-07-111-6/+2
| | | | | | | | | | | | | | | | | (GH-106468) (#106621) gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist (GH-106468) For example: class Flag(enum.Flag): A = 0x01 B = 0x02 MASK = 0xff ~Flag.MASK is Flag(0) (cherry picked from commit 95b7426f45edb570869a5513c142f29ed9f851a1) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [3.11] gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. ↵Miss Islington (bot)2023-07-051-8/+7
| | | | | | | | (GH-105542) (#105571) When inverting a Flag member (or boundary STRICT), only consider other canonical flags; when inverting an IntFlag member (or boundary KEEP), also consider aliases. (cherry picked from commit 59f009e5898a006cdc8f5249be589de6edfe5cd0) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [3.11] gh-105332: [Enum] Fix unpickling flags in edge-cases (GH-105348) ↵Miss Islington (bot)2023-06-091-21/+9
| | | | | | | | | | (GH-105519) * revert enum pickling from by-name to by-value (cherry picked from commit 4ff5690e591b7d11cf11e34bf61004e2ea58ab3c) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [3.11] gh-104271: Fix auto() fallback in case of mixed type Enum (GH-104809)Miss Islington (bot)2023-05-231-1/+1
| | | | | | [3.12] gh-104271: Fix auto() fallback in case of mixed type Enum (GH-104279) (cherry picked from commit f4e2049f14d40c1b893c68530eec5e341cf3d929) Co-authored-by: Itamar Ostricher <itamarost@gmail.com>
* [3.11] gh-103479: [Enum] require __new__ to be considered a data type ↵Ethan Furman2023-04-131-1/+2
| | | | | | | | (GH-103495) (GH-103514) a mixin must either have a __new__ method, or be a dataclass, to be interpreted as a data-type; an __init__ method is not enough (restores pre-3.11 behavior for non-dataclasses). (cherry picked from commit a6f95941a3d686707fb38e0f37758e666f25e180) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-103365: [Enum] STRICT boundary corrections (GH-103494)Miss Islington (bot)2023-04-131-28/+39
| | | | | | | | | STRICT boundary: - fix bitwise operations - make default for Flag (cherry picked from commit 2194071540313e2bbdc7214d77453b9ce3034a5c) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [3.11] gh-93910: [Enum] remove member.member deprecation (GH-103236) (GH-103299)Ethan Furman2023-04-061-19/+23
| | | | | | i.e. Color.RED.BLUE is now officially supported.. (cherry picked from commit 4ec8dd10bd4682793559c4eccbcf6ae00688c4c3) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-102549: [Enum] fail enum creation when data type raises in __init__ ↵Miss Islington (bot)2023-04-031-14/+11
| | | | | | | (GH-103149) (cherry picked from commit 2a4d8c0a9e88f45047da640ce5a92b304d2d39b1) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-98298, gh-74730: [Enum] update docs (GH-103163)Miss Islington (bot)2023-04-031-4/+4
| | | | | | | fix FlagBoundary statements add warning about reloading modules and enum identity (cherry picked from commit 5ffc1e5a21de9a30566095386236db44695d184a) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-102558: [Enum] better handling of non-Enum EnumType classes (GH-103060)Miss Islington (bot)2023-03-271-7/+5
| | | | | (cherry picked from commit f4ed2c6ae5915329e49b9f94033ef182400e29fa) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-102558: [Enum] fix AttributeError during member repr() (GH-102601)Miss Islington (bot)2023-03-231-0/+2
| | | | | (cherry picked from commit bd063756b34003c1bc7cacf5b1bd90a409180fb6) Co-authored-by: Dong-hee Na <donghee.na@python.org>
* gh-101541: [Enum] create flag psuedo-member without calling original __new__ ↵Miss Islington (bot)2023-02-061-3/+2
| | | | | | | (GH-101590) (cherry picked from commit ef7c2bfcf1fd618438f981ace64499a99ae9fae0) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-100098: [Enum] insist on actual tuples, no subclasses, for auto (GH-100099)Miss Islington (bot)2022-12-081-1/+3
| | | | | | | | | When checking for auto() instances, only top-level usage is supported, which means either alone or as part of a regular tuple. Other containers, such as lists, dicts, or namedtuples, will not have auto() transformed into a value. (cherry picked from commit ded02ca54d7bfa32c8eab0871d56e4547cd356eb) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [Enum] update version TODO comment (GH-99458)Miss Islington (bot)2022-11-141-1/+1
| | | | | (cherry picked from commit db115682bd639a2642c617f0b7d5b30cd7d7f472) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-99248: [Enum] fix negative number infinite loop (GH-99256)Miss Islington (bot)2022-11-081-1/+7
| | | | | | | | | | | [Enum] fix negative number infinite loop - _iter_bits_lsb() now raises a ValueError if a negative number is passed in - verify() now skips checking negative numbers for named flags (cherry picked from commit 0b4ffb08ccdc21fc07ce90d3f78b58a25e1af653) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-93464: [Enum] fix auto() failure during multiple assignment (GH-99148)Miss Islington (bot)2022-11-061-8/+25
| | | | | | | | | * fix auto() failure during multiple assignment i.e. `ONE = auto(), 'text'` will now have `ONE' with the value of `(1, 'text')`. Before it would have been `(<an auto instance>, 'text')` (cherry picked from commit 8feb7ab77c80968a6de6079299a39b0494b1701b) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-96865: [Enum] fix Flag to use CONFORM boundary (GH-97528)Miss Islington (bot)2022-10-051-1/+1
| | | | | (cherry picked from commit b44372e03c5461b6ad3d89763a9eb6cb82df07a4) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [Enum] fix typos (GH-96285)Miss Islington (bot)2022-09-201-2/+2
| | | | | (cherry picked from commit 58882640d631b0be0d81156928de97c2b3556f45) Co-authored-by: wim glenn <wim.glenn@gmail.com>
* [3.11] [Enum] fix check in _test_simple_enum (GH-96435)Ethan Furman2022-08-301-1/+1
| | | | | | | The builtin `property` is not a callable, so was failing the check in `_test_simple_enum` causing a match failure; this adds `property` to the bypass list. Co-authored-by: Alexandru Mărășteanu <alexei@users.noreply.github.com>
* Revert "gh-93910: [Enum] restore member.member restriction while keeping ↵Ethan Furman2022-07-181-8/+0
| | | | | performance boost (GH-94913)" (#94981) This reverts commit 30f28ac296e506b336e0ab56c41422a53c36d0c2.
* gh-94601: [Enum] fix inheritance for __str__ and friends (GH-94942)Miss Islington (bot)2022-07-181-5/+21
| | | | | (cherry picked from commit c961d14f85a0e3e53d5ad1182206ef34030f10b8) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-93910: [Enum] restore member.member restriction while keeping performance ↵Miss Islington (bot)2022-07-171-0/+8
| | | | | | | boost (GH-94913) (cherry picked from commit c20186c3972ff38577c4c5e32ca86748210983d2) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-93910: Fix enum performance regression (GH-94614)Miss Islington (bot)2022-07-071-8/+9
| | | | | | | | | | | | | | | | | This removes the performance regression in 3.11, **at the expense of not fixing the "bug" that allows accessing values from values** (e.g. `Color.RED.BLUE`). Using the benchmark @markshannon [presented](https://github.com/python/cpython/issues/93910GH-issuecomment-1165503032), the results are: | Version | Enum | Fast enum | Normal class | | --- | --- | --- | --- | | 3.10 | 2.04 | 0.59 | 0.56 | | 3.11 | 2.78 | 0.31 | 0.15 | | This PR | 1.30 | 0.32 | 0.16 | I share this mostly as information about the source of the regression, as this may be useful. It may be that the lower-risk approach for the beta is just to revert to a previously-known working state. (cherry picked from commit ed136b96737fdbeff864079d12904cb962c6cce5) Co-authored-by: Michael Droettboom <mdboom@gmail.com>
* [3.11] gh-93820: Pickle enum.Flag by name (GH-93891). (GH-94288)Serhiy Storchaka2022-06-261-1/+15
| | | | | (cherry picked from commit 536985814a7116f14c9bc90aa1b3e3d36d5b2367) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* [Enum] Remove automatic docstring generation (GH-94188)Miss Islington (bot)2022-06-231-105/+0
| | | | | (cherry picked from commit 28a2ccfff279867b87aa31f56bfc97cf3d6b3afe) Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
* [Enum] fix typo (GH-94158)Miss Islington (bot)2022-06-231-8/+8
| | | | | (cherry picked from commit b4e0d6124a848a22df1ba12891329242c9e96f11) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-91456: [Enum] Deprecate default auto() behavior with mixed value types ↵Miss Islington (bot)2022-06-231-8/+26
| | | | | | | | | | (GH-91457) When used with plain Enum, auto() returns the last numeric value assigned, skipping any incompatible member values (such as strings); starting in 3.13 the default auto() for plain Enums will require all the values to be of compatible types, and will return a new value that is 1 higher than any existing value. Co-authored-by: Ethan Furman <ethan@stoneleaf.us> (cherry picked from commit fb1e9506c14ef32d5bec126dad6fa769c8c054f6) Co-authored-by: Oscar R <89599049+oscar-LT@users.noreply.github.com>
* gh-93847: Fix repr of enum of generic aliases (GH-93885)Miss Islington (bot)2022-06-171-2/+2
| | | | | (cherry picked from commit 138db8e48b0bb006b1561f8ec76ade97afc6cbd7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-93820: Fix copy() regression in enum.Flag (GH-93876) (#93886)Miss Islington (bot)2022-06-171-0/+3
| | | | | | | | | | | | | GH-26658 introduced a regression in copy / pickle protocol for combined `enum.Flag`s. `copy.copy(re.A | re.I)` would fail with `AttributeError: ASCII|IGNORECASE`. `enum.Flag` now has a `__reduce_ex__()` method that reduces flags by combined value, not by combined name. (cherry picked from commit 05b32c1c796d6c80479756ae898f488eac5f4f71) Co-authored-by: Christian Heimes <christian@python.org> Co-authored-by: Christian Heimes <christian@python.org>
* gh-93250: [Enum] Change IntEnum boundary to KEEP for backwards compatibility ↵Miss Islington (bot)2022-05-271-1/+1
| | | | | | | | (GH-93302) (GH-93304) In previous versions of Python if an IntEnum member was combined with another integer type value using a bit-wise operation, the resulting value would still be the IntEnum type. This change restores that behavior. (cherry picked from commit 70cfe56cafb2b549983f63d5d1a54654fe63c15c) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* [3.11] gh-93035: [Enum] Fix IntFlag crash when no single-bit members ↵Miss Islington (bot)2022-05-251-1/+1
| | | | | | | | (GH-93076) (GH-93197) `EnumType` attempts to create a custom docstring for each enum/flag, but that was failing with pathological flags that had no members (only multi-bit aliases). (cherry picked from commit 08cfc3dabf0f81a4494cd0d697befc7d0dec77b7) Co-authored-by: Tobin Yehle <tobinyehle@gmail.com>
* gh-93118: [Enum] fix error message (GH-93138) (GH-93142)Miss Islington (bot)2022-05-231-2/+3
| | | | Include member names in error message. (cherry picked from commit a49721ea075a18a7787ace6752b4eb0954e1b607)
* [3.11] gh-93100: [Enum] fix missing variable in global_str (GH-93107) (GH-93134)Ethan Furman2022-05-231-0/+1
| | | | | (cherry picked from commit 046df59658c9f64a9f0fc909ed62e92c6c4dd668) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* gh-78157: [Enum] nested classes will not be members in 3.13 (GH-92366)Ethan Furman2022-05-061-4/+53
| | | | | | | - add member() and nonmember() functions - add deprecation warning for internal classes in enums not becoming members in 3.13 Co-authored-by: edwardcwang
* 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
|