Skip to content

Support and lints for lazy imports in Python 3.15+ #21305

@dylwil3

Description

@dylwil3

This issue tracks support for lazy imports in Ruff.

Python 3.15 introduced PEP 810 - Explicit lazy imports. We have done the following:

  • Update the parser
  • Update the formatter
  • Update the semantic model (though according to this subheading these will be treated the same as ordinary imports for our purposes)

But we may also want to consider some lints. For example:

  1. ✅ Sorting these imports - Retain lazy keyword when sorting imports #23762
  2. Adjust the fixes in flake8-type-checking to suggest that typing-only imports be lazily imported rather than gated behind if TYPE_CHECKING, as per this use-case in the PEP.
  3. Relatedly - replacing existing if TYPE_CHECKING: ... imports with lazy imports.
  4. Suggesting lazy imports any time we see an import not at the beginning of the file. (Developers sometimes approximate lazy imports these days by importing in the body of a function, for example.)
  5. ✅ Enforce lazy imports always (or for configurable set of modules) - Add a rule to enforce lazy imports #23777
  6. Conversely, removing lazy if we can guarantee that the import will be evaluated (e.g. by a top-level statement in the module).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions