astral-sh / ruff

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

`ICN003`: Allow banning all `from` stdlib imports #10664

Open Grub4K opened 8 months ago

Grub4K commented 8 months ago

Our project decided to no longer allow stdlib imports with from due to potentially clashing naming causing confusion. We are currently resorting to including every stdlib in the flake8-import-conventions banned-from setting:

[tool.ruff.lint.flake8-import-conventions]
banned-from = ["abc", "aifc", "antigravity", "argparse", "array", "ast", "asynchat", "asyncio", "asyncore", "atexit", "..."]

However this is very cumbersome. Is there a possiblity of referencing all stdlib modules? A shortcut like this could perhaps also be used for different importing settings.

I've searched for ICN003, banned-from and banned-import-from, but could not find anything related.

dhruvmanila commented 8 months ago

Thanks for the clear write-up!

I would rather go with adding this as a separate rule. @charliermarsh Is this why you marked this with "rule" label?

zanieb commented 8 months ago

Why a separate rule if it's the same idea?

cc @AlexWaygood

charliermarsh commented 8 months ago

Is this why you marked this with "rule" label?

(No, the "rule" label can represent either a new rule or a change to an existing rule.)

dhruvmanila commented 8 months ago

Why a separate rule if it's the same idea?

This might be a stretch but it could lead to name collisions. So, we could either have "stdlib" as a possible value or maybe provide a new config option for that rule (banned-from-stdlib = true).

carljm commented 8 months ago

I'm curious why the stdlib would be special in terms of "potentially clashing naming causing confusion." What about other third-party libraries? Should the option here really be banned-from-non-first-party?

Grub4K commented 8 months ago

Maybe banned-from-non-first-party could have "stdlib" as a sentinel to signify only stdlib for that case? Having it as a boolean option alone could mean not having enough granular control over what to ban?