feat: add type hints to paddleocr/ public API #17805
Open
scyyh11 wants to merge 4 commits intoPaddlePaddle:mainfrom
Open
feat: add type hints to paddleocr/ public API #17805scyyh11 wants to merge 4 commits intoPaddlePaddle:mainfrom
scyyh11 wants to merge 4 commits intoPaddlePaddle:mainfrom
Conversation
|
Thanks for your contribution! |
e70c90e to
54aef7a
Compare
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
Bump python_version to 3.9 (mypy dropped 3.8 support) and add PIL to ignore_missing_imports.
54aef7a to
a96572a
Compare
| super().__init__(*args, **kwargs) | ||
|
|
||
| def _get_extra_paddlex_predictor_init_args(self): | ||
| def _get_extra_paddlex_predictor_init_args(self) -> dict[str, Any]: |
Member
There was a problem hiding this comment.
PaddleOCR要求Python 3.8及以上版本,而在py3.8中dict[str, Any]这样的语法是不被支持的,可能需要统一改造下
Collaborator
Author
There was a problem hiding this comment.
这里我用了 from __future__ import annotations 预防了这个问题
Member
There was a problem hiding this comment.
我理解这个主要是会导致type hint的lazy evaluation,从而使运行时import模块不会报错,但如果用户在python 3.8手动去eval这些type hint的话,还是会报错的。可以评估一下
paddleocr/_pipelines/paddleocr_vl.py
Outdated
| pipeline_version: str = _DEFAULT_PIPELINE_VERSION, | ||
| layout_detection_model_name: str | None = None, | ||
| layout_detection_model_dir: str | None = None, | ||
| layout_threshold: float | None = None, |
Member
There was a problem hiding this comment.
部分参数可能存在类型不够确切的问题。比如这个参数应该还支持dict。需要以文档中的描述为准添加type hints
Collaborator
Author
|
PaddleX 目前没有 py.typed 或类型存根,DetResult、OCRResult 等内部类型无法用于外部类型检查。本 PR 对这类参数统一标注为 Any,返回值使用 PredictResult = Dict[str, Any]。其余参数类型与 PaddleX 签名对齐。 |
- Widen layout param types (threshold, unclip_ratio, merge_bboxes_mode) to match paddlex per-pipeline signatures - Fix DocUnderstanding.predict input type: InputType → dict - Remove dict from layout_unclip_ratio in formula/seal pipelines where paddlex doesn't accept it - Use bare dict/tuple to match paddlex exactly, no refinement
506091c to
1f69d6a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
paddleocr/package (42 files) for IDE autocompletion and static type checkingpy.typedmarker and shared_types.pymodule with reusable type aliases (ImageInput,InputType,PredictResult)pyproject.toml(Python 3.8 compatible viafrom __future__ import annotations) and CI check in codestyle workflowDetails
X | Noneunion syntax everywhere, enabled byfrom __future__ import annotationsSuccess: no issues found in 46 source filesPredictResultwithTypedDictTest plan
mypy paddleocr/passes with zero errorspre-commit run --all-filespasses