dataclass: specify _type_hints class variable, filter class variables from type hints.

This commit is contained in:
InsanePrawn 2022-09-27 06:28:53 +02:00
parent 746e42a4f6
commit 3c315d7899

View file

@ -2,7 +2,7 @@ from __future__ import annotations
from dataclasses import dataclass
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):
@ -24,6 +24,8 @@ def resolve_type_hint(hint: type) -> Iterable[type]:
class DataClass(Munch):
_type_hints: ClassVar[dict[str, Any]]
def __init__(self, d: dict = {}, validate: bool = True, **kwargs):
self.update(d | kwargs, validate=validate)
@ -91,7 +93,7 @@ class DataClass(Munch):
def __init_subclass__(cls):
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):
return f'{type(self)}{dict.__repr__(self.toDict())}'