astral-sh / ruff

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

Add `flake8-obey-import-goat` #3127

Open bellini666 opened 1 year ago

bellini666 commented 1 year ago

Ref: https://pypi.org/project/flake8-obey-import-goat

henryiii commented 1 year ago

Isn't this the same thing as TID? https://beta.ruff.rs/docs/rules/#flake8-tidy-imports-tid ?

charliermarsh commented 1 year ago

Yeah this looks identical to banned-api.

bellini666 commented 1 year ago

Yeah this looks identical to banned-api.

Not necessarily. What it it does it to ensure that some imports are done in a specific way.

For example, if you want for someone to always import os and use os.path instead of from is import path.

The banned-api, if I understood that correctly, will totally prevent it from being used

JonathanPlasse commented 1 year ago

With banned-api, you cannot specify with a glob which modules are concerned for the ban.

JonathanPlasse commented 1 year ago

The configuration of flake8-obey-import-goat look like this:

[flake8]
forbidden-imports =
    *: datetime.datetime, stdlib modules should be imported as a module
    *: typing.Optional, we use T | None instead of Optional[T]
    users.*: foo.*, users module should not use foo module
    *.implementation.*: *.domain.*, implementation layer should not use domain layer

With the banned-api plugin, the configuration would look like this:

[tool.ruff.flake8-tidy-imports.banned-api]
"datetime.datetime".msg = "stdlib modules should be imported as a module"
"typing.Optional".msg = "we use T | None instead of Optional[T]"
"foo.*".msg = "users module should not use foo module"
"*.domain.*".msg = "implementation layer should not use domain layer"

banned-api, could be extended by adding a path like this, by default would be *.

[tool.ruff.flake8-tidy-imports.banned-api]
"datetime.datetime".msg = "stdlib modules should be imported as a module"
"typing.Optional".msg = "we use T | None instead of Optional[T]"
"foo.*".msg = "users module should not use foo module"
"foo.*".path = "users.*"
"*.domain.*".msg = "implementation layer should not use domain layer"
"*.domain.*".path = "*.implementation.*"
JonathanPlasse commented 1 year ago

Is a pull request welcome with this new behavior? It would be backward compatible. How should we handle that this is a merge of two plugins in the documentation? Should we mention flake8-obey-import-goat in flake8-tidy-imports.banned-api? Or, should we only add the license flake8-obey-import-goat?