Commit graph

188 commits

Author SHA1 Message Date
Ethan Furman
d771729679
[3.12] gh-116040: [Enum] fix by-value calls when second value is falsey (GH-116072) (GH-116476)
e.g. Cardinal(1, 0)

(cherry picked from commit 13ffd4bd9f)
2024-03-07 15:26:13 -08:00
Miss Islington (bot)
e4fd5d542a
[3.12] gh-115539: Allow enum.Flag to have None members (GH-115636) (GH-115694)
gh-115539: Allow enum.Flag to have None members (GH-115636)
(cherry picked from commit c2cb31bbe1)

Co-authored-by: Jason Zhang <yurenzhang2017@gmail.com>
2024-02-19 16:18:40 -08:00
Miss Islington (bot)
79d1d6de1d
[3.12] gh-115252: Fix test_enum with -OO mode again (GH-115334) (GH-115396)
(cherry picked from commit ca3604a3e3)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-02-13 10:38:27 +00:00
Serhiy Storchaka
cfb79caaab
[3.12] gh-97959: Fix rendering of routines in pydoc (GH-113941) (GH-115296)
* Class methods no longer have "method of builtins.type instance" note.
* Corresponding notes are now added for class and unbound methods.
* Method and function aliases now have references to the module or the
  class where the origin was defined if it differs from the current.
* Bound methods are now listed in the static methods section.
* Methods of builtin classes are now supported as well as methods of
  Python classes.
(cherry picked from commit 2939ad02be)
2024-02-11 13:56:34 +00:00
Miss Islington (bot)
ada6629169
[3.12] gh-115252: Fix test_enum with -OO mode (GH-115253) (GH-115260)
(cherry picked from commit 33f56b7432)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-02-10 21:15:40 -08:00
Miss Islington (bot)
7d2f88edf0
[3.12] gh-114071: [Enum] update docs and code for tuples/subclasses (GH-114871) (GH-114993)
Update documentation with `__new__` and `__init__` entries.

Support use of `auto()` in tuple subclasses on member assignment lines.  Previously, auto() was only supported on the member definition line either solo or as part of a tuple:

    RED = auto()
    BLUE = auto(), 'azul'

However, since Python itself supports using tuple subclasses where tuples are expected, e.g.:

    from collections import namedtuple
    T = namedtuple('T', 'first second third')

    def test(one, two, three):
        print(one, two, three)

    test(*T(4, 5, 6))
    GH- 4 5 6

it made sense to also support tuple subclasses in enum definitions.
(cherry picked from commit ff7588b729)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2024-02-08 14:01:38 -08:00
Miss Islington (bot)
35e330ba85
[3.12] gh-114149: [Enum] revert GH-114196 and add more tuple-subclass tests (GH-114215) (GH-114218)
gh-114149: [Enum] revert GH-114160 and add more tuple-subclass tests (GH-114215)

