'Is it possible to enforce via CI that module_a does not import anything from module_b?

I'm maintaining several open source projects and I want to write code at work that nudges people to do the right thing.

I have a situation where I see people importing stuff in module_a from module_b, but that should not happen. There are two reasons for it:

  1. Production code importing stuff from test code: I hope I don't need to explain why that is a bad idea.
  2. Import Cycles: Some modules are so basic, that they should not import any other modules from the package (e.g. constants.py / errors.py / utils.py).

For this question, you can assume that all imports happen on module level (hence not inside a function).

Is it possible to enforce via CI (e.g. mypy / pytest / flake8) that module_a does not import anything from module_b?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source