FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-144384: Lazily import `_colorize` by hugovk · Pull Request #149318 · python/cpython · GitHub

/ cpython Public

gh-144384: Lazily import _colorize - #149318

Merged
hugovk merged 16 commits into
python:mainfrom
hugovk:3.15-lazy-_colorize
May 6, 2026
Merged

gh-144384: Lazily import _colorize#149318
hugovk merged 16 commits into
python:mainfrom
hugovk:3.15-lazy-_colorize

Conversation

hugovk commented May 3, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Lazily import _colorize to avoid slow construction/import when not needed.

The easily testable:

Before After
difflib: 10ms 2ms (80% faster)
doctest: 48ms 40ms (17% faster)
json.tool: 13ms 6ms (54% faster)
pdb: 43ms 37ms (14% faster)
traceback: 15ms 8ms (47% faster)
unittest: 20ms 14ms (30% faster)

Some indirect improvements:

Before After
asyncio: 47ms 27ms (43% faster)
logging: 17ms 10ms (41% faster)

traceback needs some extra handling: add a shutdown theme, so if it attempts to reify _colorize during shutdown when the import machinery is no longer around, it has a no-op fallback.

Also add ensure_lazy_imports tests.

hugovk requested a review from berkerpeksag as a code owner May 3, 2026 14:32
hugovk added the performance Performance or resource usage label May 3, 2026
hugovk added the stdlib Standard Library Python modules in the Lib/ directory label May 3, 2026
hugovk requested review from ambv and gaogaotiantian as code owners May 3, 2026 14:32

picnixz left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can we have a convention for lazy imports

Comment thread Lib/_pyrepl/fancycompleter.py Outdated
Comment thread Lib/traceback.py
Comment thread Lib/traceback.py

hugovk left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/traceback.py
Comment thread Lib/traceback.py
Comment thread Lib/traceback.py

Copy link
Copy Markdown
Member

Seeing #149321, I think this should wait till that is somewhat figured out.

hugovk commented May 4, 2026

Copy link
Copy Markdown
Member Author

Seeing #149321, I think this should wait till that is somewhat figured out.

#149338 has been merged, updating.

hugovk commented May 4, 2026

Copy link
Copy Markdown
Member Author

And both the commands from #149321 are working.


The CIFuzz failures are unrelated network errors:

413.7 E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/universe/t/tcltk-defaults/tcl-dev_8.6.9+1_amd64.deb  Connection failed [IP: 185.125.190.81 80]

There's another outage: https://status.canonical.com/#/incident/KNms6QK9ewuzz-7xUsPsNylV20jEt5kyKsd8A-3ptQG1OfBTMZO_fBkVMmGqQC3NLBLamiI6KOEL7KpsobFJ4Q==

hugovk commented May 6, 2026

Copy link
Copy Markdown
Member Author

CIFuzz now also passing.

sergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good to me, but I'm not very familiar with lazy imports.

Copy link
Copy Markdown
Member

I'm not sure this makes sense for the pyrepl modules. Aren't those essentially always going to want _colorize?

Copy link
Copy Markdown
Contributor

I'm not sure the sqlite.__main__ lazy import is worthwhile? While it's not imported at top level, main will immediately import it with ArgumentParser() or get_theme a little later.

hugovk commented May 6, 2026
edited
Loading

Copy link
Copy Markdown
Member Author

I'm not sure this makes sense for the pyrepl modules. Aren't those essentially always going to want _colorize?

NO_COLOR=1 ./python.exe is a thing, and gives you a colour-free REPL.

But it still gets imported:

❯ NO_COLOR=1 ./python.exe
Python 3.15.0a8+ (heads/3.15-lazy-_colorize:2d346d5a1ea, May  6 2026, 16:40:10) [Clang 17.0.0 (clang-1700.6.4.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> "_colorize" in sys.modules
True

So I'll remove that change.

hugovk commented May 6, 2026

Copy link
Copy Markdown
Member Author

And reverted in sqlite3.__main__ as well.

read-the-docs-community Bot commented May 6, 2026
edited
Loading

Copy link
Copy Markdown

hugovk commented May 6, 2026

Copy link
Copy Markdown
Member Author

Also reverted pdb, it imports it via pyrepl:

Copy link
Copy Markdown
Member

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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

bedevere-app Bot commented May 6, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

JelleZijlstra left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

hugovk enabled auto-merge (squash) May 6, 2026 16:00
hugovk merged commit 7cea70e into python:main May 6, 2026
67 of 68 checks passed
hugovk deleted the 3.15-lazy-_colorize branch May 6, 2026 16:07

hugovk commented May 6, 2026

Copy link
Copy Markdown
Member Author

Thanks all!

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

Labels

performance Performance or resource usage stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL