Skip to content

PERF: Defer xarray and openpyxl imports to reduce CLI startup time - #375

Open
karlhillx wants to merge 2 commits into
nasa:mainfrom
karlhillx:fix/374-defer-imports
Open

karlhillx wants to merge 2 commits into
nasa:mainfrom
karlhillx:fix/374-defer-imports

Conversation

@karlhillx

@karlhillx karlhillx commented Sep 19, 2026 •

Copy link
Copy Markdown

GitHub Issue: #374

Description

import ncompare no longer loads xarray (with pandas) or openpyxl at import
time. xarray moved into get_root_dims, openpyxl into the Excel output path.
Module-level h5py and netCDF4 imports are untouched, so the #363
import-order question is unaffected.

Local test steps

  • uv run pytest: 26 passed, 2 integration deselected (Python 3.11, macOS)
  • uv run ruff check ncompare: clean
  • python -X importtime -c "import ncompare": 214 ms to 58 ms warm;
    fixture CLI run 0.47 s to 0.23 s
  • New regression test asserts xarray and openpyxl stay out of sys.modules
    after import ncompare (runs in a fresh interpreter, since the suite
    imports xarray)

Overview of integration done

Golden-file tests cover text, CSV, and XLSX output, so both deferred paths
run end to end and pass.

PR Acceptance Checklist

  • Unit tests added/updated and passing.
  • Integration testing (CI runs the Earthdata suite)
  • CHANGELOG.md updated (entries added at release time)
  • Documentation updated (if needed).

📚 Documentation preview 📚: https://ncompare--375.org.readthedocs.build/en/375/

xarray now imports inside get_root_dims, its only use site; openpyxl
imports moved into the Excel output path. Closes nasa#374.
Copilot AI lite review requested due to automatic review settings September 19, 2026 13:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved review issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Defers heavy xarray and openpyxl imports to reduce CLI startup time.

Changes:

  • Lazily imports xarray for dimension inspection.
  • Lazily imports openpyxl for Excel output.
  • Adds a subprocess regression test for import behavior.
File Description
tests/​test_imports.py Verifies deferred imports remain unloaded.
ncompare/​printing.py Defers Excel dependencies.
ncompare/​getters.py Defers the xarray dependency.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Fixes the W292 finding from the pre-commit ruff hook.

This branch has not been deployed

No deployments
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.

2 participants