extract dataclass-based config helper
This commit is contained in:
parent
217116dfe9
commit
8cbbafae1d
2 changed files with 29 additions and 19 deletions
18
my/core/cfg.py
Normal file
18
my/core/cfg.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from typing import TypeVar, Type, Callable, Dict, Any
|
||||
|
||||
Attrs = Dict[str, Any]
|
||||
|
||||
C = TypeVar('C')
|
||||
|
||||
# todo not sure about it, could be overthinking...
|
||||
# but short enough to change later
|
||||
def make_config(cls: Type[C], migration: Callable[[Attrs], Attrs]=lambda x: x) -> C:
|
||||
props = dict(vars(cls.__base__))
|
||||
props = migration(props)
|
||||
from dataclasses import fields
|
||||
params = {
|
||||
k: v
|
||||
for k, v in props.items()
|
||||
if k in {f.name for f in fields(cls)}
|
||||
}
|
||||
return cls(**params) # type: ignore[call-arg]
|
Loading…
Add table
Add a link
Reference in a new issue