chore: ban module-level imports of startup-heavy modules - #380
Draft
cloudsmith-iduffy wants to merge 2 commits into
Draft
chore: ban module-level imports of startup-heavy modules#380cloudsmith-iduffy wants to merge 2 commits into
cloudsmith-iduffy wants to merge 2 commits into
Conversation
6 tasks
Select TID253 with a ban list of the modules that dominated CLI startup. Files that load lazily are allowlisted per file. Defer the keyring import in the frozen entrypoint to the selftest that uses it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
core/keyring.py sits on the eager import path through the credential chain. Import the keyring library inside the functions that use it and remove the file from the TID253 allowlist. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
cloudsmith-iduffy
force-pushed
the
lint/ban-heavy-module-level-imports
branch
from
August 21, 2026 23:40
82a5203 to
f7601a4
Compare
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.
Description
The perf work in this stack got
mcp,httpx,cloudsmith_api,requests,rich,semver,urllib3andkeyringout of CLI startup. The startup tests guard the two main entry points, but someone could still addimport requeststo the top of any other eagerly-imported file and quietly hand everydocker pullits ~30ms back.This closes that gap with ruff's banned-module-level-import rule (
TID253): the eight modules above can now only be imported inside functions. Files that are themselves loaded lazily — the command modules, thecore/api/*call sites, the MCP server, and the session/rest/saml/download leaves — are allowlisted per file, so their existing top-level imports stay put. Anything new fails lint, in pre-commit and CI, via the existing ruff hook.Turning the rule on immediately found two real problems:
packaging/pyinstaller/entry.py) importedkeyring.backendat module level, but only the packaging selftest uses it — every invocation of the frozen binary paid for an import it didn't need. It now lives inside the selftest helper. (Frozen imports are pre-compiled, so this one is below measurement noise — the binary clocks 0.26s before and after. Fixed because the rule is right, not because it's measurable.)core/keyring.pyimported thekeyringlibrary at module level, and that file is on the eager path (it's pulled in through the credential chain). My first instinct was to allowlist it; on review that was just papering over a real cost. The library is only used inside five functions, so the imports moved there — andcloudsmith --versiondropped from 0.12s to 0.09s.No flame graphs on this one — the only import-graph change is the small keyring subtree, and the numbers above tell the story. The real value is that the next accidental heavy import gets caught by the linter instead of a profiler.
Type of Change
Lint guard for startup performance, plus the two import deferrals it flagged.
Additional Notes
cli/commands/*as a directory, which also exempts the eagerly-importedmain.pyandregistry.py. The subprocess tests incli/tests/test_startup_imports.pycover exactly that hole.SELFTEST: OK, 129 modules).🤖 Generated with Claude Code