Skip to content

Improve auto-generated documentation and type stubs#1413

Open
KurtBoehm wants to merge 7 commits into
libvips:masterfrom
KurtBoehm:master
Open

Improve auto-generated documentation and type stubs#1413
KurtBoehm wants to merge 7 commits into
libvips:masterfrom
KurtBoehm:master

Conversation

@KurtBoehm

Copy link
Copy Markdown

This PR introduces several improvements to the typing in the documentation and the type stubs:

  • Use bytes as the type of buffers (which is actually what functions such as pngsave_buffer return) and str | Path for file names (which were typed as just str until now, but the helper function converting strings to bytes already handles pathlib.Path).
  • Extend the type stub generation to generate the full enum definitions automatically and define Operation and Introspect.
  • Extend the type stub with more operations (all apart from nifti-based ones).

The only remaining limitation of the Image type stubs is the lack of nifti operations, as I was not able to install libniftiio on Arch Linux in a way that libvips recognized during compilation (and I did not really want to spend more time on installing it).

-  Use `bytes` and `str | Path` where appropriate.
- Extend the type stub generation to generate the full enum definitions and define `Operation` and `Introspect`.
- Extend the type stub with more operations (all apart from nifti-based ones).
@jcupitt

jcupitt commented May 31, 2026

Copy link
Copy Markdown
Member

This is very cool @KurtBoehm! I'll try to review this week.

Yes, nifti is surprisingly difficult to build :( I leave it out of the flatpak and win builds as well, it's just too annoying.

KurtBoehm added 6 commits May 31, 2026 15:30
…nd pyright

- Create a separate stub for `enums.py` to support importing these separately.
- Properly type the NumPy functions, which mypy and pyright ignore is NumPy is not available.
- Add the missing trigonometric, exponential, and complex operations.
- Force keyword arguments to be keyword arguments.
- Type `interpolate` as `Interpolate`, not the overly broad `GObject`.
- Introduce type aliases to reduce duplication.
- Make very minor adjustments elsewhere to avoid mypy errors.
@KurtBoehm

Copy link
Copy Markdown
Author

Since nothing seems to have happened since my last commit and I have noticed more issues/limitations with the type stubs, I have added many further improvements (most are listed in the commit message).
Most notably, proper NumPy typing is now present, the missing trigonometric/exponential/complex operations have been added, and enums.py now gets its own pyi file so that the enumerations submodule can be imported.
With these changes, mypy and pyright find no issues with any of the files under examples/.

If you are okay with this, I propose splitting the large pyi file into one per submodule to enable imports such as from pyvips.error import … (not only for enums) and adding some additional components from other helper submodules.
Otherwise, this PR should be pretty much complete from my side.

Since the type stubs require Python 3.10+, and Python 3.9 has reached end-of-life anyway, I switched the CI PyPy version to 3.10 (PyPy 3.11 appears to be incompatible with Ubuntu 22.04).

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