|
3 | 3 | ) |
4 | 4 | import math |
5 | 5 | from typing import ( |
| 6 | + Any, |
6 | 7 | ClassVar, |
7 | 8 | Self, |
8 | 9 | dataclass_transform, |
|
11 | 12 | UUID, |
12 | 13 | ) |
13 | 14 |
|
14 | | -import attr |
| 15 | +from attrs import ( |
| 16 | + frozen, |
| 17 | +) |
15 | 18 |
|
16 | 19 | from azul import ( |
17 | 20 | R, |
@@ -137,12 +140,28 @@ def change_version(uuid: str, old_version: int, new_version: int) -> str: |
137 | 140 | return uuid |
138 | 141 |
|
139 | 142 |
|
140 | | -@dataclass_transform(frozen_default=True, kw_only_default=True) |
| 143 | +@dataclass_transform(frozen_default=True, |
| 144 | + kw_only_default=True, |
| 145 | + order_default=True) |
141 | 146 | class UUIDPartitionMeta(type): |
142 | 147 |
|
143 | | - def __init__(cls, *args, **kwargs): |
144 | | - super().__init__(*args, **kwargs) |
145 | | - attr.s(frozen=True, kw_only=True, auto_attribs=True)(cls) |
| 148 | + def __init__(cls, name: str, bases: tuple[type, ...], members: dict[str, Any]): |
| 149 | + super().__init__(name, bases, members) |
| 150 | + |
| 151 | + # We can't use slots=True for two reasons: |
| 152 | + # |
| 153 | + # 1) slots=True causes attrs to duplicate the class (the instance of |
| 154 | + # this metaclass), which then causes this method to be invoked twice, |
| 155 | + # the second time feeding an already decorated class back to attrs. |
| 156 | + # This could be addressed by overriding __new__ instead of __init__. |
| 157 | + # |
| 158 | + # 2) We would like to be able to use @cached_property on methods of |
| 159 | + # instances, and @cached_property does not work with slotted classes. |
| 160 | + # |
| 161 | + # The assert below ensures that attrs does not duplicate the class, and |
| 162 | + # instead only augments it. |
| 163 | + # |
| 164 | + assert cls is frozen(kw_only=True, slots=False, order=True)(cls) |
146 | 165 | cls.root = cls(prefix_length=0, prefix=0) |
147 | 166 |
|
148 | 167 |
|
|
0 commit comments