refactor: change component typing from generics to class attributes (#1138)

This commit is contained in:
Juro Oravec 2025-04-20 22:05:29 +02:00 committed by GitHub
parent 912d8e8074
commit b49002b545
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 2451 additions and 610 deletions

View file

@ -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)