Skip to content

Enable banned-module-level-imports (TID253) ruff rule #7030

@danielhollas

Description

@danielhollas

ruff rule TID253

# banned-module-level-imports (TID253)

Derived from the **flake8-tidy-imports** linter.

## What it does
Checks for module-level imports that should instead be imported lazily
(e.g., within a function definition, or an `if TYPE_CHECKING:` block, or
some other nested context).

## Why is this bad?
Some modules are expensive to import. For example, importing `torch` or
`tensorflow` can introduce a noticeable delay in the startup time of a
Python program.

In such cases, you may want to enforce that the module is imported lazily
as needed, rather than at the top of the file. This could involve inlining
the import into the function that uses it, rather than importing it
unconditionally, to ensure that the module is only imported when necessary.

## Options
- `lint.flake8-tidy-imports.banned-module-level-imports`

We currently enforce this in AiiDA for some modules using a custom test/verdi command, but enabling this rule would make this more robust.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions