Commit graph

172 commits

Author SHA1 Message Date
Miss Islington (bot)
5562a09821
[3.11] gh-116600: [Enum] fix global Flag repr (GH-116615) (#116630)
* 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>
2024-03-11 17:36:15 -07:00
Miss Islington (bot)
b3384af464
[3.11] gh-115539: Allow enum.Flag to have None members (GH-115636) (GH-115695)
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:30 -08:00
Serhiy Storchaka
7ba2d78cbb
[3.11] gh-115252: Fix test_enum with -OO mode again (GH-115334) (GH-115397)
(cherry picked from commit ca3604a3e3)
2024-02-13 10:40:40 +00:00
Nikita Sobolev
879d8c4660
[3.11] gh-115252: Fix test_enum with -OO mode (GH-115253) (#115279)
(cherry picked from commit 33f56b7432)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-02-11 18:19:59 +03:00
Serhiy Storchaka
b9e90031a3
[3.11] [3.12] gh-97959: Fix rendering of routines in pydoc (GH-113941) (GH-115296) (GH-115302)
* 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)
(cherry picked from commit cfb79caaab)
2024-02-11 15:19:43 +00:00
Ethan Furman
4a61169672
[3.11] gh-111181: Fix enum doctests (GH-111180) (GH-111617)
gh-111181: Fix enum doctests (GH-111180)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
(cherry picked from commit c4dc5a6ae8)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-11-03 16:51:56 -07:00
Ethan Furman
effa2ecdcf
[3.11] gh-108682: [Enum] raise TypeError if super().__new__ called in custom __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 d48760b2f1)
2023-09-07 18:57:48 -07:00
Miss Islington (bot)
a276ce4505
[3.11] gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106694)
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:48:16 -07:00
Miss Islington (bot)
318f6ae1cd
[3.11] gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist (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 95b7426f45)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-07-11 14:10:02 +02:00
Miss Islington (bot)
3f244b2f6e
[3.11] gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (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 59f009e589)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-07-05 13:59:18 +02:00
Miss Islington (bot)
fed1b5a198
[3.11] gh-105332: [Enum] Fix unpickling flags in edge-cases (GH-105348) (GH-105519)
* 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:02:12 -07:00
Miss Islington (bot)
582aadc80e
[3.11] gh-104271: Fix auto() fallback in case of mixed type Enum (GH-104809)
[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>
2023-05-23 12:48:20 -07:00
Ethan Furman
3b929a7b32
[3.11] gh-103479: [Enum] require __new__ to be considered a data type (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 a6f95941a3)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-04-13 12:04:06 -07:00
Miss Islington (bot)
804a973d8e
gh-103365: [Enum] STRICT boundary corrections (GH-103494)
STRICT boundary:

- fix bitwise operations
- make default for Flag
(cherry picked from commit 2194071540)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-04-13 08:55:53 -07:00
Ethan Furman
58e330ac9c
[3.11] gh-93910: [Enum] remove member.member deprecation (GH-103236) (GH-103299)
i.e. Color.RED.BLUE is now officially supported..
(cherry picked from commit 4ec8dd10bd)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-04-05 21:29:14 -07:00
Miss Islington (bot)
5342f5e713
gh-102549: [Enum] fail enum creation when data type raises in __init__ (GH-103149)
(cherry picked from commit 2a4d8c0a9e)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-04-03 16:01:31 -07:00
Miss Islington (bot)
5ba5ce47f6
gh-102558: [Enum] better handling of non-Enum EnumType classes (GH-103060)
(cherry picked from commit f4ed2c6ae5)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-03-27 16:49:15 -07:00
Miss Islington (bot)
8132aefa0f
gh-102558: [Enum] fix AttributeError during member repr() (GH-102601)
(cherry picked from commit bd063756b3)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
2023-03-23 13:56:38 -07:00
Miss Islington (bot)
1649f3e591
Rename redundant enum tests so that they run (GH-102535)
(cherry picked from commit a028778d4c)

Co-authored-by: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
2023-03-14 15:46:01 -07:00
Miss Islington (bot)
cf8973c638
gh-101541: [Enum] create flag psuedo-member without calling original __new__ (GH-101590)
(cherry picked from commit ef7c2bfcf1)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2023-02-05 19:52:22 -08:00
Miss Islington (bot)
d4426c8295
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.
(cherry picked from commit ded02ca54d)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-12-07 23:21:29 -08:00
Miss Islington (bot)
4f31171e3f
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
(cherry picked from commit 0b4ffb08cc)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-11-08 12:33:21 -08:00
Miss Islington (bot)
be4bab8c2b
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')`
(cherry picked from commit 8feb7ab77c)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-11-06 06:30:32 -08:00
Miss Islington (bot)
c9480d5ad5
gh-96865: [Enum] fix Flag to use CONFORM boundary (GH-97528)
(cherry picked from commit b44372e03c)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-10-05 16:32:16 -07:00
Miss Islington (bot)
fd118a0850
gh-95591: [Enum] use _FlagTests base class (GH-96475)
(cherry picked from commit 9b58eaf98c)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-09-20 16:34:14 -07:00
Ethan Furman
8f58db2279
[3.11] [Enum] fix check in _test_simple_enum (GH-96435)
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>
2022-08-30 12:39:03 -07:00
Ethan Furman
be84daf52a
Revert "gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)" (#94981)
This reverts commit 30f28ac296.
2022-07-18 13:56:21 -07:00
Miss Islington (bot)
e747562345
gh-94601: [Enum] fix inheritance for __str__ and friends (GH-94942)
(cherry picked from commit c961d14f85)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-07-17 19:18:41 -07:00
Miss Islington (bot)
30f28ac296
gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)
(cherry picked from commit c20186c397)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-07-16 21:53:41 -07:00
Miss Islington (bot)
65c431685b
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/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>
2022-07-07 05:01:09 -07:00
Serhiy Storchaka
1b27ec5ac6
[3.11] gh-93820: Pickle enum.Flag by name (GH-93891). (GH-94288)
(cherry picked from commit 536985814a)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2022-06-26 14:24:04 +03:00
Miss Islington (bot)
fbf31454e9
[Enum] Remove automatic docstring generation (GH-94188)
(cherry picked from commit 28a2ccfff2)

Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
2022-06-23 14:18:20 -07:00
Miss Islington (bot)
321acd4138
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>
(cherry picked from commit fb1e9506c1)

Co-authored-by: Oscar R <89599049+oscar-LT@users.noreply.github.com>
2022-06-22 23:46:33 -07:00
Miss Islington (bot)
0319052090
gh-93847: Fix repr of enum of generic aliases (GH-93885)
(cherry picked from commit 138db8e48b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2022-06-16 22:00:22 -07:00
Miss Islington (bot)
3fbf5c6427
gh-93820: Fix copy() regression in enum.Flag (GH-93876) (#93886)
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>
2022-06-17 06:56:20 +03:00
Miss Islington (bot)
647426d4fa
gh-93250: [Enum] Change IntEnum boundary to KEEP for backwards compatibility (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 70cfe56caf)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-05-27 15:43:13 -07:00
Ethan Furman
96218f774e
[3.11] gh-93100: [Enum] fix missing variable in global_str (GH-93107) (GH-93134)
(cherry picked from commit 046df59658)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
2022-05-23 10:11:18 -07:00
Ethan Furman
93364f9716
gh-78157: [Enum] nested classes will not be members in 3.13 (GH-92366)
- add member() and nonmember() functions
- add deprecation warning for internal classes in enums not
  becoming members in 3.13

Co-authored-by: edwardcwang
2022-05-06 00:16:22 -07:00
Christian Heimes
2b16a08bc7
bpo-40280: Detect missing threading on WASM platforms (GH-32352)
Co-authored-by: Brett Cannon <brett@python.org>
2022-04-07 09:22:47 +02:00
Nikita Sobolev
6c83c8e6b5
bpo-46198: rename duplicate tests and remove unused code (GH-30297) 2022-03-10 08:20:11 -08:00
Ethan Furman
353e3b2820
bpo-46477: [Enum] ensure Flag subclasses have correct bitwise methods (GH-30816) 2022-01-22 18:27:52 -08:00
Ethan Furman
7c0914d35e
bpo-45535: [Enum] include special dunders in dir() (GH-30677)
Include the `__dunders__` in `dir()` that make `Enum` special:

- `__contains__`
- `__getitem__`
- `__iter__`
- `__len__`
- `__members__`
2022-01-18 15:13:13 -08:00
Nikita Sobolev
596cf51a4d
bpo-46418: [Enum] simplify MODULE declaration in tests (GH-30647) 2022-01-17 12:16:56 -08:00
Kumar Aditya
83d544b929
bpo-40066: [Enum] skip failing doc test (GH-30637) 2022-01-17 07:18:13 -08:00
Victor Stinner
42a64c03ec
Revert "bpo-40066: [Enum] update str() and format() output (GH-30582)" (GH-30632)
This reverts commit acf7403f9b.
2022-01-17 13:58:40 +01:00
Ethan Furman
acf7403f9b
bpo-40066: [Enum] update str() and format() output (GH-30582)
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`
2022-01-15 22:41:43 -08:00
Nikita Sobolev
e674e48ddc
bpo-46242: [Enum] better error message for extending Enum with members (GH-30357) 2022-01-14 14:18:00 -08:00
Nikita Sobolev
13e4659276
bpo-46327: [Enum] remove skipped tests (GH-30512) 2022-01-10 11:12:34 -08:00
Nikita Sobolev
582286d71c
bpo-46301: [Enum] fix refleak tests (GH30510) 2022-01-10 11:09:00 -08:00
Nikita Sobolev
8d59d2563b
bpo-46301: [Enum] test uncomparable values in _convert_ (GH-30472)
add tests that cover different types, and same non-comparable types
2022-01-08 11:43:42 -08:00