Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
refactor: remove unused Hybrid model imports and improve type checking for PDFDocument
myhloli0b39865fa59f
diff --git a/mineru/model/runtime/__init__.py b/mineru/model/runtime/__init__.pyindex ab461706..73e6634c 100644--- a/mineru/model/runtime/__init__.py+++ b/mineru/model/runtime/__init__.py@@ -3,13 +3,10 @@from .contracts import AtomicModelNamefrom .device import get_device, get_model_stack-from .hybrid import HybridLocalModelContext, HybridLocalModelContextSingletonfrom .memory import clean_memory, get_vram__all__ = ["AtomicModelName",- "HybridLocalModelContext",- "HybridLocalModelContextSingleton","clean_memory","get_device","get_model_stack",diff --git a/mineru/parser/base.py b/mineru/parser/base.pyindex 844ab21f..eada59f3 100644--- a/mineru/parser/base.py+++ b/mineru/parser/base.py@@ -6,14 +6,16 @@ from abc import ABC, abstractmethodfrom copy import deepcopyfrom dataclasses import dataclassfrom pathlib import Path-from typing import Any, Literal+from typing import TYPE_CHECKING, Any, Literalfrom ..render import render_markdown, render_structured_contentfrom ..render.contracts import ImageRenderer, RenderMode-from .writer import DataWriterfrom ..types import FileSuffix, MiddleJson, PageInfofrom ..utils.image_payload import ImagePayloadCache-from ..model.flash.pdf.document import PDFDocument+from .writer import DataWriter++if TYPE_CHECKING:+ from ..model.flash.pdf.document import PDFDocumentMIDDLE_JSON_SCHEMA_VERSION: str = "2.0"_LEGACY_SCHEMA_VERSION: str = "1.0"diff --git a/tests/unittest/test_parser_api_contract.py b/tests/unittest/test_parser_api_contract.pyindex ab502939..860bae47 100644--- a/tests/unittest/test_parser_api_contract.py+++ b/tests/unittest/test_parser_api_contract.py@@ -119,6 +119,80 @@ print("ok")assert result.stdout.strip() == "ok"+def test_dependency_preflight_does_not_load_hybrid_runtime_without_torch() -> None:+ """校验 light stack 的依赖预检不会在检查前加载 Hybrid 与 Torch。"""+ repo_root = Path(__file__).resolve().parents[2]+ code = """+import importlib.abc+import os+import sys++os.environ["MINERU_MODEL_STACK"] = "light"+++class BlockTorchFinder(importlib.abc.MetaPathFinder):+ def find_spec(self, fullname, path=None, target=None):+ # 阻断 Torch 导入,验证依赖预检只加载轻量设备选择模块。+ if fullname == "torch" or fullname.startswith("torch."):+ raise ModuleNotFoundError(f"blocked Torch import: {fullname}", name=fullname)+ return None+++sys.meta_path.insert(0, BlockTorchFinder())+from mineru.parser.tier import required_modules_for_tier++assert required_modules_for_tier("basic") == []+assert "mineru.model.runtime.hybrid" not in sys.modules+print("ok")+"""++ result = subprocess.run(+ [sys.executable, "-c", code],+ cwd=repo_root,+ capture_output=True,+ text=True,+ check=False,+ )++ assert result.returncode == 0, result.stderr+ assert result.stdout.strip() == "ok"+++def test_public_parser_import_does_not_load_opencv() -> None:+ """校验公共 parser 导入在无 libGL 的 headless 环境中不加载 OpenCV。"""+ repo_root = Path(__file__).resolve().parents[2]+ code = """+import importlib.abc+import sys+++class BlockCv2Finder(importlib.abc.MetaPathFinder):+ def find_spec(self, fullname, path=None, target=None):+ # 模拟 headless Linux 缺少 libGL 时 cv2 的导入错误。+ if fullname == "cv2" or fullname.startswith("cv2."):+ raise ImportError("libGL.so.1: cannot open shared object file: No such file or directory")+ return None+++sys.meta_path.insert(0, BlockCv2Finder())+import mineru.parser++assert "cv2" not in sys.modules+print("ok")+"""++ result = subprocess.run(+ [sys.executable, "-c", code],+ cwd=repo_root,+ capture_output=True,+ text=True,+ check=False,+ )++ assert result.returncode == 0, result.stderr+ assert result.stdout.strip() == "ok"++def test_hybrid_local_runtime_uses_context_names_and_local_lock_env() -> None:"""校验 Hybrid 本地模型运行时只暴露 context 命名,并优先读取新的本地锁环境变量。"""repo_root = Path(__file__).resolve().parents[2]