bndr / pycycle

Tool for pinpointing circular imports in Python. Find cyclic imports in any project
MIT License
333 stars 24 forks source link

[BUG] Doesn't detect cycles when deep relative imports are used #14

Open webknjaz opened 4 years ago

webknjaz commented 4 years ago

I haven't tried to debug it but it's reproducible with this version of my project: https://github.com/sanitizers/octomachinery/tree/4cd3455d6d5f62b94bd1fd2ce78658cbce3eb18a

pycycle says No worries, no cycles here! but in fact it misses some.

This is how it explodes in runtime:

$ ipython ```python-traceback Python 3.8.0 (default, Oct 16 2019, 16:00:06) Type 'copyright', 'credits' or 'license' for more information IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: from octomachinery.github.api.app_client import GitHubApp --------------------------------------------------------------------------- ImportError Traceback (most recent call last) in ----> 1 from octomachinery.github.api.app_client import GitHubApp ~/.pyenv/versions/chronographer-github-app-py3.8.0-pyenv-venv/lib/python3.8/site-packages/octomachinery/github/api/app_client.py in 10 11 # pylint: disable=relative-beyond-top-level ---> 12 from ...app.routing import WEBHOOK_EVENTS_ROUTER 13 # pylint: disable=relative-beyond-top-level 14 from ...app.routing.abc import OctomachineryRouterBase ~/.pyenv/versions/chronographer-github-app-py3.8.0-pyenv-venv/lib/python3.8/site-packages/octomachinery/app/__init__.py in 1 """GitHub App infra.""" 2 ----> 3 from .server.runner import run # noqa: F401 ~/.pyenv/versions/chronographer-github-app-py3.8.0-pyenv-venv/lib/python3.8/site-packages/octomachinery/app/server/runner.py in 18 from .config import WebServerConfig 19 # pylint: disable=relative-beyond-top-level ---> 20 from .machinery import run_forever as run_server_forever 21 22 ~/.pyenv/versions/chronographer-github-app-py3.8.0-pyenv-venv/lib/python3.8/site-packages/octomachinery/app/server/machinery.py in 9 10 # pylint: disable=relative-beyond-top-level ---> 11 from ...github.api.app_client import GitHubApp 12 # pylint: disable=relative-beyond-top-level 13 from ...utils.asynctools import auto_cleanup_aio_tasks ImportError: cannot import name 'GitHubApp' from partially initialized module 'octomachinery.github.api.app_client' (most likely due to a circular import) (~/.pyenv/versions/chronographer-github-app-py3.8.0-pyenv-venv/lib/python3.8/site-packages/octomachinery/github/api/app_client.py) ```
togakangaroo commented 2 years ago

Just looking through the code and I had the same question when I couldn't understand how it was handling relative imports (which in python2 can look the same as regular imports). Seems like it doesn't