-
Notifications
You must be signed in to change notification settings - Fork 10
/
default.py
34 lines (26 loc) · 1.14 KB
/
default.py
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
28
29
30
31
32
33
34
from typing import Iterable, Type, TypeVar
from automapper import Mapper
T = TypeVar("T")
_IGNORED_FIELDS = ("return", "args", "kwargs")
def __init_method_classifier__(target_cls: Type[T]) -> bool:
"""Default classifier for classes with described fields in `__init__` method"""
return (
hasattr(target_cls, "__init__")
and hasattr(getattr(target_cls, "__init__"), "__annotations__")
and isinstance(
getattr(getattr(target_cls, "__init__"), "__annotations__"), dict
)
and getattr(getattr(target_cls, "__init__"), "__annotations__")
)
def __init_method_spec_func__(target_cls: Type[T]) -> Iterable[str]:
"""Default spec function for classes with described fields in `__init__` method.
If __init__ of the target class accepts `*args` or `**kwargs`
then current spec function won't work properly and another spec_func should be added
"""
return (
field
for field in target_cls.__init__.__annotations__.keys()
if field not in _IGNORED_FIELDS
)
def extend(mapper: Mapper) -> None:
mapper.add_spec(__init_method_classifier__, __init_method_spec_func__)