forked from massive-com/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelclass.py
More file actions
27 lines (21 loc) · 705 Bytes
/
modelclass.py
File metadata and controls
27 lines (21 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import inspect
import typing
from dataclasses import dataclass
_T = typing.TypeVar("_T")
def modelclass(cls: typing.Type[_T]) -> typing.Type[_T]:
cls = dataclass(cls)
type_hints = typing.get_type_hints(cls)
attributes = [
a
for a in type_hints.keys()
if not a.startswith("__") and not inspect.isroutine(getattr(cls, a, None))
]
def init(self, *args, **kwargs):
for i, a in enumerate(args):
if i < len(attributes):
self.__dict__[attributes[i]] = a
for k, v in kwargs.items():
if k in attributes:
self.__dict__[k] = v
cls.__init__ = init # type: ignore[assignment]
return cls