Skip to content

Commit 02f5ec8

Browse files
committed
feat: add type hints to paddleocr/ public API (Phase 1)
Add comprehensive type annotations across the entire paddleocr/ package for IDE autocompletion and static type checking support. - Add PEP 561 py.typed marker and shared _types.py (ImageInput, InputType, PredictResult) - Annotate all model base classes, mixins, and 13 model subclasses - Annotate all pipeline base, utils, and 10 pipeline subclasses - Annotate utility modules (_utils/cli, deprecation, logging) and core modules - Add mypy configuration in pyproject.toml (Python 3.8 compat via `from __future__ import annotations`) - Add mypy type check step to CI codestyle workflow
1 parent fed730d commit 02f5ec8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1251
-1008
lines changed

.github/workflows/codestyle.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ jobs:
3434
- uses: pre-commit/action@v3.0.1
3535
with:
3636
extra_args: '--all-files'
37+
38+
- name: Type check with mypy
39+
run: |
40+
pip install mypy numpy
41+
mypy paddleocr/

paddleocr/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from ._cli import main
1919

2020

21-
def console_entry() -> int:
21+
def console_entry() -> None:
2222
# See https://docs.python.org/3/library/signal.html#note-on-sigpipe
2323
try:
2424
# Flush output here to force SIGPIPE to be triggered while inside this

paddleocr/_abstract.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
18+
import argparse
19+
from typing import Any
1620

1721

1822
class CLISubcommandExecutor(metaclass=abc.ABCMeta):
1923
@abc.abstractmethod
20-
def add_subparser(self, subparsers):
24+
def add_subparser(self, subparsers: argparse._SubParsersAction) -> argparse.ArgumentParser:
2125
raise NotImplementedError
2226

2327
@abc.abstractmethod
24-
def execute_with_args(self, args):
28+
def execute_with_args(self, args: argparse.Namespace) -> None:
2529
raise NotImplementedError

paddleocr/_common_args.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
17+
import argparse
18+
from typing import Any
19+
1520
from paddlex.inference import PaddlePredictorOption
1621
from paddlex.utils.device import get_default_device, parse_device
1722

@@ -28,7 +33,7 @@
2833
from ._utils.cli import str2bool
2934

3035

