From b3cc7d868a24ad0d2eb59f6e44dbbcb8ba7f5b5e Mon Sep 17 00:00:00 2001 From: Xtreme Date: Thu, 12 Mar 2026 16:08:16 -0400 Subject: [PATCH] added object field utility in utils/obj_field.py --- src/gmdkit/utils/obj_field.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/gmdkit/utils/obj_field.py diff --git a/src/gmdkit/utils/obj_field.py b/src/gmdkit/utils/obj_field.py new file mode 100644 index 0000000..648c1fe --- /dev/null +++ b/src/gmdkit/utils/obj_field.py @@ -0,0 +1,20 @@ + +from typing import Any, TypeVar, Generic, overload + +T = TypeVar("T") + +class ObjField(Generic[T]): + def __init__(self, key: Any) -> None: + self.key = key + + @overload + def __get__(self, instance: None, owner: type[Any]) -> "ObjField[T]": ... + @overload + def __get__(self, instance: object, owner: type[Any] | None = None) -> T: ... + def __get__(self, instance: object | None, owner: type[Any] | None = None) -> T | "ObjField[T]": + if instance is None: + return self + return instance.obj[self.key] # type: ignore[attr-defined] + + def __set__(self, instance: Any, value: T) -> None: + instance.obj[self.key] = value \ No newline at end of file