enum
frequenz.core.enum ¤
Enum utilities with support for deprecated members.
This module provides an Enum base class that extends the
standard library's enum.Enum to support marking certain members as deprecated.
See the class documentation for details and examples.
Attributes¤
frequenz.core.enum.EnumT
module-attribute
¤
Type variable for enum types.
frequenz.core.enum.ValueT
module-attribute
¤
ValueT = TypeVar('ValueT')
Type variable for enum member values.
Classes¤
frequenz.core.enum.DeprecatedMember ¤
Class to mark members as deprecated.
This class should not be used directly, use
deprecated_member instead.
Please read the Enum documentation for details and
examples.
Source code in src/frequenz/core/enum.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
Attributes¤
message
instance-attribute
¤
message: str | None = message
The warning message, or None if it is generated from new_name.
A generated message is only known once the enum class is created, since it includes the class' fully qualified name, and is not stored here.
new_name
instance-attribute
¤
new_name: str | None = new_name
The name of the member to use instead, in the same enum, if given.
since
instance-attribute
¤
since: str | None = since
The version the member is deprecated since, if given.
It is only used for the documentation, never in the warning.
Methods:¤
__init__ ¤
__init__(
value: Any,
message: str | None = None,
*,
new_name: str | None = None,
since: str | None = None
) -> None
Initialize this instance.
At least one of message and new_name must be given.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value of the enum member to mark as deprecated.
TYPE:
|
message
|
The warning message to emit when the member is reached, or
TYPE:
|
new_name
|
The name of the member to use instead, in the same enum.
TYPE:
|
since
|
The version the member is deprecated since, such as
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If neither |
ValueError
|
If |
Source code in src/frequenz/core/enum.py
frequenz.core.enum.DeprecatedMemberWarning ¤
Bases: DeprecationWarning
Warning category for deprecated enum members.
frequenz.core.enum.DeprecatingEnumType ¤
Bases: EnumType
Enum metaclass that supports deprecated members.
Tip
Normally it is not necessary to use this class directly, use
Enum instead.
Behavior:
- In the class body, members may be declared as
NAME = deprecated_member(value, ...). Seedeprecated_member()andDeprecatedMemberfor details. - During class creation, these wrappers are replaced with
valueso that a normal enum member or alias is created byEnumType. - Once the class is created, each
new_nameis checked to be a member that is not deprecated, and the warnings of members without amessageare generated from it, now that the fully qualified name of the class is known. -
The deprecated names are recorded so that:
MyEnum.NAMEwarns (attribute access by name)MyEnum["NAME"]warns (lookup by name)MyEnum(value)warns only if the resolved member has no non-deprecated aliases (all names for that member are deprecated).
Source code in src/frequenz/core/enum.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
Methods:¤
__call__ ¤
Resolve value to a member, warning if the member is purely deprecated.
Source code in src/frequenz/core/enum.py
__getattribute__ ¤
Resolve name to a member, warning if the member is deprecated.
Source code in src/frequenz/core/enum.py
__getitem__ ¤
Resolve name to a member, warning if the member is deprecated.
Source code in src/frequenz/core/enum.py
__new__ ¤
__new__(
mcs,
name: str,
bases: tuple[type[EnumT], ...],
classdict: Mapping[str, Any],
**kw: Any
) -> type[EnumT]
Create the new enum class rewriting deprecated members.
Members declared as
DeprecatedMember are replaced
with their value.
Source code in src/frequenz/core/enum.py
frequenz.core.enum.Enum ¤
Bases: Enum
Base class for enums that support deprecated members.
This class extends the standard library's enum.Enum to support marking
certain members as deprecated. Deprecated members can be accessed, but doing so
will emit a DeprecationWarning, specifically
a DeprecatedMemberWarning.
To declare a deprecated member, use the
deprecated_member() function.
When using the enum constructor (i.e. MyEnum(value)), a warning is only emitted if
the resolved member has no non-deprecated aliases. If there is at least one
non-deprecated alias for the member, no warning is emitted.
Example
from frequenz.core.enum import Enum, deprecated_member
class TaskStatus(Enum):
OPEN = 1
IN_PROGRESS = 2
FINISHED = 3
PENDING = deprecated_member(1, new_name="OPEN", since="v1.2.0")
CANCELLED = deprecated_member(
4,
"mypkg.TaskStatus.CANCELLED is deprecated. "
"Cancelled tasks are deleted now.",
)
"""The task was cancelled.
Deprecated:
Deprecated since v1.3.0. Cancelled tasks are deleted now.
"""
# Accessing deprecated members by name warns:
status1 = TaskStatus.PENDING
assert status1 is TaskStatus.OPEN
status2 = TaskStatus["PENDING"]
assert status2 is TaskStatus.OPEN
# No warning, 1 resolves to OPEN, which is not deprecated
status3 = TaskStatus(1)
assert status3 is TaskStatus.OPEN
# Warns, as all the names for 4 are deprecated
status4 = TaskStatus(4)
assert status4.name == "CANCELLED"
PENDING is a plain rename, so new_name is enough: reaching it warns
with mypkg.TaskStatus.PENDING is deprecated. Use
mypkg.TaskStatus.OPEN instead., mypkg being the module defining
the enum, and
griffe-frequenz-core
documents it with a Deprecated since v1.2.0. Use
[`OPEN`][mypkg.TaskStatus.OPEN] instead. notice.
CANCELLED has no member to point at, so it gives its own message,
starting with its fully qualified name. That replaces the warning
only, so its notice is written by hand, as a Deprecated: admonition
in the member's docstring. That notice says the version too, so
since is not needed.
Source code in src/frequenz/core/enum.py
Functions:¤
frequenz.core.enum.deprecated_member ¤
deprecated_member(
value: ValueT,
message: str | None = None,
*,
new_name: str | None = None,
since: str | None = None
) -> ValueT
Mark an enum member as deprecated.
At least one of message and new_name must be given. With new_name alone,
reaching the member warns with <Enum>.<NAME> is deprecated. Use
<Enum>.<new_name> instead., with fully qualified names, which is what the
deprecations guide asks for. message replaces that warning.
since is not part of the warning: it is only there for the documentation.
griffe-frequenz-core
reads it, together with new_name, to generate the member's Deprecated:
notice, so message changes the warning only. To change the notice, write a
Deprecated: admonition by hand in the member's docstring, which replaces the
generated one.
When the enum class is created, new_name is checked to be another member of
the same enum, which is not deprecated itself.
Please read the Enum documentation for details and
examples.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value of the enum member to mark as deprecated.
TYPE:
|
message
|
The warning message to emit when the member is reached, or
TYPE:
|
new_name
|
The name of the member to use instead, in the same enum.
TYPE:
|
since
|
The version the member is deprecated since, such as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ValueT
|
The wrapped value, to mark the enum member as deprecated. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If neither |
ValueError
|
If |
Source code in src/frequenz/core/enum.py
frequenz.core.enum.unique ¤
Class decorator for enums that ensures unique non-deprecated values.
This works similarly to @enum.unique, but it only enforces
uniqueness for members that are not deprecated. This allows deprecated members to
be aliases for non-deprecated members without causing a ValueError.
If you need strict uniqueness for all deprecated and non-deprecated members, use
@enum.unique instead.
Example
| PARAMETER | DESCRIPTION |
|---|---|
enumeration
|
The enum class to decorate. |
| RETURNS | DESCRIPTION |
|---|---|
type[EnumT]
|
The decorated enum class. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If duplicate values are found among non-deprecated members. |