31-
def parse_common_args(kwargs, *, default_enable_hpi):
36+
def parse_common_args(kwargs: dict[str, Any], *, default_enable_hpi: bool | None) -> dict[str, Any]:
3237
default_vals = {
3338
"device": DEFAULT_DEVICE,
3439
"enable_hpi": default_enable_hpi,
@@ -57,13 +62,13 @@ def parse_common_args(kwargs, *, default_enable_hpi):
5762
return kwargs
5863

5964

60-
def prepare_common_init_args(model_name, common_args):
65+
def prepare_common_init_args(model_name: str | None, common_args: dict[str, Any]) -> dict[str, Any]:
6166
device = common_args["device"]
6267
if device is None:
6368
device = get_default_device()
6469
device_type, _ = parse_device(device)
6570

66-
init_kwargs = {}
71+
init_kwargs: dict[str, Any] = {}
6772
init_kwargs["device"] = device
6873
init_kwargs["use_hpip"] = common_args["enable_hpi"]
6974

@@ -94,7 +99,7 @@ def prepare_common_init_args(model_name, common_args):
9499
return init_kwargs
95100

96101

97-
def add_common_cli_opts(parser, *, default_enable_hpi, allow_multiple_devices):
102+
def add_common_cli_opts(parser: argparse.ArgumentParser, *, default_enable_hpi: bool | None, allow_multiple_devices: bool) -> None:
98103
if allow_multiple_devices:
99104
help_ = "Device(s) to use for inference, e.g., `cpu`, `gpu`, `npu`, `gpu:0`, `gpu:0,1`. If multiple devices are specified, inference will be performed in parallel. Note that parallel inference is not always supported. By default, GPU 0 will be used if available; otherwise, the CPU will be used."
100105
else:

paddleocr/_constants.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
DEFAULT_DEVICE = None
16-
DEFAULT_USE_TENSORRT = False
17-
DEFAULT_PRECISION = "fp32"
18-
DEFAULT_ENABLE_MKLDNN = True
19-
DEFAULT_MKLDNN_CACHE_CAPACITY = 10
20-
DEFAULT_CPU_THREADS = 10
21-
SUPPORTED_PRECISION_LIST = ["fp32", "fp16"]
22-
DEFAULT_USE_CINN = False
15+
from __future__ import annotations
16+
17+
DEFAULT_DEVICE: str | None = None
18+
DEFAULT_USE_TENSORRT: bool = False
19+
DEFAULT_PRECISION: str = "fp32"
20+
DEFAULT_ENABLE_MKLDNN: bool = True
21+
DEFAULT_MKLDNN_CACHE_CAPACITY: int = 10
22+
DEFAULT_CPU_THREADS: int = 10
23+
SUPPORTED_PRECISION_LIST: list[str] = ["fp32", "fp16"]
24+
DEFAULT_USE_CINN: bool = False

paddleocr/_env.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import os
1618

17-
DISABLE_AUTO_LOGGING_CONFIG = (
19+
DISABLE_AUTO_LOGGING_CONFIG: bool = (
1820
os.getenv("PADDLEOCR_DISABLE_AUTO_LOGGING_CONFIG", "0") == "1"
1921
)

paddleocr/_models/_doc_vlm.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
18+
import argparse
19+
from typing import Any
1620

1721
from .._utils.cli import (
1822
get_subcommand_args,
@@ -25,13 +29,13 @@
2529
class BaseDocVLM(PaddleXPredictorWrapper):
2630
def __init__(
2731
self,
28-
*args,
29-
**kwargs,
30-
):
31-
self._extra_init_args = {}
32+
*args: Any,
33+
**kwargs: Any,
34+
) -> None:
35+
self._extra_init_args: dict[str, Any] = {}
3236
super().__init__(*args, **kwargs)
3337

34-
def _get_extra_paddlex_predictor_init_args(self):
38+
def _get_extra_paddlex_predictor_init_args(self) -> dict[str, Any]:
3539
return self._extra_init_args
3640

3741

@@ -40,10 +44,10 @@ class BaseDocVLMSubcommandExecutor(PredictorCLISubcommandExecutor):
4044

4145
@property
4246
@abc.abstractmethod
43-
def wrapper_cls(self):
47+
def wrapper_cls(self) -> type[PaddleXPredictorWrapper]:
4448
raise NotImplementedError
4549

46-
def execute_with_args(self, args):
50+
def execute_with_args(self, args: argparse.Namespace) -> None:
4751
params = get_subcommand_args(args)
4852
params["input"] = self.input_validator(params["input"])
4953
perform_simple_inference(self.wrapper_cls, params)

paddleocr/_models/_image_classification.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
18+
import argparse
19+
from typing import Any
1620

1721
from .._utils.cli import (
1822
add_simple_inference_args,
@@ -26,20 +30,20 @@ class ImageClassification(PaddleXPredictorWrapper):
2630
def __init__(
2731
self,
2832
*,
29-
topk=None,
30-
**kwargs,
31-
):
33+
topk: int | None = None,
34+
**kwargs: Any,
35+
) -> None:
3236
self._extra_init_args = {
3337
"topk": topk,
3438
}
3539
super().__init__(**kwargs)
3640

37-
def _get_extra_paddlex_predictor_init_args(self):
41+
def _get_extra_paddlex_predictor_init_args(self) -> dict[str, Any]:
3842
return self._extra_init_args
3943

4044

4145
class ImageClassificationSubcommandExecutor(PredictorCLISubcommandExecutor):
42-
def _update_subparser(self, subparser):
46+
def _update_subparser(self, subparser: argparse.ArgumentParser) -> None:
4347
add_simple_inference_args(subparser)
4448

4549
subparser.add_argument(
@@ -50,9 +54,9 @@ def _update_subparser(self, subparser):
5054

5155
@property
5256
@abc.abstractmethod
53-
def wrapper_cls(self):
57+
def wrapper_cls(self) -> type[PaddleXPredictorWrapper]:
5458
raise NotImplementedError
5559

56-
def execute_with_args(self, args):
60+
def execute_with_args(self, args: argparse.Namespace) -> None:
5761
params = get_subcommand_args(args)
5862
perform_simple_inference(self.wrapper_cls, params)

paddleocr/_models/_object_detection.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
18+
import argparse
19+
from typing import Any
1620

1721
from .._utils.cli import (
1822
add_simple_inference_args,
@@ -27,13 +31,13 @@ class ObjectDetection(PaddleXPredictorWrapper):
2731
def __init__(
2832
self,
2933
*,
30-
img_size=None,
31-
threshold=None,
32-
layout_nms=None,
33-
layout_unclip_ratio=None,
34-
layout_merge_bboxes_mode=None,
35-
**kwargs,
36-
):
34+
img_size: int | None = None,
35+
threshold: float | None = None,
36+
layout_nms: bool | None = None,
37+
layout_unclip_ratio: float | None = None,
38+
layout_merge_bboxes_mode: str | None = None,
39+
**kwargs: Any,
40+
) -> None:
3741
self._extra_init_args = {
3842
"img_size": img_size,
3943
"threshold": threshold,
@@ -43,12 +47,12 @@ def __init__(
4347
}
4448
super().__init__(**kwargs)
4549

46-
def _get_extra_paddlex_predictor_init_args(self):
50+
def _get_extra_paddlex_predictor_init_args(self) -> dict[str, Any]:
4751
return self._extra_init_args
4852

4953

5054
class ObjectDetectionSubcommandExecutor(PredictorCLISubcommandExecutor):
51-
def _update_subparser(self, subparser):
55+
def _update_subparser(self, subparser: argparse.ArgumentParser) -> None:
5256
add_simple_inference_args(subparser)
5357

5458
subparser.add_argument(
@@ -79,9 +83,9 @@ def _update_subparser(self, subparser):
7983

8084
@property
8185
@abc.abstractmethod
82-
def wrapper_cls(self):
86+
def wrapper_cls(self) -> type[PaddleXPredictorWrapper]:
8387
raise NotImplementedError
8488

85-
def execute_with_args(self, args):
89+
def execute_with_args(self, args: argparse.Namespace) -> None:
8690
params = get_subcommand_args(args)
8791
perform_simple_inference(self.wrapper_cls, params)

paddleocr/_models/_text_detection.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
17+
import argparse
18+
from typing import Any
19+
1520

1621
class TextDetectionMixin:
1722
def __init__(
1823
self,
1924
*,
20-
limit_side_len=None,
21-
limit_type=None,
22-
thresh=None,
23-
box_thresh=None,
24-
unclip_ratio=None,
25-
input_shape=None,
26-
**kwargs,
27-
):
25+
limit_side_len: int | None = None,
26+
limit_type: str | None = None,
27+
thresh: float | None = None,
28+
box_thresh: float | None = None,
29+
unclip_ratio: float | None = None,
30+
input_shape: tuple[int, int, int] | None = None,
31+
**kwargs: Any,
32+
) -> None:
2833
self._extra_init_args = {
2934
"limit_side_len": limit_side_len,
3035
"limit_type": limit_type,
@@ -35,12 +40,12 @@ def __init__(
3540
}
3641
super().__init__(**kwargs)
3742

38-
def _get_extra_paddlex_predictor_init_args(self):
43+
def _get_extra_paddlex_predictor_init_args(self) -> dict[str, Any]:
3944
return self._extra_init_args
4045

4146

4247
class TextDetectionSubcommandExecutorMixin:
43-
def _add_text_detection_args(self, subparser):
48+
def _add_text_detection_args(self, subparser: argparse.ArgumentParser) -> None:
4449
subparser.add_argument(
4550
"--limit_side_len",
4651
type=int,

0 commit comments

Comments
 (0)