seddonym / import-linter

Import Linter allows you to define and enforce rules for the internal and external imports within your Python project.
https://import-linter.readthedocs.io/
BSD 2-Clause "Simplified" License
664 stars 45 forks source link

Support "forbidden" contracts within packages (again) #199

Open benthorner opened 10 months ago

benthorner commented 10 months ago

Previously it was possible to write a contract like this:

[importlinter:contract:core-factories]
name = "Factories should be split into subpackages"
type = forbidden
allow_indirect_imports = true
source_modules =
    tests.factories
forbidden_modules =
    tests.factories.domain_a
    tests.factories.domain_b

...to prevent the following imports:

# tests/factories/__init__.py
from tests.factories.domain_a import factory_1
...
from tests.factories.domain_b import factory_2

This stopped working in https://github.com/seddonym/import-linter/commit/2cdb36fb2c4f657a5339d36dd5797bd891e7111d - essentially, the bug was a feature.

Although this is quite a "dumb" use of import linter - it's little more than a grep - it makes sense to express such contracts alongside other more powerful ones for the same codebase.

Would it be possible to support this again?

Suggestions

*Treating modules as packages seems to be the default / intended behaviour.

seddonym commented 10 months ago

Thanks Ben for these suggestions. I agree we should support this somehow.

I'm leaning towards adding an as_packages flag which, if it was set to false, would allow you to do this. That would fit in well with the terminology of Grimp, the underlying static analysis library: many of the methods there allow you to specify whether to treat the modules as_packages.

Open to other ideas though.

seddonym commented 8 months ago

Having had more time to mull this over, I think it would the as_packages flag would be a good addition. Let me know if you would be interested in submitting a pull request!