Skip to content

Defer heavy imports to speed up import time - #968

Merged
Carreau merged 1 commit into
ipython:mainfrom
Carreau:laz-imp
Aug 4, 2026
Merged

Defer heavy imports to speed up import time#968
Carreau merged 1 commit into
ipython:mainfrom
Carreau:laz-imp

Conversation

@Carreau

@Carreau Carreau commented Aug 3, 2026

Copy link
Copy Markdown
Member

traitlets and its core utils imported inspect (which pulls in ast, dis, tokenize, linecache), pathlib, and ast at module top level, and traitlets.config additionally imported logging.config (pulling logging.handlers, socket, pickle, dataclasses), pprint and json eagerly. All of these are only needed on cold paths (string parsing, filesystem Path traits, help/config-dump output, logging configuration at Application startup). Because the modules use from __future__ import annotations, annotations referring to these names are never evaluated at runtime, so the imports can be deferred to their actual (rare) use sites. inspect.isclass(x) is replaced with isinstance(x, type) (which is strictly identical since python 3) and inspect.currentframe() with sys._getframe() (which is the same on Cpython) to avoid needing inspect at all on the class-definition path.

traitlets and its core utils imported inspect (which pulls in ast, dis,
tokenize, linecache), pathlib, and ast at module top level, and
traitlets.config additionally imported logging.config (pulling
logging.handlers, socket, pickle, dataclasses), pprint and json eagerly.
All of these are only needed on cold paths (string parsing, filesystem
Path traits, help/config-dump output, logging configuration at Application
startup). Because the modules use `from __future__ import annotations`,
annotations referring to these names are never evaluated at runtime, so the
imports can be deferred to their actual (rare) use sites. inspect.isclass(x)
is replaced with isinstance(x, type) (which is strictly identical since python 3)
and inspect.currentframe() with sys._getframe() (which is the same on Cpython)
to avoid needing inspect at all on the class-definition path.
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.98%. Comparing base (1a0ca47) to head (3c621a5).

Files with missing lines Patch % Lines
traitlets/traitlets.py 83.33% 1 Missing and 1 partial ⚠️
traitlets/utils/getargspec.py 60.00% 1 Missing and 1 partial ⚠️
Components Coverage Δ
traitlets 85.35% <87.50%> (+0.17%) ⬆️
tests 99.03% <100.00%> (+<0.01%) ⬆️

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Carreau
Carreau merged commit 626bbe5 into ipython:main Aug 4, 2026
58 of 60 checks passed
@Carreau Carreau added this to the 5.17 milestone Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant