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

Feature request: allow forbidding all third-party modules in contracts without listing each explicitly

Open adriamontoto opened this issue 5 months ago • 1 comments

Hi, first of all, thanks for building Import Linter!

Currently, to forbid third-party libraries, we have to manually list each external dependency in forbidden_modules, like so:

[[tool.importlinter.contracts]]
name = 'Domain layer must not depend on third-party libraries'
type = 'forbidden'
source_modules = ['backend.*.domain']
forbidden_modules = ['fastapi', 'uvicorn', 'pydantic', 'psycopg']
unmatched_ignore_imports_alerting = 'none'

As the number of dependencies grows, this quickly becomes hard to maintain and easy to get out-of-sync with actual dependencies in requirements.txt file.

It would be great to have a built-in way to forbid all third-party libraries without needing to list them manually, perhaps via a keyword like:

forbidden_modules = ['<third_party>']

Or even a config-wide option like:

[tool.importlinter]
forbid_third_party = ['backend.*.domain']

adriamontoto avatar Jul 21 '25 16:07 adriamontoto

Nice idea! I'd be happy to consider a pull request.

You might want to start by creating a custom contract type that you can use on your project. It shouldn't be that much work to extend ForbiddenImportContract to look at modules in the graph that don't belong either to root_packages or to stdlib.stdlib_module_names.

Once you've got that working, feel free to submit a pull request - or share your custom contract here in case someone else feels more comfortable in doing that.

seddonym avatar Jul 22 '25 16:07 seddonym