edgartools icon indicating copy to clipboard operation
edgartools copied to clipboard

6.0: import-time reduction via lazy submodule loading (PEP 562)

Open dgunning opened this issue 4 months ago • 3 comments

Why

import edgar costs roughly 0.9s of CPU today because the package eagerly imports its whole world. For CLI tools, serverless functions, and short scripts, that's the single biggest fixed cost of using the library.

Scope

PEP 562 module-level __getattr__ for lazy submodule loading: the top-level namespace keeps its ergonomic surface (from edgar import Company, find, obj), but heavy subsystems (XBRL, documents, funds, AI tooling) load on first touch instead of at import.

Design-first, because lazy loading has sharp edges: it must not break type-checker resolution, IDE completion, pickling, or multiprocessing spawn. It lands after the public-API declaration, so laziness is applied to a defined surface rather than an implicit one, and only if the design review is clean.

Acceptance

  • Measured import-time reduction against the profiling baseline (which records the precise current number).
  • mypy/pyright still resolve all public names; IDE completion unaffected.
  • No behavior change beyond timing — verified by the full test suite and an import-order stress test.

dgunning avatar Jul 31 '26 19:07 dgunning

Status: not started, and there is now a reason to keep it that way for a while.

The rough ~0.9 s in the description has been replaced by a proper baseline: 788 ms, recorded in engineering/analysis/perf-baseline/timings.json alongside the rest of the 6.0 performance work. Re-measured today at 5.52.0, it is still ~790 ms. Nothing has regressed and nothing has improved.

The useful finding is where some of that time goes. python -X importtime -c "import edgar" shows bs4 being imported eagerly, through this chain:

edgar → edgar._filings → edgar._markdown → edgar.files.html_documents
      → edgar.files → edgar.files.page_breaks → bs4      (~48 ms)

edgar/files/__init__.py imports page_breaks at module level, and page_breaks imports bs4 at module level, so every user of the library pays for the legacy HTML parser's dependency on import, whether or not they ever touch it.

That ~48 ms goes away when #930 removes edgar.files, with no lazy-loading machinery involved. pandas (~187 ms) is the other large block and is genuinely load-bearing.

So the sequencing stands as written: this stays design-first, and behind #930 and #932. Two reasons, both unchanged and both reinforced:

  1. Lazy-loading an undeclared surface is a breakage generator, which is why this depends on #932. __all__ has shipped now, so that half of the dependency is satisfied.
  2. PEP 562 __getattr__ moves ImportError from load time to use time. That is a real downgrade in error quality unless the messages are deliberate, and it is the reason this issue says "commit only if the spike is clean."

The honest position for the roadmap: this is a P2, it is unstarted, and part of what it was going to buy arrives via another issue anyway. It is a candidate for slipping past 6.0 if the window gets tight — nothing about it is breaking, so it can ship in any 6.x.

dgunning avatar Aug 23 '26 19:08 dgunning

Lazy submodule loading is design-first work and moves to after 6.0. The import deadlock in #1325 is tracked against it.

dgunning avatar Oct 08 '26 20:10 dgunning

This shipped in 6.1.0 with #1911, so I'm closing it.

edgar/__init__.py now serves every top-level name through a module __getattr__, so from edgar import Company, find, obj works as before, but nothing heavy loads until you touch it. On 6.2.0, import edgar takes about 8 ms, down from the 788 ms in the perf baseline. After a bare import edgar, pandas, edgar.xbrl and edgar.documents are no longer in sys.modules.

On the sharp edges the issue listed, the public names are declared again under a TYPE_CHECKING block, so type checkers and IDE completion still resolve them, and __dir__ lists the full surface. The threaded-import deadlock in #1325 was fixed in the same batch and is covered by tests/issues/regression/test_issue_1325.py and test_issue_1325_import_ordering.py.

The first call into a subsystem now pays the cost the import used to pay up front, so a script that goes straight to company.get_financials() won't run much faster overall. CLI tools, serverless functions and anything that only needs part of the library will.

dgunning avatar Nov 22 '26 21:11 dgunning