summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_enum.py
Commit message (Collapse)AuthorAgeFilesLines
* Revert "gh-93910: [Enum] restore member.member restriction while keeping ↵Ethan Furman2022-07-181-6/+1
| | | | | 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-1/+6
| | | | | | | 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-0/+1
| | | | | | | | | | | | | | | | | 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-2/+42
| | | | | (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-162/+4
| | | | | (cherry picked from commit 28a2ccfff279867b87aa31f56bfc97cf3d6b3afe) Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
* gh-91456: [Enum] Deprecate default auto() behavior with mixed value types ↵Miss Islington (bot)2022-06-231-9/+56
| | | | | | | | | | (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-0/+10
| | | | | (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/+28
| | | | | | | | | | | | | 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-2/+10
| | | | | | | | (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-93100: [Enum] fix missing variable in global_str (GH-93107) (GH-93134)Ethan Furman2022-05-231-0/+11
| | | | | (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-0/+141
| | | | | | | - add member() and nonmember() functions - add deprecation warning for internal classes in enums not becoming members in 3.13 Co-authored-by: edwardcwang
* bpo-40280: Detect missing threading on WASM platforms (GH-32352)Christian Heimes2022-04-071-0/+2
| | | Co-authored-by: Brett Cannon <brett@python.org>
* bpo-46198: rename duplicate tests and remove unused code (GH-30297)Nikita Sobolev2022-03-101-1/+1
|
* bpo-46477: [Enum] ensure Flag subclasses have correct bitwise methods (GH-30816)Ethan Furman2022-01-231-0/+7
|
* bpo-45535: [Enum] include special dunders in dir() (GH-30677)Ethan Furman2022-01-181-24/+29
| | | | | | | | | Include the `__dunders__` in `dir()` that make `Enum` special: - `__contains__` - `__getitem__` - `__iter__` - `__len__` - `__members__`
* bpo-46418: [Enum] simplify `MODULE` declaration in tests (GH-30647)Nikita Sobolev2022-01-171-1/+1
|
* bpo-40066: [Enum] skip failing doc test (GH-30637)Kumar Aditya2022-01-171-1505/+1359
|
* Revert "bpo-40066: [Enum] update str() and format() output (GH-30582)" ↵Victor Stinner2022-01-171-1359/+1505
| | | | | (GH-30632) This reverts commit acf7403f9baea3ae1119fc6b4a3298522188bf96.
* bpo-40066: [Enum] update str() and format() output (GH-30582)Ethan Furman2022-01-161-1505/+1359
| | | | | | | | | | | | | | | 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-0/+2
| | | | (GH-30357)
* bpo-46327: [Enum] remove skipped tests (GH-30512)Nikita Sobolev2022-01-101-11/+0
|
* bpo-46301: [Enum] fix refleak tests (GH30510)Nikita Sobolev2022-01-101-44/+58
|
* bpo-46301: [Enum] test uncomparable values in `_convert_` (GH-30472)Nikita Sobolev2022-01-081-0/+35
| | | add tests that cover different types, and same non-comparable types
* bpo-46296: [Enum] add a test for missing `value` recovery (GH-30458)Nikita Sobolev2022-01-071-0/+10
| | | In `__set_name__` there is a check for the `_value_` attribute and an attempt to add it if missing; this adds a test to cover the case for simple enums with a custom `__new__` method.
* bpo-46262: [Enum] test error path in `Flag._missing_` (GH-30408)Nikita Sobolev2022-01-051-0/+26
| | | | | | add tests that exercise the `_missing_` error path for `Flag` and `IntFlag` Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
* bpo-45535: Improve output of Enum ``dir()`` (GH-29316)Alex Waygood2021-12-021-37/+320
| | | | | | 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-25130: Add calls of gc.collect() in tests to support PyPy (GH-28005)Serhiy Storchaka2021-08-291-1/+3
|
* bpo-44929: [Enum] Fix global repr (GH-27789)Pablo Galindo Salgado2021-08-251-11/+57
| | | | | | | | | | | | * 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-44564 Move formatted assertion under deprecation warning context (GH-27090)Brandon Schabell2021-08-041-1/+1
|
* bpo-43945: [Enum] reduce scope of new format() behavior (GH-26752)Ethan Furman2021-06-181-18/+161
| | | | | | | | | | | | | | | | | | | | | * [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-2/+2
|
* [Enum] improve test, add andrei kulakov to ACKS (GH-26726)Ethan Furman2021-06-151-0/+23
|
* bpo-44242: [Enum] improve error messages (GH-26669)Ethan Furman2021-06-111-7/+5
|
* bpo-44342: [Enum] fix data type search (GH-26667)Ethan Furman2021-06-111-0/+8
| | | | | | | 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-4/+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-0/+47
| | | | | | | | | | | | | | (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-15/+129
| | | | Move the check for missing named flags in flag aliases from Flag creation to a new *verify* decorator.
* bpo-43957: [Enum] Deprecate ``TypeError`` from containment checks. (GH-25670)Ethan Furman2021-04-271-26/+117
| | | | | 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: Properly re-intialize module variables in test_enum (GH-25516)Ammar Askar2021-04-221-2/+13
| | | | | | | | | | Previously TestIntEnumConvert and TestStrEnumConvert would end up converting the module level variables from their regular int form to a `test.test_enum.X` instance after _convert would run. This meant that after a single test ran, the next set of _convert functions would be operating on the enum instances rather than ints. This would cause some tests such as the one involving format to fail when running under a mode that repeatedly runs test such as the refleak finder.
* bpo-38659: [Enum] add _simple_enum decorator (GH-25497)Ethan Furman2021-04-211-5/+56
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-56/+5
| | | This reverts commit dbac8f40e81eb0a29dc833e6409a1abf47467da6.
* bpo-38659: [Enum] add _simple_enum decorator (GH-25285)Ethan Furman2021-04-201-5/+56
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-0/+25
| | | | ``_is_private`` now returns ``False`` instead of raising an exception when enum name matches beginning of class name as used in a private variable
* bpo-42248: [Enum] ensure exceptions raised in ``_missing_`` are released ↵Ethan Furman2021-04-121-0/+32
| | | | (GH-25350)
* bpo-40066: Enum: modify `repr()` and `str()` (GH-22392)Ethan Furman2021-03-311-118/+149
| | | | | | | | | * 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-3/+3
| | | | * 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-0/+23
| | | | | | | 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] only include .rst test if file available (GH-24342)Ethan Furman2021-01-261-4/+6
| | | | | | | * [Enum] only include .rst test if file available In order to ensure the ReST documentation is up to date for Enum, use doctest to check it -- but only if the .rst files have not been stripped.