astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.85k stars 1.1k forks source link

Implement import-linter #1912

Open jankatins opened 1 year ago

jankatins commented 1 year ago

Import Linter allows you to define and enforce rules for the internal and external imports within your Python project.

From the readme at https://github.com/seddonym/import-linter:

Decide on the dependency flows you wish to check. In this example, we have decided to make sure that myproject.foo has dependencies on neither myproject.bar nor myproject.baz, so we will use the forbidden contract type.

Create an .importlinter file in the root of your project to define your contract(s). In this case:

[importlinter]
root_package = myproject

[importlinter:contract:1]
name=Foo doesn't import bar or baz
type=forbidden
source_modules=
    myproject.foo
forbidden_modules=
    myproject.bar
    myproject.baz

Some more background on this kind of linter in this blog post: https://sourcery.ai/blog/dependency-rules/

charliermarsh commented 1 year ago

If helpful in the interim, we do support some subset of this via flake8-tidy-imports, e.g.:

[tool.ruff.flake8-tidy-imports.banned-api]
"cgi".msg = "The cgi module is deprecated, see https://peps.python.org/pep-0594/#cgi."
"typing.TypedDict".msg = "Use typing_extensions.TypedDict instead."
ashrub-holvi commented 3 months ago

Looks like there is an alternative to import-linter written in Rust - https://github.com/gauge-sh/tach/