From a61de1b1dc93a9cbcd138f439355a2328cb432f5 Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Tue, 9 Jul 2024 09:32:29 +0200 Subject: [PATCH] Improve dataclass instance check (#507) --- kpops/components/base_components/base_defaults_component.py | 5 +++-- kpops/utils/dataclasses.py | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 kpops/utils/dataclasses.py diff --git a/kpops/components/base_components/base_defaults_component.py b/kpops/components/base_components/base_defaults_component.py index f63eaf5c2..3ad8ac894 100644 --- a/kpops/components/base_components/base_defaults_component.py +++ b/kpops/components/base_components/base_defaults_component.py @@ -4,7 +4,7 @@ import logging from abc import ABC from collections.abc import Hashable, Sequence -from dataclasses import asdict, is_dataclass +from dataclasses import asdict from functools import cached_property from pathlib import Path from typing import Any, TypeVar @@ -23,6 +23,7 @@ from kpops.component_handlers import ComponentHandlers from kpops.config import KpopsConfig from kpops.utils import cached_classproperty +from kpops.utils.dataclasses import is_dataclass_instance from kpops.utils.dict_ops import ( generate_substitution, update_nested, @@ -181,7 +182,7 @@ def extend_with_defaults(cls, config: KpopsConfig, **kwargs: Any) -> dict[str, A for k, v in kwargs.items(): if isinstance(v, pydantic.BaseModel): kwargs[k] = v.model_dump(exclude_unset=True) - elif is_dataclass(v): + elif is_dataclass_instance(v): kwargs[k] = asdict(v) defaults_file_paths_ = get_defaults_file_paths( diff --git a/kpops/utils/dataclasses.py b/kpops/utils/dataclasses.py new file mode 100644 index 000000000..16db70d94 --- /dev/null +++ b/kpops/utils/dataclasses.py @@ -0,0 +1,5 @@ +from dataclasses import is_dataclass + + +def is_dataclass_instance(obj: object) -> bool: + return is_dataclass(obj) and not isinstance(obj, type)