Uh oh!
There was an error while loading. Please reload this page.
gh-144384: Lazily import _colorize - #149318
Conversation
picnixz
left a comment
There was a problem hiding this comment.
Can we have a convention for lazy imports
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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:
importjsonimportosimportsubprocessfromcollectionsimportdefaultdictfrompathlibimportPathfromtypingimportFinallazyimportastlazyimportshutillazyfromdataclassesimportdataclassUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
StanFromIreland
commented
May 3, 2026
Seeing #149321, I think this should wait till that is somewhat figured out. |
hugovk
commented
May 4, 2026
hugovk
commented
May 4, 2026
And both the commands from #149321 are working. The CIFuzz failures are unrelated network errors: There's another outage: https://status.canonical.com/#/incident/KNms6QK9ewuzz-7xUsPsNylV20jEt5kyKsd8A-3ptQG1OfBTMZO_fBkVMmGqQC3NLBLamiI6KOEL7KpsobFJ4Q== |
hugovk
commented
May 6, 2026
CIFuzz now also passing. |
sergey-miryanov
left a comment
There was a problem hiding this comment.
Looks good to me, but I'm not very familiar with lazy imports.
JelleZijlstra
commented
May 6, 2026
I'm not sure this makes sense for the pyrepl modules. Aren't those essentially always going to want _colorize? |
DavidCEllis
commented
May 6, 2026
I'm not sure the |
But it still gets imported: So I'll remove that change. |
hugovk
commented
May 6, 2026
And reverted in |
Documentation build overview
11 files changed · |
hugovk
commented
May 6, 2026
JelleZijlstra
commented
May 6, 2026
Yes, pdb also seemed questionable to me since if you use pdb you'll almost certainly want the prompt, which will give you colors. |
JelleZijlstra
left a comment
There was a problem hiding this comment.
json.tool is also unnecessary, it uses colorize in main() which is the only interface to that module.
I haven't looked much into profiling, are there plausible code paths where that is being used that don't go through colorize? Same for unittest and doctest.
difflib and traceback seem OK, they're broad libraries and there are plenty of functions in there that don't need color.
When you're done making the requested changes, leave the comment: |
JelleZijlstra
left a comment
There was a problem hiding this comment.
A couple still seem questionable but the cost of an extra lazy import isn't that high, so I'm OK with this going in.
Uh oh!
There was an error while loading. Please reload this page.
hugovk
commented
May 6, 2026
Thanks all! |

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