Fix type hinting for the Python module#1287
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1287 +/- ##
=======================================
Coverage 24.76% 24.76%
=======================================
Files 169 169
Lines 19598 19598
=======================================
Hits 4854 4854
Misses 14744 14744 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a custom stub generator for the JSBSim Python module (replacing the broken stubgen), updates the Cython source to include type hints, and adjusts CI to invoke the new generator.
- Add
pyxstubgen.pyandcython.larkto parsejsbsim.pyxand emit a proper_jsbsim.pyi - Annotate
__version__injsbsim.pyx.inand expose the generated stub in__init__.py - Update CI (
cpp-python-build.yml) to installlarkand runpyxstubgen.pyinstead ofstubgen
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| python/pyxstubgen.py | New script using Lark to parse .pyx and write stubs |
| python/jsbsim.pyx.in | Add a type annotation to the __version__ variable |
| python/cython.lark | Grammar for parsing Cython code |
| python/init.py | Import the generated _jsbsim module |
| .github/workflows/cpp-python-build.yml | Install lark and run the custom stub generator |
Comments suppressed due to low confidence (4)
python/pyxstubgen.py:1
- Use a more portable shebang for Python 3, e.g.
#!/usr/bin/env python3, to ensure the correct interpreter is used.
#! /usr/bin/python
.github/workflows/cpp-python-build.yml:466
- The
mypyinstallation was removed here; if CI or other checks still rely onmypy, consider re-adding it or moving it intorequirements.txt.
pip install build lark
.github/workflows/cpp-python-build.yml:491
- Removing
-DCYTHON_FLAGS="-X embedsignature=True"may strip embedded signatures from the Cython build. If you rely on__annotations__or signature metadata, restore this flag.
cmake -DCMAKE_INCLUDE_PATH=$PWD/../cxxtest -DBUILD_JULIA_PACKAGE=ON -DCMAKE_PREFIX_PATH=$CXXWRAP_PREFIX_PATH -DCMAKE_C_FLAGS_DEBUG="-g -O2 -fno-fast-math" -DCMAKE_CXX_FLAGS_DEBUG="-g -O2 -fno-fast-math" -DCMAKE_BUILD_TYPE=Debug ..
python/init.py:1
- [nitpick] Importing
_jsbsimas a module before importing its symbols may be redundant if you only re-export names. Consider removing the module-level import if it's unused elsewhere.
from . import _jsbsim
|
Yeah, I've launched a Copilot code review because I'm masochistic and because I don't want to look like an old fart reluctant to progress. 😉 |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The type hinting introduced by PR #755 is broken. By inspection of the stub files
*.pyiit appears that the utility toolstubgenfrom themypypackage is producing garbage for modules generated by Cython .This PR replaces the usage of
stubgenby a new Python scriptpyxstubgen.pythat parsesjsbsim.pyxto find all the classes, methods and functions declarations with their type hints and then generates a proper stub file_jsbsim.pyi.Since parsing a Cython file is no trivial job, the script
pystubgen.pyis using the lark parser (a parser toolkit fully written in Python). A grammar filecython.larkhas been created for that purpose. It is the minimum grammar that works for the JSBSim cython code and is by no means pretending to be able to read any cython file.This PR is addressing some proverbial itch scratching: I am annoyed that my favorite IDE does not autocomplete the classes and functions from the JSBSim Python module. I have run some tests that confirm that autocomplete works when the Python module is generated using the new script
pyxstubgen.py.Please note that this feature is meant to be used by our CI workflow when building the packages that will uploaded to PyPI. It is not part of the standard development cycle (i.e. write code/build/tests) so developers do not need to install
larkon their systems.