-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import-outside-top-level (PLC0415)
is incompatible with flake8-tidy-imports.banned-module-level-imports
#12803
Comments
I think this is pretty reasonable. I've implemented this as a custom lint rule (for tensorflow -- to ensure it's always imported lazily) in the past. |
Any imports that are marked as required-top-level should also be ignored in |
Do you mean "required-not-top-level" ? Also I just found https://docs.astral.sh/ruff/rules/banned-module-level-imports/ , it seems to be doing exactly what I want. But it's not respected by |
import-outside-top-level (PLC0415)
is incompatible with flake8-tidy-imports.banned-module-level-imports
Yes sorry, that’s what I meant. Thanks for spotting that! |
I’ll call it a bug that they’re aren’t compatible. |
Reframed issue:
import-outside-top-level (PLC0415)
andbanned-module-level-imports (TID253)
are incompatible together.ruff.toml
Code:
Original issue
PLC0415, top level module import, banned api
ruff --version
): ruff 0.5.5I came across a use-case where it would be preferable to check that certain imports are never imported directly at the module level. This is mainly for dependencies that should stay optional, or that are known to be be ostly and only useful in specific codepaths.
For example, setuptools could configure
ctypes
,typing_extensions
, and more, to ensure that they're never unconditionally imported.A check that the import is behind a conditional (if-else, try-except) or in a function, should be sufficient.
This is similar in idea to the https://docs.astral.sh/ruff/rules/#flake8-type-checking-tch (TCH001, TCH002, TCH003) rules.
This would conflict with https://docs.astral.sh/ruff/rules/import-outside-top-level/ (PLC0415). My original idea was to suggest an ignore list for PLC0415, but I think this could be pushed further with a rule that actually enforces it.
If this idea is rejected, I'd still like an ignore/allowlist for PLC0415 .
The text was updated successfully, but these errors were encountered: