pycycle icon indicating copy to clipboard operation
pycycle copied to clipboard

Cycle not detected with empty __init__.py

Open shobsi opened this issue 1 year ago • 0 comments

Have a circular import

(venv) $ tree .
.
├── alpha.py
└── beta.py

1 directory, 2 files

(venv)  $ cat alpha.py
from beta import Class2
class Class1:
    obj = Class2()

(venv)  $ cat beta.py
from alpha import Class1
class Class2:
    obj = Class1()

Python fails with the ImportError as expected:

(venv)  $ python alpha.py
Traceback (most recent call last):
  File "/cur/dir/alpha.py", line 1, in <module>
    from beta import Class2
  File "/cur/dir/beta.py", line 1, in <module>
    from alpha import Class1
  File "/cur/dir/alpha.py", line 1, in <module>
    from beta import Class2
ImportError: cannot import name 'Class2' from partially initialized module 'beta' (most likely due to a circular import) (/cur/dir/beta.py)

pycycle detects the cycle as expected:

(venv) $ pycycle --here
Project successfully transformed to AST, checking imports for cycles..
Cycle Found :(
alpha -> beta: Line 1 =>> alpha
Finished.

Create an empty __init__.py in the directory. After that pycycle can no longer detect the cycle.

(venv) $ touch __init__.py
(venv) $ pycycle --here
Project successfully transformed to AST, checking imports for cycles..
No worries, no cycles here!
If you think some cycle was missed, please open an Issue on Github.
Finished. 

shobsi avatar Nov 07 '24 03:11 shobsi