Skip to main content

PEP484 python stub generator for Java classes accessed through Chaquopy on Android

Project description

chaquopy-stubgen

This tool is a Python type stub generator from Java classes. It is optimized for the usage with Android via chaquopy v17 and briefcase.

The generated stubs can be used with Python 3.10 or higher.

⚠️ Disclaimer: This is not an official tool of the Chaquopy project. It is an independent, community-maintained project.

CLI Usage

Example call uv run -m chaquopy_stubgen "$ANDROID_HOME/platforms/android-35/android.jar"

$ uv run -m chaquopy_stubgen --help
usage: __main__.py [-h] [--jvmpath JVMPATH] [--output-dir OUTPUT_DIR] [--no-clean] [--cache-dir CACHE_DIR] inputs [inputs ...]

Generate Python Type Stubs for Java classes that are optimized for chaquopy.

positional arguments:
  inputs                List of .jar/.aar files, directories containing .class files, Android platform shorthands (e.g.
                        'android-35'), or Maven coordinates (e.g. 'androidx.appcompat:appcompat:1.0.2') to generate stubs for.

options:
  -h, --help            show this help message and exit
  --jvmpath JVMPATH     path to the JVM ("libjvm.so", "jvm.dll", ...) (default: use system default JVM)
  --output-dir OUTPUT_DIR
                        path to write stubs to (default: ./dist/stubs)
  --no-clean            skip clearing the output directory before generating stubs
  --cache-dir CACHE_DIR
                        directory for caching downloaded artifacts (default: ~/.cache/chaquopy-stubgen)

Caveats & Limitations

Importing Java Packages

Always use from java.lang import String rather than import java.lang.String. While the stubs permit both forms, only the first works at runtime under chaquopy's import hook. See the chaquopy documentation for details.

Handling of null Values

In Java, any reference type can in principle be null. Annotating every return type as X | None would be technically correct, but makes the generated stubs very noisy and forces callers to perform if ... is not None: checks everywhere.

Whether a method can actually return null usually only becomes apparent from its documentation. chaquopy-stubgen therefore ships a whitelist of methods that are documented to return null; those are emitted with | None appended to their return type. All other reference return types are treated as non-optional, even though they could theoretically be null at runtime.

The whitelist is not exhaustive and may have gaps or false positives. Feel free to open an issue or pull request if you find a missing or incorrect entry.

Development

  • Create venv: uv sync
  • Test: uv run pytest

Credits

This is based on stubgenj, but generates stubs optimized for chaquopy and is internally reworked to use ASM instead of Java Reflection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chaquopy_stubgen-0.3.2.tar.gz (26.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chaquopy_stubgen-0.3.2-py3-none-any.whl (226.7 kB view details)

Uploaded Python 3

File details

Details for the file chaquopy_stubgen-0.3.2.tar.gz.

File metadata

  • Download URL: chaquopy_stubgen-0.3.2.tar.gz
  • Upload date:
  • Size: 26.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chaquopy_stubgen-0.3.2.tar.gz
Algorithm Hash digest
SHA256 b73ccff713c14bd1a0e13aaeea7b3a5066195323eefd2ecf9d89a1dfc236aeb4
MD5 d58b679e71b385a4351cd45ddb9d74e3
BLAKE2b-256 5f475da6be8a3e90b31c8ea84e51451586fdcda8557b4f375f99254766073e85

See more details on using hashes here.

Provenance

The following attestation bundles were made for chaquopy_stubgen-0.3.2.tar.gz:

Publisher: python-publish.yml on timrid/chaquopy-stubgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chaquopy_stubgen-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for chaquopy_stubgen-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b369713a0bf9cb1c1e89f7550042114c33d2a19b92b741f229f403cf4cb34f3
MD5 1b18cd9eb5434cd6a7cee2bbeb4da804
BLAKE2b-256 71e02f6543f619ae1d998d9a8754a471cda13ed3d541c1e3d44c2e694de38165

See more details on using hashes here.

Provenance

The following attestation bundles were made for chaquopy_stubgen-0.3.2-py3-none-any.whl:

Publisher: python-publish.yml on timrid/chaquopy-stubgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page