You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The InvokeAI import graph is a tangled web ripe with circular imports and import-order dependencies. We currently work around these issues with short-term hacks, but they are a hindrance to development. The good news is that these issues are generally easy to avoid by following some simple patterns.
Here is a description of the direction that I think we should be moving to solve these problems (discussed offline with several members of the Invoke team):
Cross-directory imports should be one-directional. For example, files in invokeai/app/invocations are expected to import from invokeai.backend, but files in invokeai/backend should not import from invokeai.app.invocations.
Avoid re-exporting symbols from __init__.py files unnecessarily. This pattern makes sense for building clean public APIs, but most of our codebase is non-public (with the exception of invokeai.invocation_api). Also, re-exporting from __init__.py tends to lead to eager importing of modules before they are needed, which exacerbates circular import issues if the import tree is poorly structured, and could be contributing to the slow app launch times.
We have a bad habit of doing stuff on import. During import, we should only be defining classes/functions/constants - not executing code. This anti-pattern has led to several import-order dependencies.
Use absolute imports rather than relative imports. This is primarily a style preference, but PEP 8 recommends absolute imports, and in my experience they work better with Python LSP services (e.g. for auto-import and refactoring features). We currently have a mix of absolute and relative imports (mostly absolute) - we should standardize this. Also, if we enforce absolute imports with a linter, this makes refactoring easier. With absolute imports, if I move invokeai.app.service.some.module, then I can confidently search and replace all imports of this module. With relative imports, it is harder to find all references to the moved module.
Next Steps
This is intended to be a long-lived issue as we gradually work towards the proposed goal state over time. We won't try to solve all of these problem in a single pass.
The text was updated successfully, but these errors were encountered:
## Summary
This PR migrates all relative imports to absolute imports, and adds a
ruff check to enforce this going forward.
The justification for this change is here:
#6575
## QA Instructions
Smoke test all common workflows. Most of the relative -> absolute
conversions could be completed automatically, so the risk is relatively
low.
## Merge Plan
As with any far-reaching change like this, it is likely to cause some
merge conflicts with some in-flight branches. Unfortunately, there's no
way around this, but let me know if you can think of in-flight work that
will be significantly disrupted by this.
## Checklist
- [x] _The PR has a short but descriptive title, suitable for a
changelog_
- [x] _Tests added / updated (if applicable)_ N/A
- [x] _Documentation added / updated (if applicable)_ N/A
Problem
The InvokeAI import graph is a tangled web ripe with circular imports and import-order dependencies. We currently work around these issues with short-term hacks, but they are a hindrance to development. The good news is that these issues are generally easy to avoid by following some simple patterns.
Examples of workarounds for import issues:
InvokeAI/invokeai/app/services/config/config_default.py
Line 159 in bb6ff4c
InvokeAI/invokeai/backend/ip_adapter/ip_adapter.py
Lines 140 to 141 in bb6ff4c
InvokeAI/invokeai/backend/raw_model.py
Lines 3 to 7 in bb6ff4c
Proposal
Here is a description of the direction that I think we should be moving to solve these problems (discussed offline with several members of the Invoke team):
invokeai/app/invocations
are expected to import frominvokeai.backend
, but files ininvokeai/backend
should not import frominvokeai.app.invocations
.__init__.py
files unnecessarily. This pattern makes sense for building clean public APIs, but most of our codebase is non-public (with the exception ofinvokeai.invocation_api
). Also, re-exporting from__init__.py
tends to lead to eager importing of modules before they are needed, which exacerbates circular import issues if the import tree is poorly structured, and could be contributing to the slow app launch times.invokeai.app.service.some.module
, then I can confidently search and replace all imports of this module. With relative imports, it is harder to find all references to the moved module.Next Steps
This is intended to be a long-lived issue as we gradually work towards the proposed goal state over time. We won't try to solve all of these problem in a single pass.
The text was updated successfully, but these errors were encountered: