bento icon indicating copy to clipboard operation
bento copied to clipboard

Detect cyclic imports

Open dmontagu opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe.

One form of static check that I think would be really useful for helping make sure a codebase is well organized and robust to refactors is import cycle detection.


Currently, I occasionally make use of this neat little tool and run it over my projects and manually inspect the graphs to look for import cycles. I typically find that, after refactoring in such a way to make my import graph a DAG, everything feels better organized, and I can more flexibly add new functionality without running into fragile cyclic import logic.

(Note: python can handle cyclic imports in some cases, but in my experience, its usually a sign that modules aren't organized as well as they could be.)

So far, I haven't found any tools that can automatically generate a warning when two modules import each other, or more generally when there is an import cycle.

(The tool linked above just generates graphs, nothing CI/linter-friendly, though the data necessary is obviously present somewhere in memory while generating the graphs.)


Describe the solution you'd like

It would be really awesome if bento could warn me if/when my import graph is not a DAG.

This probably doesn't fall under the purview of of flake8 or bandit, so I'm not sure how much of a priority it would be for bento in the short term, but I would find this very useful (and I think it shouldn't be too hard to implement).

dmontagu avatar Oct 28 '19 19:10 dmontagu

Thanks for filing an issue, @dmontagu !

Have you taken a look at https://github.com/bndr/pycycle for import detection? Curious if it would work to solve your specific usecase. If so, it might not be hard for us to integrate!

ievans avatar Oct 28 '19 20:10 ievans

Nope haven't tried it, but yes, it does look like it would address my use case!

dmontagu avatar Oct 28 '19 20:10 dmontagu