Skip to content

Commit 5f1deaa

Browse files
hannes-ucscachave11-ucsc
authored andcommitted
Switch to new attrs API, explain why we can't use slots
1 parent afa3442 commit 5f1deaa

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/azul/uuids.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
)
44
import math
55
from typing import (
6+
Any,
67
ClassVar,
78
Self,
89
dataclass_transform,
@@ -11,7 +12,9 @@
1112
UUID,
1213
)
1314

14-
import attr
15+
from attrs import (
16+
frozen,
17+
)
1518

1619
from azul import (
1720
R,
@@ -137,12 +140,28 @@ def change_version(uuid: str, old_version: int, new_version: int) -> str:
137140
return uuid
138141

139142

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)
141146
class UUIDPartitionMeta(type):
142147

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)
146165
cls.root = cls(prefix_length=0, prefix=0)
147166

148167

0 commit comments

Comments
 (0)