Skip to content

Commit a6184e3

Browse files
committed
✨ BasicConfModel support class args extra
1 parent 332376b commit a6184e3

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎arclet/entari/config/models/default.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ def __init__(self, **kwargs):
5555
setattr(self, k, v)
5656

5757
def __init_subclass__(cls, **kwargs):
58-
super().__init_subclass__(**kwargs)
5958
dataclass(**({k: v for k, v in (kwargs | {"kw_only": True}).items() if k in _available_dc_attrs}))(cls)
6059
store_field_description(cls, cls.__dataclass_fields__) # type: ignore
60+
if "extra" in kwargs:
61+
extra = kwargs["extra"]
62+
assert extra in ("allow", "forbid", "ignore"), "extra must be one of 'allow', 'forbid', 'ignore'"
63+
setattr(cls, "__entari_conf_extra__", extra)
6164

6265

6366
def _resolve_type(field_type: Any, types_namespace: dict[str, Any]) -> Any:
@@ -212,7 +215,14 @@ def keys(cls, obj: BasicConfModel) -> list[str]:
212215

213216
@classmethod
214217
def schema(cls, t: type[BasicConfModel], ref_root: str = "/") -> dict[str, Any]:
215-
return SchemaGenerator.from_dc(t, ref_root) # type: ignore
218+
schema = SchemaGenerator.from_dc(t, ref_root) # type: ignore
219+
if hasattr(t, "__entari_conf_extra__"):
220+
extra = getattr(t, "__entari_conf_extra__")
221+
if extra == "allow":
222+
schema["additionalProperties"] = True
223+
elif extra == "forbid":
224+
schema["additionalProperties"] = False
225+
return schema
216226

217227

218228
__all__ = ["BasicConfModel", "field"]

0 commit comments

Comments
 (0)