mirror of
https://gitlab.com/kupfer/kupferbootstrap.git
synced 2025-06-26 18:25:36 -04:00
dataclass: specify _type_hints class variable, filter class variables from type hints.
This commit is contained in:
parent
746e42a4f6
commit
3c315d7899
1 changed files with 4 additions and 2 deletions
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from munch import Munch
|
from munch import Munch
|
||||||
from typing import Optional, Union, Mapping, Any, get_type_hints, get_origin, get_args, Iterable
|
from typing import ClassVar, Optional, Union, Mapping, Any, get_type_hints, get_origin, get_args, Iterable
|
||||||
|
|
||||||
|
|
||||||
def munchclass(*args, init=False, **kwargs):
|
def munchclass(*args, init=False, **kwargs):
|
||||||
|
@ -24,6 +24,8 @@ def resolve_type_hint(hint: type) -> Iterable[type]:
|
||||||
|
|
||||||
class DataClass(Munch):
|
class DataClass(Munch):
|
||||||
|
|
||||||
|
_type_hints: ClassVar[dict[str, Any]]
|
||||||
|
|
||||||
def __init__(self, d: dict = {}, validate: bool = True, **kwargs):
|
def __init__(self, d: dict = {}, validate: bool = True, **kwargs):
|
||||||
self.update(d | kwargs, validate=validate)
|
self.update(d | kwargs, validate=validate)
|
||||||
|
|
||||||
|
@ -91,7 +93,7 @@ class DataClass(Munch):
|
||||||
|
|
||||||
def __init_subclass__(cls):
|
def __init_subclass__(cls):
|
||||||
super().__init_subclass__()
|
super().__init_subclass__()
|
||||||
cls._type_hints = get_type_hints(cls)
|
cls._type_hints = {name: hint for name, hint in get_type_hints(cls).items() if get_origin(hint) is not ClassVar}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{type(self)}{dict.__repr__(self.toDict())}'
|
return f'{type(self)}{dict.__repr__(self.toDict())}'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue