Skip to content

Commit 0a5445c

Browse files
committed
[feat] Strict field type requirements for subclasses of GroupBase
1 parent 47f6bce commit 0a5445c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/oqd_dataschema/base.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# %%
1616
import warnings
17-
from typing import Annotated, Any, Literal, Optional, Union
17+
from typing import Annotated, Any, ClassVar, Literal, Optional, Union
1818

1919
import numpy as np
2020
from bidict import bidict
@@ -86,6 +86,19 @@ class GroupBase(BaseModel, extra="forbid"):
8686

8787
def __init_subclass__(cls, **kwargs):
8888
super().__init_subclass__(**kwargs)
89+
90+
for k, v in cls.__annotations__.items():
91+
if k == "class_":
92+
raise AttributeError("`class_` attribute should not be set manually.")
93+
94+
if k == "attrs" and k is not Attrs:
95+
raise TypeError("`attrs` should be of type `Attrs`")
96+
97+
if k not in ["class_", "attrs"] and v not in [Dataset, ClassVar]:
98+
raise TypeError(
99+
"All fields of `GroupBase` have to be of type `Dataset`."
100+
)
101+
89102
cls.__annotations__["class_"] = Literal[cls.__name__]
90103
setattr(cls, "class_", cls.__name__)
91104

0 commit comments

Comments
 (0)