Skip to content

Commit 4537376

Browse files
committed
gen/gen_mpy: Add Python stub file generation for IDE support.
Extends the LVGL bindings generator to automatically create Python stub files (.pyi) that provide type hints and enable IDE autocompletion for LVGL MicroPython bindings. Features: - Type mapping from C types to Python type hints - Single stub file with all widgets, functions, enums, and constants - Detailed documentation header with content statistics The generated lvgl.pyi file includes 40+ widget classes, 300+ functions, and comprehensive type information for better development experience. Include C function names, location and description in Python stub docstrings. The docstrings include a "C function: <name>" line before the "Source: <file>:<line>" reference. For example: - Python method `delete()` shows "C function: lv_obj_delete" - Python method `add_flag()` shows "C function: lv_obj_add_flag" Create a proper Python package structure for LVGL type stubs: - Create stubs/pyproject.toml with setuptools-scm versioning - Add stubs/lvgl-stubs/ package directory with __init__.py and py.typed - Update gen_mpy.py to output stubs to package directory by default - Add .gitignore to exclude generated .pyi files but include package structure - Update README.md with comprehensive IDE support documentation Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 44f70b1 commit 4537376

File tree

9 files changed

+886
-2
lines changed

9 files changed

+886
-2
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ yacctab.py
66
*.bak
77
.history
88
.vscode
9+
10+
# Python cache files
11+
__pycache__/
12+
*.pyc
13+
*.pyo
14+
*.pyd
15+
16+
# Build artifacts
17+
build/
18+
*.pp
19+
*.pp.c
20+
21+
# Test files
22+
test_*/

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,66 @@ print('\n'.join(dir(lvgl.btn)))
507507
...
508508
```
509509

510+
## IDE Support and Type Stubs
511+
512+
For better development experience with IDEs (VS Code, PyCharm, etc.), this repository includes Python type stubs that provide autocompletion, type checking, and documentation hints.
513+
514+
### Installation
515+
516+
The type stubs are automatically generated during the build process and packaged for easy installation:
517+
518+
#### Development Installation (recommended)
519+
520+
For development with the latest stubs:
521+
522+
```bash
523+
pip install -e /path/to/lv_binding_micropython/stubs
524+
```
525+
526+
#### From Built Package
527+
528+
After building and packaging:
529+
530+
```bash
531+
pip install lvgl-stubs
532+
```
533+
534+
### Features
535+
536+
Once installed, your IDE will automatically provide:
537+
538+
- **Autocompletion** for all LVGL functions, methods, and properties
539+
- **Type checking** with mypy and other type checkers
540+
- **Function signatures** with parameter names and types
541+
- **Documentation strings** extracted from LVGL C headers
542+
- **Source location references** to help navigate LVGL documentation
543+
544+
### Building Stubs
545+
546+
The stubs are automatically generated when running the binding generation:
547+
548+
```bash
549+
# Build with automatic stub generation
550+
make USER_C_MODULES=/path/to/lv_binding_micropython/micropython.cmake
551+
552+
# Or generate stubs manually
553+
cd gen
554+
python gen_mpy.py --stubs /path/to/output/dir [other options...]
555+
```
556+
557+
The generated `lvgl.pyi` stub file contains type definitions for:
558+
- All LVGL widget classes (buttons, labels, sliders, etc.)
559+
- Module-level functions and constants
560+
- Enum definitions with proper typing
561+
- Struct types with documented fields
562+
- Callback function signatures
563+
564+
### Package Structure
565+
566+
The stubs are packaged in `stubs/` directory with:
567+
- `pyproject.toml` - Package configuration with setuptools-scm versioning
568+
- `lvgl-stubs/` - Python package containing type stubs
569+
- `lvgl-stubs/__init__.py` - Package initialization
570+
- `lvgl-stubs/py.typed` - Marks package as typed
571+
- `lvgl-stubs/lvgl.pyi` - Generated type stubs (git-ignored)
572+

0 commit comments

Comments
 (0)