From 01538e2b8828222fcc3468916dc43a22646fd9ea Mon Sep 17 00:00:00 2001 From: ftomassetti Date: Sat, 26 Sep 2026 19:35:29 +0200 Subject: [PATCH 1/3] Fix circular import when lionweb.language is imported first lionweb.utils eagerly imported language_validator, which imports lionweb.language; lionweb.language reaches lionweb.utils again through lionweb.model, so importing lionweb.language (or serialization, self.lioncore, generation) first in a fresh process failed. lionweb.utils now exposes LanguageValidator and InvalidLanguageError lazily through a module-level __getattr__, keeping the public API unchanged. A regression test imports each package in a fresh interpreter. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 +++++++ src/lionweb/utils/__init__.py | 17 +++++++++++- tests/test_imports.py | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/test_imports.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 6286100..aa6d05a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. +## Unreleased + +### Fixed + +- `import lionweb.language` (and `lionweb.serialization`, `lionweb.self.lioncore`, + `lionweb.generation`) no longer fails with a circular import when it is the + first `lionweb` import in the process. `lionweb.utils` now loads + `LanguageValidator` and `InvalidLanguageError` lazily. + ## 0.4.3 ### Changed diff --git a/src/lionweb/utils/__init__.py b/src/lionweb/utils/__init__.py index e7469d1..8b70183 100644 --- a/src/lionweb/utils/__init__.py +++ b/src/lionweb/utils/__init__.py @@ -1,9 +1,13 @@ +from typing import TYPE_CHECKING, Any + from .id_utils import clean_string_as_id, is_valid_id from .issue import Issue from .issue_severity import IssueSeverity -from .language_validator import InvalidLanguageError, LanguageValidator from .node_navigation import root +if TYPE_CHECKING: + from .language_validator import InvalidLanguageError, LanguageValidator + __all__ = [ "is_valid_id", "clean_string_as_id", @@ -13,3 +17,14 @@ "LanguageValidator", "root", ] + + +def __getattr__(name: str) -> Any: + # language_validator depends on lionweb.language, which in turn (through + # lionweb.model) imports this package: loading it eagerly makes + # `import lionweb.language` fail with a circular import. + if name in ("InvalidLanguageError", "LanguageValidator"): + from . import language_validator + + return getattr(language_validator, name) + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/tests/test_imports.py b/tests/test_imports.py new file mode 100644 index 0000000..74bee91 --- /dev/null +++ b/tests/test_imports.py @@ -0,0 +1,49 @@ +import os +import subprocess +import sys +import unittest + +SRC_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src") + +MODULES = [ + "lionweb", + "lionweb.api", + "lionweb.generation", + "lionweb.language", + "lionweb.lionweb_version", + "lionweb.model", + "lionweb.self.lioncore", + "lionweb.serialization", + "lionweb.utils", +] + + +class ImportsTest(unittest.TestCase): + """Each package must be importable first, in a fresh interpreter.""" + + def test_each_package_can_be_imported_first(self): + env = dict(os.environ) + env["PYTHONPATH"] = os.pathsep.join(filter(None, [SRC_DIR, env.get("PYTHONPATH")])) + for module in MODULES: + with self.subTest(module=module): + result = subprocess.run( + [sys.executable, "-c", f"import {module}"], + env=env, + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + + def test_lazy_utils_exports(self): + from lionweb.utils import InvalidLanguageError, LanguageValidator + from lionweb.utils.language_validator import ( + InvalidLanguageError as DirectInvalidLanguageError, + ) + from lionweb.utils.language_validator import LanguageValidator as DirectLanguageValidator + + self.assertIs(LanguageValidator, DirectLanguageValidator) + self.assertIs(InvalidLanguageError, DirectInvalidLanguageError) + + +if __name__ == "__main__": + unittest.main() From ee7b95778a189a3353d9700c0d44e6d6bfe24bd1 Mon Sep 17 00:00:00 2001 From: ftomassetti Date: Sat, 26 Sep 2026 20:33:43 +0200 Subject: [PATCH 2/3] Break the import cycle by moving the autoresolve constants Instead of loading LanguageValidator lazily in lionweb.utils, move the autoresolve prefix constants to lionweb.autoresolve: lionweb.model used them through lionweb.utils.autoresolve, which ran the whole lionweb.utils package __init__ and closed the cycle back to lionweb.language. lionweb.utils.autoresolve keeps re-exporting them. The changelog entry now targets 0.4.4. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 ++++--- src/lionweb/autoresolve.py | 4 ++++ .../model/classifier_instance_utils.py | 2 +- src/lionweb/serialization/node_populator.py | 8 ++++---- src/lionweb/utils/__init__.py | 17 +---------------- src/lionweb/utils/autoresolve.py | 10 +++++++--- tests/test_imports.py | 19 ++++++++----------- 7 files changed, 29 insertions(+), 38 deletions(-) create mode 100644 src/lionweb/autoresolve.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aa6d05a..4362088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,15 @@ All notable changes to this project will be documented in this file. -## Unreleased +## 0.4.4 ### Fixed - `import lionweb.language` (and `lionweb.serialization`, `lionweb.self.lioncore`, `lionweb.generation`) no longer fails with a circular import when it is the - first `lionweb` import in the process. `lionweb.utils` now loads - `LanguageValidator` and `InvalidLanguageError` lazily. + first `lionweb` import in the process. The autoresolve prefix constants moved + to `lionweb.autoresolve`, so `lionweb.model` no longer imports the + `lionweb.utils` package; `lionweb.utils.autoresolve` still re-exports them. ## 0.4.3 diff --git a/src/lionweb/autoresolve.py b/src/lionweb/autoresolve.py new file mode 100644 index 0000000..6edaa16 --- /dev/null +++ b/src/lionweb/autoresolve.py @@ -0,0 +1,4 @@ +from typing import Final + +LIONCORE_AUTORESOLVE_PREFIX: Final = "LionWeb.LionCore_M3." +LIONCOREBUILTINS_AUTORESOLVE_PREFIX: Final = "LionWeb.LionCore_builtins." diff --git a/src/lionweb/model/classifier_instance_utils.py b/src/lionweb/model/classifier_instance_utils.py index 45b4d94..a0496e8 100644 --- a/src/lionweb/model/classifier_instance_utils.py +++ b/src/lionweb/model/classifier_instance_utils.py @@ -1,6 +1,6 @@ from typing import TYPE_CHECKING, Optional -from lionweb.utils.autoresolve import ( +from lionweb.autoresolve import ( LIONCORE_AUTORESOLVE_PREFIX, LIONCOREBUILTINS_AUTORESOLVE_PREFIX, ) diff --git a/src/lionweb/serialization/node_populator.py b/src/lionweb/serialization/node_populator.py index 6247409..818d58c 100644 --- a/src/lionweb/serialization/node_populator.py +++ b/src/lionweb/serialization/node_populator.py @@ -1,6 +1,10 @@ from typing import TYPE_CHECKING, cast from lionweb.api.classifier_instance_resolver import ClassifierInstanceResolver +from lionweb.autoresolve import ( + LIONCORE_AUTORESOLVE_PREFIX, + LIONCOREBUILTINS_AUTORESOLVE_PREFIX, +) from lionweb.language.lioncore_builtins import LionCoreBuiltins from lionweb.lionweb_version import LionWebVersion from lionweb.model import ClassifierInstance, Node @@ -10,10 +14,6 @@ from lionweb.serialization.deserialization_exception import DeserializationException from lionweb.serialization.deserialization_status import DeserializationStatus from lionweb.serialization.unavailable_node_policy import UnavailableNodePolicy -from lionweb.utils.autoresolve import ( - LIONCORE_AUTORESOLVE_PREFIX, - LIONCOREBUILTINS_AUTORESOLVE_PREFIX, -) class NodePopulator: diff --git a/src/lionweb/utils/__init__.py b/src/lionweb/utils/__init__.py index 8b70183..e7469d1 100644 --- a/src/lionweb/utils/__init__.py +++ b/src/lionweb/utils/__init__.py @@ -1,13 +1,9 @@ -from typing import TYPE_CHECKING, Any - from .id_utils import clean_string_as_id, is_valid_id from .issue import Issue from .issue_severity import IssueSeverity +from .language_validator import InvalidLanguageError, LanguageValidator from .node_navigation import root -if TYPE_CHECKING: - from .language_validator import InvalidLanguageError, LanguageValidator - __all__ = [ "is_valid_id", "clean_string_as_id", @@ -17,14 +13,3 @@ "LanguageValidator", "root", ] - - -def __getattr__(name: str) -> Any: - # language_validator depends on lionweb.language, which in turn (through - # lionweb.model) imports this package: loading it eagerly makes - # `import lionweb.language` fail with a circular import. - if name in ("InvalidLanguageError", "LanguageValidator"): - from . import language_validator - - return getattr(language_validator, name) - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/src/lionweb/utils/autoresolve.py b/src/lionweb/utils/autoresolve.py index 6edaa16..9f6c6cf 100644 --- a/src/lionweb/utils/autoresolve.py +++ b/src/lionweb/utils/autoresolve.py @@ -1,4 +1,8 @@ -from typing import Final +# Kept for backward compatibility: the constants live in lionweb.autoresolve so +# that lionweb.model can use them without importing the lionweb.utils package. +from lionweb.autoresolve import ( + LIONCORE_AUTORESOLVE_PREFIX, + LIONCOREBUILTINS_AUTORESOLVE_PREFIX, +) -LIONCORE_AUTORESOLVE_PREFIX: Final = "LionWeb.LionCore_M3." -LIONCOREBUILTINS_AUTORESOLVE_PREFIX: Final = "LionWeb.LionCore_builtins." +__all__ = ["LIONCORE_AUTORESOLVE_PREFIX", "LIONCOREBUILTINS_AUTORESOLVE_PREFIX"] diff --git a/tests/test_imports.py b/tests/test_imports.py index 74bee91..6a8806e 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -8,6 +8,7 @@ MODULES = [ "lionweb", "lionweb.api", + "lionweb.autoresolve", "lionweb.generation", "lionweb.language", "lionweb.lionweb_version", @@ -15,11 +16,17 @@ "lionweb.self.lioncore", "lionweb.serialization", "lionweb.utils", + "lionweb.utils.autoresolve", ] class ImportsTest(unittest.TestCase): - """Each package must be importable first, in a fresh interpreter.""" + """Verifies there are no circular imports between the lionweb packages. + + Each package is imported first, in a fresh interpreter: an import cycle can + go unnoticed in the rest of the suite, where modules are already loaded in + an order that happens to work. + """ def test_each_package_can_be_imported_first(self): env = dict(os.environ) @@ -34,16 +41,6 @@ def test_each_package_can_be_imported_first(self): ) self.assertEqual(result.returncode, 0, result.stderr) - def test_lazy_utils_exports(self): - from lionweb.utils import InvalidLanguageError, LanguageValidator - from lionweb.utils.language_validator import ( - InvalidLanguageError as DirectInvalidLanguageError, - ) - from lionweb.utils.language_validator import LanguageValidator as DirectLanguageValidator - - self.assertIs(LanguageValidator, DirectLanguageValidator) - self.assertIs(InvalidLanguageError, DirectInvalidLanguageError) - if __name__ == "__main__": unittest.main() From 4edfc2d473711f242e35260f7ad218ead333fe8c Mon Sep 17 00:00:00 2001 From: ftomassetti Date: Sat, 26 Sep 2026 20:34:59 +0200 Subject: [PATCH 3/3] Remove the lionweb.utils.autoresolve re-export The autoresolve constants now live only in lionweb.autoresolve. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- src/lionweb/utils/autoresolve.py | 8 -------- tests/test_imports.py | 1 - 3 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 src/lionweb/utils/autoresolve.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4362088..8231eb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file. `lionweb.generation`) no longer fails with a circular import when it is the first `lionweb` import in the process. The autoresolve prefix constants moved to `lionweb.autoresolve`, so `lionweb.model` no longer imports the - `lionweb.utils` package; `lionweb.utils.autoresolve` still re-exports them. + `lionweb.utils` package. `lionweb.utils.autoresolve` has been removed. ## 0.4.3 diff --git a/src/lionweb/utils/autoresolve.py b/src/lionweb/utils/autoresolve.py deleted file mode 100644 index 9f6c6cf..0000000 --- a/src/lionweb/utils/autoresolve.py +++ /dev/null @@ -1,8 +0,0 @@ -# Kept for backward compatibility: the constants live in lionweb.autoresolve so -# that lionweb.model can use them without importing the lionweb.utils package. -from lionweb.autoresolve import ( - LIONCORE_AUTORESOLVE_PREFIX, - LIONCOREBUILTINS_AUTORESOLVE_PREFIX, -) - -__all__ = ["LIONCORE_AUTORESOLVE_PREFIX", "LIONCOREBUILTINS_AUTORESOLVE_PREFIX"] diff --git a/tests/test_imports.py b/tests/test_imports.py index 6a8806e..b3519e0 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -16,7 +16,6 @@ "lionweb.self.lioncore", "lionweb.serialization", "lionweb.utils", - "lionweb.utils.autoresolve", ]