PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.53k stars 583 forks source link

Circular Import Issue Due to Import Reordering in Python 3.8 #2259

Open myousefi opened 7 months ago

myousefi commented 7 months ago

Description

When using isort to organize imports in our project, we encountered a subtle issue where isort's reordering of import statements introduced a circular import problem in Python versions 3.8.19. This issue arises specifically in our simulation engine module, where the order of imports is crucial to avoid circular dependencies.

Environment

Minimal Working Example (MWE)

simulation.py

from __future__ import annotations
from .replication_manager import ReplicationManager

class Simulation:
    pass

replication_manager.py

from __future__ import annotations
from .simulation import Simulation

class ReplicationManager:
    def __init__(self, simulation: Simulation):
        self.simulation = simulation

init.py

from .simulation import Simulation
from .replication_manager import ReplicationManager

__all__ = ["Simulation", "ReplicationManager"]

In this example, running isort would reorder the imports in the init file a way that introduces a circular dependency between simulation.py and replication_manager.py.