* and fix global flag repr
(cherry picked from commit 06e29a224f)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
gh-115539: Allow enum.Flag to have None members (GH-115636)
(cherry picked from commit c2cb31bbe1)
Co-authored-by: Jason Zhang <yurenzhang2017@gmail.com>
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 d48760b2f1)
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 95b7426f45)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
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 59f009e589)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
[3.12] gh-104271: Fix auto() fallback in case of mixed type Enum (GH-104279)
(cherry picked from commit f4e2049f14)
Co-authored-by: Itamar Ostricher <itamarost@gmail.com>
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 a6f95941a3)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
STRICT boundary:
- fix bitwise operations
- make default for Flag
(cherry picked from commit 2194071540)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
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 ded02ca54d)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
[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 0b4ffb08cc)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* 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 8feb7ab77c)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
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>
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 ed136b9673)
Co-authored-by: Michael Droettboom <mdboom@gmail.com>
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 fb1e9506c1)
Co-authored-by: Oscar R <89599049+oscar-LT@users.noreply.github.com>
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 05b32c1c79)
Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Christian Heimes <christian@python.org>
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 70cfe56caf)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
`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 08cfc3dabf)
Co-authored-by: Tobin Yehle <tobinyehle@gmail.com>
- add member() and nonmember() functions
- add deprecation warning for internal classes in enums not
becoming members in 3.13
Co-authored-by: edwardcwang
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`
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.
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.
* 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>
* [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'