refactor: drop the __init__ lazy-export machinery, superseded by 41f4a0a

Once litellm/PyPDF2 moved inside the functions that use them, the
legacy modules became cheap to import — the PEP 562 __getattr__ table
in __init__ was guarding a door that no longer needs guarding. Restore
plain eager imports (~0.3s total, still zero heavy deps at import);
keep only the small submodule __getattr__ for pageindex.utils /
pageindex.page_index_md attribute access.
This commit is contained in:
Ray 2026-07-24 03:16:30 +08:00
parent 41f4a0a6f5
commit 15b0b18857

View file

@ -9,15 +9,13 @@ _chatgpt_key = _os.getenv("CHATGPT_API_KEY")
if not _os.getenv("OPENAI_API_KEY") and _chatgpt_key:
_os.environ["OPENAI_API_KEY"] = _chatgpt_key
from typing import TYPE_CHECKING as _TYPE_CHECKING
if _TYPE_CHECKING:
# Static-only bindings for the lazy legacy names — real signatures for IDEs.
from .index.page_index import page_index, page_index_main, tree_parser
from .index.page_index_md import md_to_tree
from .index.utils import ConfigLoader, llm_completion, llm_acompletion
from .retrieve import get_document, get_document_structure, get_page_content
# Legacy (pre-SDK) exports. Cheap to import eagerly: litellm/PyPDF2 load
# inside the functions that use them, not at module import.
from .index.page_index import * # noqa: E402
from .index.page_index_md import md_to_tree
from .retrieve import get_document, get_document_structure, get_page_content
# SDK exports — must stay light: no LLM/indexing imports here.
# SDK exports
from .client import PageIndexClient, LocalClient, CloudClient
from .config import IndexConfig, set_llm_params
from .collection import Collection
@ -71,37 +69,11 @@ __all__ = [
"get_page_content",
]
# Legacy (pre-SDK) exports resolve lazily (PEP 562) so cloud-only
# `import pageindex` never pays for litellm/PyPDF2.
_LAZY_LEGACY = {
"md_to_tree": ".index.page_index_md",
"get_document": ".retrieve",
"get_document_structure": ".retrieve",
"get_page_content": ".retrieve",
}
def __getattr__(name):
if name.startswith("_"):
# dunder probes (pickle, IPython) must not trigger the heavy import
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
import importlib
# `pageindex.utils` / `pageindex.page_index_md` attribute access without an
# explicit submodule import.
if name in ("utils", "page_index_md"):
import importlib
return importlib.import_module(f".{name}", __name__)
module = _LAZY_LEGACY.get(name)
if module is not None:
value = getattr(importlib.import_module(module, __name__), name)
globals()[name] = value
return value
# Remaining pre-SDK names live in index.page_index (star-exports index.utils).
legacy = importlib.import_module(".index.page_index", __name__)
try:
value = getattr(legacy, name)
except AttributeError:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from None
globals()[name] = value
return value
def __dir__():
return sorted(set(globals()) | set(__all__) | {"utils", "page_index_md"})
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")