Skip to content

Commit 0c35d96

Browse files
p-wysockialmilosz
andauthored
[PyOV] Add Python stub files developer documentation (#29654)
Related to #29139 ### Tickets: - CVS-163067 --------- Signed-off-by: p-wysocki <[email protected]> Co-authored-by: Alicja Miloszewska <[email protected]>
1 parent ab21a9d commit 0c35d96

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/bindings/python/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ If you want to contribute to OpenVINO Python API, here is the list of learning m
3636
* [How to extend OpenVINO Python API](./docs/code_examples.md)
3737
* [How to test OpenVINO Python API](./docs/test_examples.md)
3838
* [How to upgrade local Python version](./docs/python_version_upgrade.md)
39+
* [How we manage stub .pyi files](./docs/stubs.md)
3940

4041
## See also
4142

src/bindings/python/docs/stubs.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Python Stub Files in OpenVINO
2+
3+
## What are stub `.pyi` files?
4+
5+
Stub files (`.pyi`) are used to provide type hints for Python code. They describe the structure of a Python module, including its classes, methods, and functions, without containing any actual implementation. Stub files are particularly useful for improving code readability, enabling better autocompletion in IDEs, and supporting static type checking tools like `mypy`.
6+
7+
## Automation of stub file generation in OpenVINO
8+
9+
In OpenVINO, the generation of stub files is automated as part of the development workflow. When building the Python API for the first time, a Git pre-commit hook is installed into the OpenVINO repository's `.git` directory. The related Python dependencies are `pybind11-stubgen` for stub generation and `pre-commit` for automating git hooks.
10+
11+
### What is a git pre-commit hook?
12+
13+
A Git pre-commit hook is a script that runs automatically before a commit is finalized. It allows developers to enforce specific checks or perform automated tasks, such as code formatting, linting, or file generation, ensuring that the repository maintains a consistent and high-quality state.
14+
15+
### What happens during the pre-commit hook?
16+
17+
When the pre-commit hook is triggered, the following steps occur:
18+
19+
1. **Stub file generation**: The `pybind11-stubgen` tool is executed to generate new `.pyi` stub files for the OpenVINO Python API. This tool uses the Python package `openvino` to extract type information and create the stub files, so it's important that currently installed OpenVINO version contains all changes related to your Pull Request.
20+
2. **Automatic addition to commit**: The newly generated `.pyi` files are automatically staged and added to the current commit. This ensures that the stub files are always up-to-date with the latest changes in the Python API.
21+
22+
### Ensuring changes are installed
23+
24+
To ensure that `pybind11-stubgen` works correctly, the `openvino` Python package must include your latest changes. This can be achieved by:
25+
26+
- Setting the `PYTHONPATH` environment variable to point to your local OpenVINO build directory. For example:
27+
28+
```bash
29+
export PYTHONPATH=$PYTHONPATH:/home/pwysocki/openvino/bin/intel64/Release/python
30+
```
31+
32+
- Installing an up-to-date wheel containing your changes using `pip install`.
33+
34+
### Skipping the pre-commit hook
35+
36+
If you need to skip the pre-commit hook for any reason, you can do so by setting the `SKIP` environment variable before running the `git commit` command:
37+
38+
```bash
39+
SKIP=generate_stubs git commit -m "Your commit message"
40+
```
41+
42+
This bypasses the stub file generation process, allowing you to commit without triggering the hook.
43+
44+
### Uninstalling the pre-commit hook
45+
46+
If you wish to remove the hook, you can run:
47+
48+
```bash
49+
pre-commit uninstall
50+
```
51+
52+
## See also
53+
54+
 * [OpenVINO™ README](../../../../README.md)
55+
 * [OpenVINO™ bindings README](../../README.md)
56+
 * [Developer documentation](../../../../docs/dev/index.md)

0 commit comments

Comments
 (0)