Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

fix: improve IDE support for lazy package exports

SWHL096293330eb0
diff --git a/python/pyproject.toml b/python/pyproject.toml
index 695cb11..2b1f055 100644
--- a/python/pyproject.toml
+++ b/python/pyproject.toml
@@ -58,6 +58,8 @@ namespaces = false
[tool.setuptools.package-data]
rapidocr = [
"**/*.yaml",
+ "**/*.pyi",
+ "py.typed",
]
[tool.setuptools_scm]
diff --git a/python/rapidocr/__init__.py b/python/rapidocr/__init__.py
index cc622c6..c91c283 100644
--- a/python/rapidocr/__init__.py
+++ b/python/rapidocr/__init__.py
@@ -1,31 +1,34 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
-from .utils.typings import EngineType, LangCls, LangDet, LangRec, ModelType, OCRVersion
-
-
-def __getattr__(name):
- if name == "RapidOCR":
- from .main import RapidOCR
+from importlib import import_module
+from typing import TYPE_CHECKING
- return RapidOCR
+from .utils.typings import EngineType, LangCls, LangDet, LangRec, ModelType, OCRVersion
- if name == "LoadImageError":
- from .utils.load_image import LoadImageError
+if TYPE_CHECKING:
+ from .main import RapidOCR
+ from .utils.download_models import download_models
+ from .utils.load_image import LoadImageError
+ from .utils.vis_res import VisRes
- return LoadImageError
+_LAZY_IMPORTS = {
+ "RapidOCR": ".main",
+ "LoadImageError": ".utils.load_image",
+ "VisRes": ".utils.vis_res",
+ "download_models": ".utils.download_models",
+}
- if name == "VisRes":
- from .utils.vis_res import VisRes
- return VisRes
+def __getattr__(name):
+ if name in _LAZY_IMPORTS:
+ return getattr(import_module(_LAZY_IMPORTS[name], __name__), name)
- if name == "download_models":
- from .utils.download_models import download_models
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
- return download_models
- raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+def __dir__():
+ return sorted([*globals(), *__all__])
__all__ = [
diff --git a/python/rapidocr/__init__.pyi b/python/rapidocr/__init__.pyi
new file mode 100644
index 0000000..21c8988
--- /dev/null
+++ b/python/rapidocr/__init__.pyi
@@ -0,0 +1,14 @@
+from typing import List
+
+from .main import RapidOCR as RapidOCR
+from .utils.download_models import download_models as download_models
+from .utils.load_image import LoadImageError as LoadImageError
+from .utils.typings import EngineType as EngineType
+from .utils.typings import LangCls as LangCls
+from .utils.typings import LangDet as LangDet
+from .utils.typings import LangRec as LangRec
+from .utils.typings import ModelType as ModelType
+from .utils.typings import OCRVersion as OCRVersion
+from .utils.vis_res import VisRes as VisRes
+
+__all__: List[str]
diff --git a/python/rapidocr/py.typed b/python/rapidocr/py.typed
new file mode 100644
index 0000000..e69de29