This reverts commit 05e142b154.
(cherry picked from commit 4c7e09d012)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2024-01-17 20:36:28 -08:00
Miss Islington (bot)
c1890e666e
[3.12] gh-114149: [Enum] fix tuple subclass handling when using custom __new__ (GH-114160) (GH-114196)
(cherry picked from commit 33b47a2c28)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2024-01-17 13:34:48 -08:00
Miss Islington (bot)
ec00397912
[3.12] gh-111181: Fix enum doctests (GH-111180) (GH-111518)
gh-111181: Fix enum doctests (GH-111180)
(cherry picked from commit c4dc5a6ae8)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-10-31 15:01:28 -07:00
Miss Islington (bot)
ca848bbf78
[3.12] gh-109022: [Enum] require names=() to create empty enum type (GH-109048) (#109122)
gh-109022: [Enum] require `names=()` to create empty enum type (GH-109048)

add guard so that ``Enum('bar')`` raises a TypeError instead of
creating a new enum class called `bar`.  To create the new but
empty class, use:

    huh = Enum('bar', names=())
(cherry picked from commit c74e440168)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-09-12 15:53:16 +02:00
Miss Islington (bot)
8c3793a539
[3.12] gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704) (#108733)
gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704)

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)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-09-01 00:04:55 +02:00
Miss Islington (bot)
77d9fdf23b
[3.12] gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106695)
gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666)
(cherry picked from commit 357e9e9da3)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
2023-07-12 15:47:53 -07:00
Miss Islington (bot)
6968f9e4d3
[3.12] gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist (GH-106468) (#106620)
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>
2023-07-11 14:09:41 +02:00
Miss Islington (bot)
74d84cf84d
[3.12] gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (GH-105542) (#105572)
gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (GH-105542)

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>
2023-07-05 13:39:59 +02:00
Miss Islington (bot)
2f4a2d6c1b
[3.12] gh-105332: [Enum] Fix unpickling flags in edge-cases (GH-105348) (GH-105520)
* revert enum pickling from by-name to by-value

(cherry picked from commit 4ff5690e59)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-06-08 18:01:51 -07:00
Itamar Ostricher
f4e2049f14
[3.12] gh-104271: Fix auto() fallback in case of mixed type Enum (GH-104279)
gh-104271: Fix auto() fallback in case of mixed type Enum
2023-05-23 11:11:35 -07:00
Ethan Furman
700ec657c8
gh-103596: [Enum] do not shadow mixed-in methods/attributes (GH-103600)
For example:

    class Book(StrEnum):
        title = auto()
        author = auto()
        desc = auto()

    Book.author.desc is Book.desc

but

    Book.author.title() == 'Author'

is commonly expected.  Using upper-case member names avoids this confusion and possible performance impacts.

Co-authored-by: samypr100 <3933065+samypr100@users.noreply.github.com>
2023-04-18 16:19:23 -07:00
Ethan Furman
a6f95941a3
gh-103479: [Enum] require __new__ to be considered a data type (GH-103495)
a mixin must either have a __new__ method, or be a dataclass, to be interpreted as a data-type
2023-04-13 08:31:03 -07:00
Ethan Furman
2194071540
gh-103365: [Enum] STRICT boundary corrections (GH-103494)
STRICT boundary:

- fix bitwise operations
- make default for Flag
2023-04-13 08:24:33 -07:00
Ethan Furman
4ec8dd10bd
gh-93910: [Enum] remove member.member deprecation (GH-103236)
i.e. Color.RED.BLUE is now officially supported.
2023-04-05 17:33:52 -07:00
Ethan Furman
2a4d8c0a9e
gh-102549: [Enum] fail enum creation when data type raises in __init__ (GH-103149) 2023-03-31 13:52:31 -07:00
Ethan Furman
f4ed2c6ae5
gh-102558: [Enum] better handling of non-Enum EnumType classes (GH-103060) 2023-03-27 16:26:16 -07:00
Ethan Furman
b838d80085
gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod (GH-103062) 2023-03-27 16:25:19 -07:00
Dong-hee Na
bd063756b3
gh-102558: [Enum] fix AttributeError during member repr() (GH-102601) 2023-03-23 13:30:18 -07:00
JosephSBoyle
a028778d4c
Rename redundant enum tests so that they run (#102535) 2023-03-14 15:16:45 -07:00
Alex Waygood
401d7a7f00
gh-102515: Remove unused imports in the Lib/ directory (#102516) 2023-03-08 11:45:38 +00:00
Ethan Furman
ef7c2bfcf1
gh-101541: [Enum] create flag psuedo-member without calling original __new__ (GH-101590) 2023-02-05 19:29:06 -08:00
Ethan Furman
a5a7cea202
gh-100039: enhance __signature__ to work with str and callables (GH-100168)
Callables should be either class- or static-methods.
Enum now uses the classmethod version to greatly improve the help
given for enums and flags.
2022-12-16 12:30:47 -08:00
Ethan Furman
ded02ca54d
gh-100098: [Enum] insist on actual tuples, no subclasses, for auto (GH-100099)
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.
2022-12-07 22:58:08 -08:00
Nikita Sobolev
889b0b9bf9
[Enum] Remove unused code from test_enum.py (GH-96986) 2022-12-06 18:44:47 -08:00
Ethan Furman
679efbb080
gh-94943: [Enum] improve repr() when inheriting from a dataclass (GH-99740)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
2022-12-06 13:43:41 -08:00
Nikita Sobolev
5cfb7d19f5
gh-99518: Fix escape symbol in test_enum (#99519) 2022-11-16 15:06:37 +01:00
Ethan Furman
65dab1506e
gh-92647: [Enum] use final status to determine lookup or create (GH-99500)
* use final status to determine lookup or create

* 📜🤖 Added by blurb_it.

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2022-11-15 08:49:22 -08:00
Ethan Furman
0b4ffb08cc
gh-99248: [Enum] fix negative number infinite loop (GH-99256)
[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
2022-11-08 12:00:19 -08:00
Ethan Furman
a71b117c3d
[Enum] add built-in property to test_test_simple_enum (GH-98453) 2022-11-06 12:03:42 -08:00
Ethan Furman
8feb7ab77c
gh-93464: [Enum] fix auto() failure during multiple assignment (GH-99148)
* 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')`
2022-11-05 18:01:08 -07:00
Ethan Furman
b44372e03c
gh-96865: [Enum] fix Flag to use CONFORM boundary (GH-97528) 2022-10-05 15:25:55 -07:00
Nikita Sobolev
9b58eaf98c
gh-95591: [Enum] use _FlagTests base class (GH-96475) 2022-09-20 16:08:39 -07:00
Ethan Furman
4e704d7847
gh-95077: [Enum] add code-based deprecation warnings for member.member access (GH-95083)
* issue deprecation warning for member.member access
* always store member property in current class
* remove __getattr__
2022-07-25 11:05:10 -07:00
Ethan Furman
73eab9f35c
Revert "gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)" (#94985)
This reverts commit c20186c397.
2022-07-18 13:22:52 -07:00
Ethan Furman
c961d14f85
gh-94601: [Enum] fix inheritance for __str__ and friends (GH-94942) 2022-07-17 18:51:04 -07:00
Ethan Furman
c20186c397
gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913) 2022-07-16 18:13:57 -07:00
Michael Droettboom
ed136b9673
gh-93910: Fix enum performance regression (GH-94614)
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/93910#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.
2022-07-07 04:26:56 -07:00
Serhiy Storchaka
33fc3b5e42
gh-94318: Strip trailing spaces in pydoc text output (GH-94319) 2022-06-27 13:33:34 +03:00
Serhiy Storchaka
536985814a
gh-93820: Pickle enum.Flag by name (GH-93891) 2022-06-26 10:54:00 +03:00
Sam Ezeh
28a2ccfff2
[Enum] Remove automatic docstring generation (GH-94188) 2022-06-23 13:35:37 -07:00
Oscar R
fb1e9506c1
gh-91456: [Enum] Deprecate default auto() behavior with mixed value types (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>
2022-06-22 23:20:24 -07:00
Carl Bordum Hansen
9a479c3c10
gh-88123: Implement new Enum __contains__ (GH-93298)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-06-22 00:04:04 -07:00
Serhiy Storchaka
138db8e48b
gh-93847: Fix repr of enum of generic aliases (GH-93885) 2022-06-16 12:16:12 -07:00
Christian Heimes
05b32c1c79
gh-93820: Fix copy() regression in enum.Flag (GH-93876)
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.
2022-06-15 23:42:36 -07:00