| Commit message (Collapse) | Author | Age | Files | Lines |
|
|
|
|
|
|
|
|
| |
* 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
|
|
|
|
| |
* update docs, renable doc tests
* make deprecation warning active for two releases
|
|
|
|
|
|
|
| |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
|
|
|
|
|
|
|
|
|
|
|
| |
`type.__new__` calls `__set_name__` and `__init_subclass__`, which means
that any work metaclasses do after calling `super().__new__()` will not
be available to those two methods. In particular, `Enum` classes that
want to make use of `__init_subclass__` will not see any members.
Almost all customization is therefore moved to before the
`type.__new__()` call, including changing all members to a proto member
descriptor with a `__set_name__` that will do the final conversion of a
member to be an instance of the `Enum` class.
|
|
|
| |
Solution to support calls to `__init_subclass__` with members defined is too brittle and breaks with certain mixins.
|
|
|
|
|
|
|
|
| |
for multiple inheritance support:
use super().new
pass **kwds to super().new
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
This allows easier Enum construction in unusual cases, such as including dynamic member definitions into a class definition:
# created dynamically
foo_defines = {'FOO_CAT': 'aloof', 'BAR_DOG': 'friendly', 'FOO_HORSE': 'big'}
class Foo(Enum):
vars().update({
k: v
for k, v in foo_defines.items()
if k.startswith('FOO_')
})
def upper(self):
# example method
return self.value.upper()
|
|
|
| |
The default for auto() is to return an integer, which doesn't work for `StrEnum`. The new `_generate_next_value_` for `StrEnum` returns the member name, lower cased.
|
|
|
| |
private names, such as `_Color__hue` and `_Color__hue_` are now normal attributes, and do not become members nor raise exceptions
|
|
|
|
|
| |
When creating an Enum, type.__new__ calls __init_subclass__, but at that point the members have not been added.
This patch suppresses the initial call, then manually calls the ancestor __init_subclass__ before returning the new Enum class.
|
| |
|
| |
|
| |
|
|
|
| |
use `str.__str__` for `StrEnum` so that `str(StrEnum.member)` is the same as directly accessing the string value of the `StrEnum` member
|
|
|
|
| |
`StrEnum` ensures that its members were already strings, or intended to
be strings.
|
| |
|
| |
|
| |
|
|
|
|
| |
fix default `_missing_` to return `None` instead of raising a `ValueError`
Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com>
|
|
|
| |
fix bug that let Enums be extended via multiple inheritance
|
|
|
|
|
| |
EnumMeta double-checks that `__repr__`, `__str__`, `__format__`, and `__reduce_ex__` are not the same as `object`'s, and replaces them if they are -- even if that replacement was intentionally done in the Enum being constructed. This patch fixes that.
Automerge-Triggered-By: @ethanfurman
|
| |
|
| |
|
|
|
| |
require `_generate_next_value_` to be defined before members
|
|
|
|
|
|
| |
* Improve the performance of _decompose() in enum.py
Co-Authored-By: Brandt Bucher <brandtbucher@gmail.com>
|
|
|
|
|
|
|
| |
creating cycles (GH-17246)
Capturing exceptions into names can lead to reference cycles though the __traceback__ attribute of the exceptions in some obscure cases that have been reported previously and fixed individually. As these variables are not used anyway, we can remove the binding to reduce the chances of creating reference cycles.
See for example GH-13135
|
| |
|
|
|
|
|
| |
(GH-14809)
* Use __qualname__ instead of __name__ in enum exception messages.
|
|
|
|
|
| |
__str__ (GH-14545)
* bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__
|
| |
|
|
|
|
|
|
|
|
|
|
| |
Co-authored-by: Maxwell <maxwellpxt@gmail.com>
Co-authored-by: Stéphane Wirtel <stephane@wirtel.be>
https://bugs.python.org/issue35899
|
|
|
| |
https://bugs.python.org/issue17467
|
|
|
| |
bpo-35585: Speed up enum by-value lookup
|
| |
|
|
|
| |
* bpo-29577: allow multiple mixin classes
|
|
|
| |
* raise exception if _missing_ returns None or invalid type
|
|
|
|
|
| |
* Fix enum members getting shadowed by parent attributes
* Move Enum._convert to EnumMeta._convert_
* Deprecate _convert
|
|
|
| |
* bpo-33217: Raise TypeError for non-Enum lookups in Enums
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
* bpo-31801: Enum: add _ignore_ as class option
_ignore_ is a list, or white-space seperated str, of names that will not
be candidates for members; these names, and _ignore_ itself, are removed
from the final class.
* bpo-31801: Enum: add documentation for _ignore_
* bpo-31801: Enum: remove trailing whitespace
* bpo-31801: Enum: fix bulleted list format
* bpo-31801: add version added for _ignore_
|
| |
|
| |
|
|
|
|
|
|
|
|
| |
* bpo-30616: Functional API of enum allows to create empty enums.
* Update NEWS
move addition to avoid conflict
|
| |
|
| |
|
|
|
|
| |
Guido; patch by Chris Angelico.
|
| |
|