You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support and lints for lazy imports in Python 3.15+ #21305
Relatedly - replacing existing if TYPE_CHECKING: ... imports with lazy imports.
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.)
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.)
More aggressive/opinionated lints - e.g. "make all imports lazy", with an unsafe autofix.
Somewhere in between, less aggressive: Suggesting lazy imports whenever a symbol isn't obviously used at the module root level (still imperfect, but less false-positives than "make all imports lazy")
In other words: "5. Make all imports lazy, except when detected to be used at module root".
Further refinement with code analysis could try to follow function calls, but that seems niche.
@brettcannon This is awesome. Just for clarification, did only the 'basic' support landed (the first 3 points in the issue description) or are also the other ones already done? In particular, I'm curious about what ordering of the imports you decided to implement ("Sorting these imports (this requires a few design decisions)").
Compatibility for library authors was also a key concern. Many maintainers need a migration path that allows them to support both new and old versions of Python at once. For this reason, the proposal includes the __lazy_modules__ global as a transitional mechanism. A module can declare which imports should be treated as lazy (by listing the module names as strings), and on Python 3.15 or later those imports will become lazy automatically, as if they were imported with the lazy keyword. On earlier versions the declaration is ignored, leaving imports eager. This gives authors a practical bridge until they can rely on the keyword as the canonical syntax.
It would be useful to also sort __lazy_modules__. Two options:
Alphabetical
Follow the isort settings, for example: stdlib, third-party, local
We agree that the PEP should take no position on any style recommendations for sorting lazy imports. While we generally like the idea of grouping lazy imports together, let’s leave that up to the linters and auto-formatters to decide the details.
Hey, isort maintainer checking in here!
I don't think we'll (ever) support enforcing lazy imports as that feels more like a linting rule (and we're a bit short staffed to implement new features anyway).
Just wanted to discuss/check how you are planning on sorting lazy imports. Is the idea to apply the same sorting rules to them as for the other import statements? Or do you have different plans.
I think it would be helpful for the community at large if (default) behaviour of isort matches ruff and (vice-versa although I obviously can't force you to 😄).
Feel free to chime in our repository as well (PyCQA/isort#2462).
Just wanted to discuss/check how you are planning on sorting lazy imports. Is the idea to apply the same sorting rules to them as for the other import statements? Or do you have different plans.
Do you have an opinion on sorting __lazy_modules__? I am personally leaning towards just doing those alphabetically and not bothering with the regular sorting we do for imports. Does ruff have an opinion on this?
Alphabetical sounds reasonable to me, personally! We have a couple of other sorting rules that also use "isort-style" sorts like unsorted-dunder-all (RUF022), which uses a "natural sort" grouped by type, and unsorted-dunder-slots (RUF023), which just uses a natural sort. We could reuse that here too.
Activity
Avasam commentedon Nov 10, 2025
Somewhere in between, less aggressive: Suggesting lazy imports whenever a symbol isn't obviously used at the module root level (still imperfect, but less false-positives than "make all imports lazy")
In other words: "5. Make all imports lazy, except when detected to be used at module root".
Further refinement with code analysis could try to follow function calls, but that seems niche.
brettcannon commentedon Feb 20, 2026
This is now in
main.tobiasdiez commentedon Feb 20, 2026
@brettcannon This is awesome. Just for clarification, did only the 'basic' support landed (the first 3 points in the issue description) or are also the other ones already done? In particular, I'm curious about what ordering of the imports you decided to implement ("Sorting these imports (this requires a few design decisions)").
ntBre commentedon Feb 20, 2026
I think Brett meant that this landed in CPython! We haven't added this support to Ruff yet, as far as I know.
brettcannon commentedon Feb 20, 2026
Correct! Sorry about the ambiguous wording. 😅
hugovk commentedon Feb 25, 2026
From the PEP:
It would be useful to also sort
__lazy_modules__. Two options:And the Steering Council's acceptance said:
See also PyCQA/isort#2462.
Add support for `lazy` import parsing (#23755)
Reject semantic syntax errors for lazy imports (#23757)
Retain `lazy` keyword when sorting imports (#23762)
charliermarsh commentedon Mar 9, 2026
We now have support for this in the parser, and a rule to enforce
lazyimports: #23777Avasam commentedon Mar 9, 2026
Can I require lazy imports for all modules except specific ones I know have side effects ?
Same reason pywin32 would use allowed-unused-imports
With
banned-eager-imports(if comparable with the documentation from https://docs.astral.sh/ruff/settings/#lint_flake8-tidy-imports_banned-module-level-imports) I'd have to list every possible import in existence except the ones I know have side effects.charliermarsh commentedon Mar 9, 2026
That seems reasonable to support.
DanielNoord commentedon Mar 14, 2026
Hey,
isortmaintainer checking in here!I don't think we'll (ever) support enforcing
lazyimports as that feels more like a linting rule (and we're a bit short staffed to implement new features anyway).Just wanted to discuss/check how you are planning on sorting
lazy imports. Is the idea to apply the same sorting rules to them as for the other import statements? Or do you have different plans.I think it would be helpful for the community at large if (default) behaviour of
isortmatchesruffand (vice-versa although I obviously can't force you to 😄).Feel free to chime in our repository as well (PyCQA/isort#2462).
dylwil3 commentedon Mar 14, 2026
At the moment we do this: #23762 (comment)
i.e. group eager before lazy within each section.
But we're open to other possibilities!
7 remaining items
chet-manley commentedon May 14, 2026
ntBre commentedon May 14, 2026
Bump ruff to v0.15.16; drop lazy-imports test excludes
DanielNoord commentedon Oct 5, 2026
Hey, checking in from PyCQA/isort#2462 (comment)
Do you have an opinion on sorting
__lazy_modules__? I am personally leaning towards just doing those alphabetically and not bothering with the regular sorting we do for imports. Doesruffhave an opinion on this?ntBre commentedon Oct 5, 2026
Alphabetical sounds reasonable to me, personally! We have a couple of other sorting rules that also use "isort-style" sorts like unsorted-dunder-all (RUF022), which uses a "natural sort" grouped by type, and unsorted-dunder-slots (RUF023), which just uses a natural sort. We could reuse that here too.
hugovk commentedon Oct 5, 2026
Keep in mind that
__lazy_modules__can be any container, not just a list:docs.python.org/3.15/reference/datamodel.html#module.__lazy_modules__
(And looks like a set is often the quickest: henryiii/flake8-lazy#50.)
Avasam commentedon Oct 5, 2026
flake8-lazycan also be a reference implementation for__lazy_modules__sorting.DanielNoord commentedon Oct 5, 2026
This seems like a good enough heuristic.
Looks pretty reasonable to me. Thanks all!
TYPE_CHECKINGimports with lazy imports #29378 as a duplicate of this issueMichaReiser commentedon Oct 19, 2026
Should we close this issue, given that general support has now been released, and create new sub issues for what's still missing?
ntBre commentedon Oct 19, 2026
Sure, I can reopen #29378 to track point 3 and open a new issue for point 4.
TYPE_CHECKINGimports with lazy imports #29378 as a duplicate of this issue