Skip to content

Add Python stub file generation with documentation parsing #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

Summary

This PR adds comprehensive Python stub file (.pyi) generation for the LVGL MicroPython bindings, providing full IDE support with autocompletion, type hints, and rich documentation.

Key Features:

  • 🚀 Fast Parallel Processing: 6 seconds vs. minutes (uses all CPU cores)
  • 📝 Rich Documentation: Automatic extraction from 1400+ LVGL functions
  • 🎯 IDE Integration: Full autocompletion and type hints (.pyi files)
  • Separate Build: Doesn't slow down main MicroPython builds
  • 🔧 Smart Formatting: Bullet points, text wrapping, proper Python conventions
  • 🔗 Source Navigation: File:line references to original C implementation

The implementation includes:

  1. Stub Generation: Creates .pyi files with proper Python type hints
  2. Documentation Parsing: Extracts Doxygen comments from C headers using parallel processing
  3. Smart Parameter Handling: Converts obj to self for class methods
  4. Performance Optimization: Processes 209 header files in ~6 seconds using all CPU cores
  5. Source References: Adds file:line references for navigation to C implementation

Testing

Tested on Unix port with full stub generation:

  • Processes 209 LVGL header files using parallel processing
  • Extracts documentation from 1423 functions
  • Generates type hints for 41 widget classes and 64 enums
  • Produces comprehensive .pyi files for IDE consumption

The generated stubs provide full autocompletion and documentation in modern Python IDEs like VS Code, PyCharm, etc.

Trade-offs and Alternatives

Trade-offs:

  • Adds ~6 seconds to generate full documentation (but as separate optional target)
  • Increases repository size slightly with documentation files

Alternatives considered:

  • External documentation parsing libraries (rejected to minimize dependencies)
  • Manual stub file maintenance (rejected due to maintenance burden)
  • No documentation extraction (rejected as it provides significant developer value)

The implementation uses custom regex-based Doxygen parsing to avoid external dependencies while providing exactly the functionality needed for LVGL's documentation format.

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

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:
- Comprehensive 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

The stubs package can be installed with 'pip install -e stubs/' for
development or built into a wheel for distribution. Generated stub files
include complete type definitions for all LVGL widgets, functions, and
enums with extracted documentation.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
@andrewleech andrewleech force-pushed the python-stub-generation branch from f72bde8 to 32986d0 Compare June 10, 2025 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants