import-linter icon indicating copy to clipboard operation
import-linter copied to clipboard

Support "forbidden" contracts within packages (again)

Open benthorner opened this issue 2 years ago • 2 comments

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

  • Introduce a separate contract type for this scenario - awkward as the intent is the same.
  • Introduce a flag to treat the source modules as modules and not packages*.
  • Something more clever where the linter recognises the contract is within the same package.

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

benthorner avatar Oct 13 '23 18:10 benthorner

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 avatar Oct 20 '23 16:10 seddonym

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!

seddonym avatar Jan 05 '24 17:01 seddonym