dataclass-extensions 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataclass_extensions/registrable.py +20 -2
- dataclass_extensions/version.py +1 -1
- {dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/METADATA +1 -1
- {dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/RECORD +7 -7
- {dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/WHEEL +0 -0
- {dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/top_level.txt +0 -0
|
@@ -11,6 +11,7 @@ R = TypeVar("R", bound="Registrable")
|
|
|
11
11
|
@dataclass
|
|
12
12
|
class Registrable:
|
|
13
13
|
_registry: ClassVar[dict[str, Type[Registrable]]]
|
|
14
|
+
_default_type: ClassVar[str | None]
|
|
14
15
|
|
|
15
16
|
type: dataclasses.InitVar[str | None] = dataclasses.field(
|
|
16
17
|
default=None, kw_only=True, repr=False
|
|
@@ -27,16 +28,20 @@ class Registrable:
|
|
|
27
28
|
f"Available choices are: {list(cls._registry.keys())}"
|
|
28
29
|
)
|
|
29
30
|
return super().__new__(cls._registry[type])
|
|
31
|
+
elif cls._default_type is not None and not hasattr(cls, "registered_name"):
|
|
32
|
+
return super().__new__(cls._registry[cls._default_type])
|
|
30
33
|
else:
|
|
31
34
|
return super().__new__(cls)
|
|
32
35
|
|
|
33
36
|
def __init_subclass__(cls, **kwargs):
|
|
34
37
|
super().__init_subclass__(**kwargs)
|
|
35
|
-
if not hasattr(cls, "
|
|
38
|
+
if not hasattr(cls, "_registry"):
|
|
36
39
|
cls._registry = {}
|
|
40
|
+
if not hasattr(cls, "_default_type"):
|
|
41
|
+
cls._default_type = None
|
|
37
42
|
|
|
38
43
|
@classmethod
|
|
39
|
-
def register(cls, name: str) -> Callable[[Type[R]], Type[R]]:
|
|
44
|
+
def register(cls, name: str, default: bool = False) -> Callable[[Type[R]], Type[R]]:
|
|
40
45
|
def register_subclass(subclass: Type[R]) -> Type[R]:
|
|
41
46
|
if not issubclass(subclass, cls):
|
|
42
47
|
raise TypeError(
|
|
@@ -46,6 +51,13 @@ class Registrable:
|
|
|
46
51
|
raise TypeError(
|
|
47
52
|
f"class {subclass.__name__} must be a dataclass in order to register it"
|
|
48
53
|
)
|
|
54
|
+
if default:
|
|
55
|
+
if cls._default_type is not None:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
f"A default implementation for {cls.__name__} has already been registered"
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
cls._default_type = name
|
|
49
61
|
|
|
50
62
|
fields = [
|
|
51
63
|
(f.name, f.type, f) for f in dataclasses.fields(subclass) if f.name != "type" # type: ignore
|
|
@@ -94,3 +106,9 @@ class Registrable:
|
|
|
94
106
|
@classmethod
|
|
95
107
|
def get_registered_names(cls) -> list[str]:
|
|
96
108
|
return list(cls._registry.keys())
|
|
109
|
+
|
|
110
|
+
@classmethod
|
|
111
|
+
def get_default(cls: Type[R]) -> Type[R]:
|
|
112
|
+
if cls._default_type is None:
|
|
113
|
+
raise ValueError(f"A default implementation of {cls.__name__} has not been registered")
|
|
114
|
+
return cls.get_registered_class(cls._default_type)
|
dataclass_extensions/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
VERSION = "0.
|
|
1
|
+
VERSION = "0.2.0"
|
|
@@ -2,12 +2,12 @@ dataclass_extensions/__init__.py,sha256=jpgRh5jjj9jM1tkhTVvB-aDThI2v4tAlq_WpR0-N
|
|
|
2
2
|
dataclass_extensions/decode.py,sha256=dstNb-RU8i3jGoTlo165UlSbyG3MhAgbc-O54mdlxqo,6329
|
|
3
3
|
dataclass_extensions/encode.py,sha256=rctJHAwlSnjOw0rTGbHRWigb2BT4DI3FYGiDzAjDmEQ,3233
|
|
4
4
|
dataclass_extensions/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
-
dataclass_extensions/registrable.py,sha256=
|
|
5
|
+
dataclass_extensions/registrable.py,sha256=6lpMHnOypZ_CPgTwiYglt0egpmhsFaOUmKC-LOBoR_Y,4425
|
|
6
6
|
dataclass_extensions/types.py,sha256=UwSV0MKjFDaK4dlgm72vJHHr6-8QNe8jMhbTZGOMIJM,247
|
|
7
7
|
dataclass_extensions/utils.py,sha256=8cigzY9JDEWexWXlcMxXyf27S50-1ZPMNmn4zUSrc6k,935
|
|
8
|
-
dataclass_extensions/version.py,sha256=
|
|
9
|
-
dataclass_extensions-0.
|
|
10
|
-
dataclass_extensions-0.
|
|
11
|
-
dataclass_extensions-0.
|
|
12
|
-
dataclass_extensions-0.
|
|
13
|
-
dataclass_extensions-0.
|
|
8
|
+
dataclass_extensions/version.py,sha256=w9Iw7QVvd8lme2wKwEbCo5IgetVjSfFBOOYAcA_Q0Ns,18
|
|
9
|
+
dataclass_extensions-0.2.0.dist-info/licenses/LICENSE,sha256=YvuKOpYh3COIF0yqq-nCMXtpS7mh1GyYvPVlW2j1G-M,11359
|
|
10
|
+
dataclass_extensions-0.2.0.dist-info/METADATA,sha256=QXmnDLRIZD1WwOtOGOVFBv0uj1AXxDcHu6McG_-pvz4,15215
|
|
11
|
+
dataclass_extensions-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
12
|
+
dataclass_extensions-0.2.0.dist-info/top_level.txt,sha256=njfdeqLtTThNGwqnoKu4JV8J1287xGAeEwyESpoU-k8,21
|
|
13
|
+
dataclass_extensions-0.2.0.dist-info/RECORD,,
|
|
File without changes
|
{dataclass_extensions-0.1.0.dist-info → dataclass_extensions-0.2.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|