pycytominer icon indicating copy to clipboard operation
pycytominer copied to clipboard

Bug: Sorting imports reveals circular dependency

Open kenibrewer opened this issue 6 months ago • 0 comments

Example code with output

After turrning on isort (in this branch), I received the following error:

(.venv) @kenibrewer ➜ /workspaces/pycytominer (fix/circular-imports) $ pytest
ImportError while loading conftest '/workspaces/pycytominer/tests/test_cyto_utils/conftest.py'.
tests/test_cyto_utils/conftest.py:12: in <module>
    from pycytominer.cyto_utils.cell_locations import CellLocation
pycytominer/__init__.py:3: in <module>
    from .aggregate import aggregate
pycytominer/aggregate.py:8: in <module>
    from pycytominer.cyto_utils import (
pycytominer/cyto_utils/__init__.py:7: in <module>
    from .DeepProfiler_processing import AggregateDeepProfiler
pycytominer/cyto_utils/DeepProfiler_processing.py:11: in <module>
    from pycytominer import aggregate, normalize
pycytominer/normalize.py:8: in <module>
    from pycytominer.cyto_utils import infer_cp_features, load_profiles, output
E   ImportError: cannot import name 'infer_cp_features' from partially initialized module 'pycytominer.cyto_utils' (most likely due to a circular import) (/workspaces/pycytominer/pycytominer/cyto_utils/__init__.py)

An attempt to resolve the error by moving cyto_utils.DeepProfiler_processing:AggregateDeepProfiler to deep_profiler.processing:AggregateDeepProfiler revealed additional circular dependencies within cyto_utils.

From a fresh codebase, a similar error also occurred when trying to switch the codebase from absolute imports (from pycytominer.cyto_utils import) to relative imports (from .cyto_utils import).

Issue description

The error message indicates an issue with circular dependencies. Resolving this issue will likely require a re-organization of cyto_utils into more granular submodules and moving anything that requires aggregate or other core functions out of cyto_utils into a new module.

Expected behavior

We should be able to sort our imports and use relative imports without impacting the ability to load modules.

Additional information

No response

kenibrewer avatar Feb 14 '24 21:02 kenibrewer