Repository navigation
Fix circular import when lionweb.language is imported first - #11
Merged
Merged
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
The autoresolve constants now live only in lionweb.autoresolve. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In a fresh interpreter, these imports fail with
ImportError: cannot import name 'Annotation' from partially initialized module 'lionweb.language.annotation':import lionweb.languageimport lionweb.serializationimport lionweb.self.lioncoreimport lionweb.generationThis affects the released 0.4.3 and
main. It only works iflionweb.modelhappens to be imported first. The cycle is:lionweb.language→language.language→lionweb.model→model.classifier_instance_utils→lionweb.utils.autoresolve(which runs thelionweb.utilspackage__init__) →utils.language_validator→lionweb.language.annotation(still initializing)Fix
lionweb.modelonly needed two string constants fromlionweb.utils.autoresolve, but importing them ran the wholelionweb.utilspackage. The constants now live in a new top-level module,lionweb.autoresolve, whichlionweb.modelandlionweb.serializationimport directly.Tests
tests/test_imports.py: imports each package in a fresh subprocess to verify there are no circular imports. It fails for the 4 packages above without the fix and passes with it.