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
679 stars 48 forks source link

Improve module validation #172

Closed seddonym closed 1 year ago

seddonym commented 1 year ago

Suggested in https://github.com/seddonym/import-linter/issues/111.

The following contract has a nonobvious problem with it:

[importlinter]
root_package = mypackage
include_external_packages = true

[importlinter:contract:a]
name=Some contract
type=forbidden
source_modules=
    mypackage.foo
forbidden_modules=
    requests.api

The forbidden module requests.api should read just requests, because requests is not one of the root packages so, assuming mypackage imports it somewhere, it will only appear in the graph as requests.

Prior to this commit, if mypackage.foo imported requests.api then this contract would pass. Now, Import Linter will error, saying "Invalid forbidden module requests.api: subpackages of external packages are not valid."