From 2e64304177e17e99a07185988546aaf0e2eb0e35 Mon Sep 17 00:00:00 2001 From: Leon Haffmans Date: Tue, 30 Jul 2024 14:03:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9Exclude=20enums=20from=20circular?= =?UTF-8?q?=20reference=20Workaround?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bo4e_generator/imports.py | 4 ++-- src/bo4e_generator/parser.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bo4e_generator/imports.py b/src/bo4e_generator/imports.py index 6cc5ce1..727c0c4 100644 --- a/src/bo4e_generator/imports.py +++ b/src/bo4e_generator/imports.py @@ -18,9 +18,9 @@ def monkey_patch_imports(namespace: dict[str, SchemaMetadata]): Overwrites the behaviour how imports are rendered. They are not going through jinja templates. They Imports class has a __str__ method, which we will overwrite here. """ - namespace = {k: v for k, v in namespace.items() if k not in ("Typ", "Landescode")} + namespace = {k: v for k, v in namespace.items() if v.module_path[0] != "enum"} # "Typ" and "Landescode" must not be wrapped inside the "if TYPE_CHECKING" block because they are used explicitly - # to set default values. + # to set default values. Generally, enums can be excluded since they cannot cause circular reference issues. import_type_checking = Import.from_full_path("typing.TYPE_CHECKING") # pylint: disable=missing-function-docstring diff --git a/src/bo4e_generator/parser.py b/src/bo4e_generator/parser.py index 71f910c..6b39366 100644 --- a/src/bo4e_generator/parser.py +++ b/src/bo4e_generator/parser.py @@ -91,7 +91,7 @@ class DataTypeWithForwardRef(self.data_type): def type_hint(self) -> str: """Return the type hint for the data type.""" type_ = super().type_hint - if self.reference and type_ in namespace: + if self.reference and type_ in namespace and namespace[type_].module_path[0] != "enum": type_ = f'"{type_}"' return type_