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

why can't you forbid subpackages of external modules? #182

Open dpinol opened 1 year ago

dpinol commented 1 year ago
[tool.importlinter]
include_external_packages = true
root_packages = ["scserver", "scweb", "test"]

[[tool.importlinter.contracts]]
name = "kk"
forbidden_modules = ["pytest_check.check"]
source_modules = ["test"]
type = "forbidden"

With this config I get: Invalid forbidden module pytest_check.check: subpackages of external packages are not valid.

seddonym commented 1 year ago

You should be able to fix this by adding pytest_check as one of the root packages. Alternatively, change pytest_check.check to just pytest_check.

The reason for this is because Import Linter analyses a graph of the imports between different modules. If you don't include pytest_check as a root package then it will just be added as a single node pytest_check in the graph.

Hope that solves your problem!

pwalsh commented 1 year ago

jumping in here, with (snippets from pyproject.toml):

[tool.importlinter]
root_packages = ["survey", "api", "django"]
include_external_packages = true

[[tool.importlinter.contracts]]
name = "Dont use cache dependencies directly"
type = "forbidden"
source_modules = ["survey", "api"]
forbidden_modules = ["django.core.cache"]

From the above comment, I would expect django.core.cache to be forbidden, but, I get a huge list of import errors as any import under django.* is listed as an error.

If I change root_packages to either

root_packages = ["survey", "api", "django.core"]

#

root_packages = ["survey", "api", "django.core.cache"]

then no linting errors related to the Django package are detected.

I'd like to, for example, prevent usage of anything under django.core.cache by raising lint errors. is this possible some other way>

seddonym commented 1 year ago

@pwalsh - I've opened a separate ticket for your question, see link above.