openlibhums / openlibhums/janeway
Refactor imports around utils and logic
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 238
- Forks
- 97
- Avg merge
- 9d 1h
- Merged PRs (30d)
- 8
Description
Is your feature request related to a problem? Please describe.
During development I encountered a particularly long import chain that ended in a circular ImportError:
File "/home/joe/git/janeway/src/cms/models.py", line 15, in <module>
from core.model_utils import AbstractBleachModelMixin
File "/home/joe/git/janeway/src/core/model_utils.py", line 49, in <module>
from utils import logic
File "/home/joe/git/janeway/src/utils/logic.py", line 11, in <module>
from core.middleware import GlobalRequestMiddleware
File "/home/joe/git/janeway/src/core/middleware.py", line 19, in <module>
from press import models as press_models
File "/home/joe/git/janeway/src/press/models.py", line 15, in <module>
from core import models as core_models
File "/home/joe/git/janeway/src/core/models.py", line 36, in <module>
from core import files, validators
File "/home/joe/git/janeway/src/core/files.py", line 32, in <module>
from utils import models as util_models
File "/home/joe/git/janeway/src/utils/models.py", line 20, in <module>
from utils.importers.up import get_input_value_by_name
File "/home/joe/git/janeway/src/utils/importers/up.py", line 17, in <module>
from utils.importers import shared
File "/home/joe/git/janeway/src/utils/importers/shared.py", line 17, in <module>
from journal import models as journal_models
File "/home/joe/git/janeway/src/journal/models.py", line 26, in <module>
from core import (
File "/home/joe/git/janeway/src/core/workflow.py", line 12, in <module>
from review.logic import assign_editor
File "/home/joe/git/janeway/src/review/logic.py", line 32, in <module>
from events import logic as event_logic
File "/home/joe/git/janeway/src/events/logic.py", line 7, in <module>
from submission import models as submission_models
File "/home/joe/git/janeway/src/submission/models.py", line 37, in <module>
from core.model_utils import(
ImportError: cannot import name 'AbstractLastModifiedModel' from partially initialized module 'core.model_utils' (most likely due to a circular import) (/home/joe/git/janeway/src/core/model_utils.py)
Describe the solution you'd like
We need to refactor the imports on this chain a bit to make it less likely that we will encounter this error when using model_utils or logic inside models files.
Additional context
This was fixed temporarily by moving an import into a method here:
diff --git a/src/core/model_utils.py b/src/core/model_utils.py
index 2f94757d..89753653 100644
--- a/src/core/model_utils.py
+++ b/src/core/model_utils.py
@@ -46,7 +46,6 @@ from modeltranslation.utils import auto_populate
from PIL import Image
import xml.etree.cElementTree as et
-from utils import logic
from utils.logger import get_logger
logger = get_logger(__name__)
@@ -96,6 +95,8 @@ class AbstractSiteModel(models.Model):
return obj
def site_url(self, path=None):
+ # This import is here to avoid circular imports
+ from utils import logic
return logic.build_url(
netloc=self.domain,
scheme=self._get_scheme(),
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the import chain through src/core/model_utils.py, src/utils/logic.py, src/core/middleware.py, src/press/models.py, src/core/models.py, src/core/files.py, src/utils/models.py, src/utils/importers/up.py, src/utils/importers/shared.py, src/journal/models.py, src/core/workflow.py, src/review/logic.py, src/events/logic.py, and src/submission/models.py. Compare it with the temporary method-local import in AbstractSiteModel.site_url; done means the circular ImportError no longer occurs when model_utils or logic is used from model files.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100