Skip to content

gh-150082: Lazily import modules within standard library#150083

Closed
clin1234 wants to merge 6 commits into
python:mainfrom
clin1234:lazyimport
Closed

gh-150082: Lazily import modules within standard library#150083
clin1234 wants to merge 6 commits into
python:mainfrom
clin1234:lazyimport

Conversation

@clin1234
Copy link
Copy Markdown
Contributor

@clin1234 clin1234 commented May 19, 2026

@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 19, 2026

Ruff lint errors like:

Error: Tools/i18n/pygettext.py:153:1: invalid-syntax: Cannot use `lazy` import statement on Python 3.13 (syntax was added in Python 3.15)

Can be fixed by updating it:

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 6878a7d92e3..e378baf8930 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,6 +1,6 @@
 repos:
   - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: e05c5c0818279e5ac248ac9e954431ba58865e61  # frozen: v0.15.7
+    rev: 5e2fb545eba1ea9dc051f6f962d52fe8f76a9794  # frozen: v0.15.13
     hooks:
       - id: ruff-check
         name: Run Ruff (lint) on Platforms/Apple/

@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 19, 2026

Or actually you'll need to update target-version to 3.15 in the relevant .ruff.toml config files. Some of them might need to stick to a lower version.

Comment thread Lib/ensurepip/__init__.py
import tempfile
from contextlib import nullcontext
from importlib import resources
lazy from importlib import resources
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of lazily importing this? It's used on all of the public code paths through the module, and there don't seem to be any significant conditional branches which bypass the usage.

@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 19, 2026

This PR is way too big: 237 changed files. Please split it up, and it's probably worth providing justification in the issue first, for why each particular import was chosen in each particular module.


# Now it is safe to import idlelib.
from idlelib.pyshell import main
lazy from idlelib.pyshell import main
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make this change.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 19, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@StanFromIreland
Copy link
Copy Markdown
Member

What is the benefit for doing this for tests?

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 19, 2026

Considering there are too many files I am closing this. In addition the PEP explicitly stated that this should not be done IIRC, and this must only be addres on a case-by-case basis.

@picnixz picnixz closed this May 19, 2026
@clin1234 clin1234 deleted the lazyimport branch May 19, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants