Python Binding Docstring Updates for Stub Generation#13198
Merged
rouault merged 3 commits intoOSGeo:masterfrom Oct 10, 2025
Merged
Python Binding Docstring Updates for Stub Generation#13198rouault merged 3 commits intoOSGeo:masterfrom
rouault merged 3 commits intoOSGeo:masterfrom
Conversation
Collaborator
Member
|
Wow, thanks for cleaning these up! I'm able to generate the stubs locally. Do we need to do anything to cause them to be imported? I notice the following is empty: |
Collaborator
Author
@dbaston - as I understand it, the above approach will only work with inline annotations (within the .py files). The .pyi stubs will work for IDEs with stub support, and type checkers such as mypy. I've yet to test though. |
Member
|
with running So I'd say they're working as intended! Until we address #13145 I guess they'll only work for return types, though. |
dbaston
approved these changes
Oct 10, 2025
Co-authored-by: Dan Baston <dbaston@gmail.com>
rouault
pushed a commit
to rouault/gdal
that referenced
this pull request
Oct 10, 2025
Co-authored-by: Dan Baston <dbaston@gmail.com>
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.
What does this PR do?
This pull request updates the Python SWIG binding docstrings to allow the docstub tool to generate stub files from Python docstrings. This approach was discussed at the recent OSGeo codesprint in Riga.
The longer-term aim is to use these stubs to allow type-checking within the GDAL Python bindings and for user code and scripts, by building on all the previous work on the Python interface and docstrings.
A summary of key changes is below. These were added using a mix of manual review, Notepad++ search/replace and regexes.
:for parameters:options : dict/list, optionalshould beoptions : dict or list, optionalanytype added when unsure of parameter types (or 20-30 parameters without documented types) - these can be refined over timeTo create the stubs:
For this to run successfully, I added a patch to the
docstublibrary - scientific-python/docstub#89.A few notes:
pyproject.tomlas they returned errors in docstub such asUnknown name in doctype: 'Layer'when used as parameter types. However in other docstrings the same types didn't return errors. I believe this is related to import order and the location of the annotation in the files. Updating the .toml file gets rid of the warnings, and allows the type to be kept. The advantage of using GDAL types (rather thanobjectorany) is that Sphinx generates a clickable link to the class in the Python API docs.PyCapsuletype comes fromWhat are related issues/pull requests?
cc @dbaston