1+ """Type stub generation utilities using stubgen.
2+
3+ This module provides utilities for generating type stubs for Java classes
4+ using the stubgenj library. `stubgenj` must be installed for this to work
5+ (it, in turn, only depends on JPype).
6+
7+ See `generate_stubs` for most functionality. For the command-line tool,
8+ see `scyjava._stubs.cli`, which provides a CLI interface for the `generate_stubs`
9+ function.
10+ """
11+
112from __future__ import annotations
213
314import ast
@@ -42,9 +53,11 @@ def generate_stubs(
4253 The prefixes to generate stubs for. This should be a list of Java class
4354 prefixes that you expect to find in the endpoints. For example,
4455 ["org.apache.commons"]. If not provided, the prefixes will be
45- automatically determined from the jar files provided by endpoints.
56+ automatically determined from the jar files provided by endpoints (see the
57+ `_list_top_level_packages` helper function).
4658 output_dir : str | Path, optional
47- The directory to write the generated stubs to. Defaults to "stubs".
59+ The directory to write the generated stubs to. Defaults to "stubs" in the
60+ current working directory.
4861 convert_strings : bool, optional
4962 Whether to cast Java strings to Python strings in the stubs. Defaults to True.
5063 NOTE: This leads to type stubs that may not be strictly accurate at runtime.
@@ -58,8 +71,10 @@ def generate_stubs(
5871 Whether to include Javadoc in the generated stubs. Defaults to True.
5972 add_runtime_imports : bool, optional
6073 Whether to add runtime imports to the generated stubs. Defaults to True.
61- This is useful if you want to use the stubs as a runtime package with type
62- safety.
74+ This is useful if you want to actually import the stubs as a runtime package
75+ with type safety. The runtime import "magic" depends on the
76+ `scyjava._stubs.setup_java_imports` function. See its documentation for
77+ more details.
6378 remove_namespace_only_stubs : bool, optional
6479 Whether to remove stubs that export no names beyond a single
6580 `__module_protocol__`. This leaves some folders as PEP420 implicit namespace
@@ -95,7 +110,7 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
95110 ep_artifacts = tuple (ep .split (":" )[1 ] for ep in endpoints )
96111 for j in cp .split (os .pathsep ):
97112 if Path (j ).name .startswith (ep_artifacts ):
98- _prefixes .update (list_top_level_packages (j ))
113+ _prefixes .update (_list_top_level_packages (j ))
99114
100115 prefixes = sorted (_prefixes )
101116 logger .info (f"Using endpoints: { scyjava .config .endpoints !r} " )
@@ -189,7 +204,7 @@ def ruff_check(output: Path, select: str = "E,W,F,I,UP,C4,B,RUF,TC,TID") -> None
189204 subprocess .run (["ruff" , "format" , * py_files , "--quiet" ])
190205
191206
192- def list_top_level_packages (jar_path : str ) -> set [str ]:
207+ def _list_top_level_packages (jar_path : str ) -> set [str ]:
193208 """Inspect a JAR file and return the set of top-level Java package names."""
194209 packages : set [str ] = set ()
195210 with ZipFile (jar_path , "r" ) as jar :
0 commit comments