From 21995c03aa35af3c4b40936d9d832ce68d88b728 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Fri, 18 Oct 2024 09:15:47 +0200 Subject: [PATCH] [fixlib][feat] Allow marking resource classes as not exportable --- fixlib/fixlib/baseresources.py | 2 ++ fixlib/fixlib/core/model_export.py | 4 +++- fixlib/test/core/model_export_test.py | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/fixlib/fixlib/baseresources.py b/fixlib/fixlib/baseresources.py index 411ec1e4fa..997539f502 100644 --- a/fixlib/fixlib/baseresources.py +++ b/fixlib/fixlib/baseresources.py @@ -296,6 +296,8 @@ class BaseResource(ABC): _categories: ClassVar[List[Category]] = [] # Link to the cloud providers product documentation of this resource kind. _docs_url: ClassVar[Optional[str]] = None + # Mark this class as exportable. Use False for internal model classes without instances. + _model_export: ClassVar[bool] = True ################################################################################ # Instance Variables diff --git a/fixlib/fixlib/core/model_export.py b/fixlib/fixlib/core/model_export.py index 53196bca45..3636c32af9 100644 --- a/fixlib/fixlib/core/model_export.py +++ b/fixlib/fixlib/core/model_export.py @@ -69,7 +69,7 @@ def transitive_classes(classes: Set[type], walk_subclasses: bool = True) -> Set[ def check(to_check: type) -> None: clazz = optional_origin(to_check) if clazz in all_classes: - pass + return elif is_dict(clazz): key_type, value_type = dict_types(to_check) check(key_type) @@ -77,6 +77,8 @@ def check(to_check: type) -> None: elif is_collection(clazz): check(type_arg(to_check)) elif attrs.has(clazz): + if getattr(clazz, "_model_export", True) is False: + return resolve_types(clazz) all_classes.add(clazz) for mro_clazz in clazz.mro()[1:]: diff --git a/fixlib/test/core/model_export_test.py b/fixlib/test/core/model_export_test.py index 0dfb8489c5..f628b98ada 100644 --- a/fixlib/test/core/model_export_test.py +++ b/fixlib/test/core/model_export_test.py @@ -70,6 +70,13 @@ class DataClassOther(DataClassBase): something: str +@define(slots=False) +class DataClassNotExported(DataClassBase): + _model_export: ClassVar[bool] = False + kind: ClassVar[str] = "other" + something: str + + def test_collection() -> None: assert is_collection(Optional[List[str]]) is True assert is_collection(List[str]) is True