Skip to content

Fix circular import when lionweb.language is imported first - #11

Merged
ftomassetti merged 3 commits into
mainfrom
fix/circular-import-lionweb-language
Sep 26, 2026
Merged

ftomassetti merged 3 commits into
mainfrom
fix/circular-import-lionweb-language

Conversation

@ftomassetti

@ftomassetti ftomassetti commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In a fresh interpreter, these imports fail with ImportError: cannot import name 'Annotation' from partially initialized module 'lionweb.language.annotation':

  • import lionweb.language
  • import lionweb.serialization
  • import lionweb.self.lioncore
  • import lionweb.generation

This affects the released 0.4.3 and main. It only works if lionweb.model happens to be imported first. The cycle is:

lionweb.language → language.language → lionweb.model → model.classifier_instance_utils → lionweb.utils.autoresolve (which runs the lionweb.utils package __init__) → utils.language_validator → lionweb.language.annotation (still initializing)

Fix

lionweb.model only needed two string constants from lionweb.utils.autoresolve, but importing them ran the whole lionweb.utils package. The constants now live in a new top-level module, lionweb.autoresolve, which lionweb.model and lionweb.serialization import directly.

Tests

  • New 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.

ftomassetti and others added 3 commits September 26, 2026 19:35
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>
@ftomassetti
ftomassetti merged commit aa4a234 into main Sep 26, 2026
5 checks passed
@ftomassetti
ftomassetti deleted the fix/circular-import-lionweb-language branch September 26, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant