gh-144384: Lazily import _colorize#149318
Open
hugovk wants to merge 8 commits intopython:mainfrom
Open
Conversation
picnixz
reviewed
May 3, 2026
Member
picnixz
left a comment
There was a problem hiding this comment.
Can we have a convention for lazy imports
hugovk
commented
May 3, 2026
Member
Author
hugovk
left a comment
There was a problem hiding this comment.
Can we have a convention for lazy imports
Yes, given we don't have a convention, and the SC left it the details to linters and auto-formatters, let's generally follow the Ruff/isort convention:
import json
import os
import subprocess
from collections import defaultdict
from pathlib import Path
from typing import Final
lazy import ast
lazy import shutil
lazy from dataclasses import dataclass
picnixz
reviewed
May 3, 2026
Member
|
Seeing #149321, I think this should wait till that is somewhat figured out. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lazily import
_colorizeto avoid slow construction/import when not needed.The easily testable:
Some indirect improvements:
tracebackneeds some extra handling: add a shutdown theme, so if it attempts to reify_colorizeduring shutdown when the import machinery is no longer around, it has a no-op fallback.Also add
ensure_lazy_importstests._colorizemodule is slow to import, affectingtracebackandloggingmodules #144384