pycycle icon indicating copy to clipboard operation
pycycle copied to clipboard

cycle not detected

Open Jofkos opened this issue 2 years ago • 0 comments

A rather primitive module cycle is not detected. Minimal working example:

reproduction
├── __init__.py
├── moduleA
│   ├── __init__.py
│   └── a_class.py
└── moduleB
    ├── __init__.py
    └── b_class.py

reproduction > moduleA > __init__.py:

from .a_class import ClassA

reproduction > moduleA > a_class.py:

import reproduction.moduleB


class ClassA:
    pass

reproduction > moduleB > __init__.py:

from .b_class import ClassB

reproduction > moduleB > b_class.py:

from ..moduleA import ClassA


class ClassB:
    pass

When running pycycle:

> pycycle --here --verbose
Trying to parse file: /private/tmp/reproduction/reproduction/__init__.py
Trying to parse file: /private/tmp/reproduction/reproduction/moduleB/__init__.py
Trying to parse file: /private/tmp/reproduction/reproduction/moduleB/b_class.py
Trying to parse file: /private/tmp/reproduction/reproduction/moduleA/a_class.py
Trying to parse file: /private/tmp/reproduction/reproduction/moduleA/__init__.py
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.

when trying to import moduleA:

> python3 -c 'import reproduction.moduleA'   
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/tmp/reproduction/reproduction/moduleA/__init__.py", line 1, in <module>
    from .a_class import ClassA
  File "/private/tmp/reproduction/reproduction/moduleA/a_class.py", line 1, in <module>
    import reproduction.moduleB
  File "/private/tmp/reproduction/reproduction/moduleB/__init__.py", line 1, in <module>
    from .b_class import ClassB
  File "/private/tmp/reproduction/reproduction/moduleB/b_class.py", line 1, in <module>
    from ..moduleA import ClassA
ImportError: cannot import name 'ClassA' from partially initialized module 'reproduction.moduleA' (most likely due to a circular import) (/private/tmp/reproduction/reproduction/moduleA/__init__.py)

Jofkos avatar Nov 01 '22 08:11 Jofkos