|
1 | 1 | """Base class for all enumerations."""
|
2 | 2 | from deprecation import deprecated
|
3 | 3 | from enum import Enum
|
4 |
| -from typing import Optional |
| 4 | +from typing import Optional, Type, Callable |
| 5 | +from warnings import warn |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class BaseEnumeration(str, Enum):
|
@@ -56,10 +57,6 @@ def from_str(cls, val: str, *, exception: bool = False) -> Optional["BaseEnumera
|
56 | 57 | BaseEnumeration
|
57 | 58 | The matching enumerated element, or None
|
58 | 59 |
|
59 |
| - :param val: |
60 |
| - :param exception: |
61 |
| - :return: |
62 |
| -
|
63 | 60 | """
|
64 | 61 | if val is None:
|
65 | 62 | result = None
|
@@ -102,3 +99,69 @@ def get_enum(cls, name: str) -> "BaseEnumeration":
|
102 | 99 | def __str__(self):
|
103 | 100 | """Return the value of the enumeration object."""
|
104 | 101 | return self.value
|
| 102 | + |
| 103 | + @classmethod |
| 104 | + def _missing_(cls, value: object) -> Optional["BaseEnumeration"]: |
| 105 | + """Allow Class(value) to resolve synonyms.""" |
| 106 | + if isinstance(value, str): |
| 107 | + return cls.from_str(value) |
| 108 | + else: |
| 109 | + return None |
| 110 | + |
| 111 | + |
| 112 | +def migrated_enum(*, |
| 113 | + old_value: str, |
| 114 | + new_value: str, |
| 115 | + deprecated_in: str, |
| 116 | + removed_in: str) -> Callable[[Type], Type]: |
| 117 | + """ |
| 118 | + Decorator for registering an enumerated value as migrated to a new symbol. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + old_value: str |
| 123 | + A string containing the old symbol name. Used for display only. |
| 124 | + new_value: str |
| 125 | + A string containing the new symbol name or the enumeration value. Used |
| 126 | + to resolve the target value. |
| 127 | + deprecated_in: str |
| 128 | + The version of the library the enumerated value was migrated. |
| 129 | + removed_in: str |
| 130 | + The version of the library the old enumerated value will be removed in. |
| 131 | +
|
| 132 | + """ |
| 133 | + def decorator(cls) -> Type: |
| 134 | + print("Sear") |
| 135 | + |
| 136 | + class MixinMeta(type(cls)): |
| 137 | + """New derived metaclass for holding the deprecated symbol.""" |
| 138 | + |
| 139 | + def __getitem__(cls, name): |
| 140 | + if name == old_value: |
| 141 | + warn( |
| 142 | + f"{old_value} is deprecated as of {deprecated_in} " |
| 143 | + f"and will be removed in {removed_in}. " |
| 144 | + f"{old_value} has been renamed to {cls(new_value).name}.", |
| 145 | + DeprecationWarning |
| 146 | + ) |
| 147 | + return cls(new_value) |
| 148 | + else: |
| 149 | + return super().__getitem__(name) |
| 150 | + |
| 151 | + def accessor(self): |
| 152 | + """Subroutine that returns the new enumerated value.""" |
| 153 | + return cls(new_value) |
| 154 | + |
| 155 | + accessor.__name__ = old_value # So deprecated knows the correct target name |
| 156 | + deprecator = deprecated(deprecated_in=deprecated_in, |
| 157 | + removed_in=removed_in, |
| 158 | + details=f"{old_value} has been renamed to {cls(new_value).name}.", |
| 159 | + ) |
| 160 | + |
| 161 | + # Add the property to the metaclass, and then update cls' meta |
| 162 | + setattr(MixinMeta, old_value, property(deprecator(accessor))) |
| 163 | + cls.__class__ = MixinMeta |
| 164 | + |
| 165 | + return cls |
| 166 | + |
| 167 | + return decorator |
0 commit comments