mirror of
https://github.com/django-components/django-components.git
synced 2025-09-10 16:06:18 +00:00
refactor: change component typing from generics to class attributes (#1138)
This commit is contained in:
parent
912d8e8074
commit
b49002b545
25 changed files with 2451 additions and 610 deletions
|
@ -1,5 +1,6 @@
|
|||
import re
|
||||
import sys
|
||||
from dataclasses import asdict, is_dataclass
|
||||
from hashlib import md5
|
||||
from importlib import import_module
|
||||
from itertools import chain
|
||||
|
@ -130,3 +131,19 @@ def is_glob(filepath: str) -> bool:
|
|||
|
||||
def flatten(lst: Iterable[Iterable[T]]) -> List[T]:
|
||||
return list(chain.from_iterable(lst))
|
||||
|
||||
|
||||
def to_dict(data: Any) -> dict:
|
||||
"""
|
||||
Convert object to a dict.
|
||||
|
||||
Handles `dict`, `NamedTuple`, and `dataclass`.
|
||||
"""
|
||||
if isinstance(data, dict):
|
||||
return data
|
||||
elif hasattr(data, "_asdict"): # Case: NamedTuple
|
||||
return data._asdict()
|
||||
elif is_dataclass(data): # Case: dataclass
|
||||
return asdict(data) # type: ignore[arg-type]
|
||||
|
||||
return dict(data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue