Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0f70b03
ignore types folder
tlambert03 Apr 23, 2025
459a999
Add scyjava-stubs CLI and dynamic import functionality
tlambert03 Apr 23, 2025
a8b2da7
remove import
tlambert03 Apr 23, 2025
686739b
add test
tlambert03 Apr 23, 2025
7fe31af
add comment to clarify stubgen command execution in test
tlambert03 Apr 23, 2025
afcc7a7
refactor: clean up jpype imports in stubgen test and main module
tlambert03 Apr 23, 2025
a5cacc8
remove unused jpype import from _genstubs.py
tlambert03 Apr 23, 2025
ab1bc2d
fix: add future annotations import to _cli.py
tlambert03 Apr 23, 2025
11649fa
refactor: enhance dynamic_import function to accept base_prefix and i…
tlambert03 Apr 23, 2025
2cb4836
refactor: rename dynamic_import to setup_java_imports and update usag…
tlambert03 Apr 23, 2025
71f761e
reword
tlambert03 Apr 23, 2025
65cc471
feat: add Hatchling build hook for generating Java stubs
tlambert03 Apr 25, 2025
6e4181e
wip
tlambert03 Apr 25, 2025
6e92b13
fix inclusion
tlambert03 Apr 25, 2025
0d231cc
add docs
tlambert03 Apr 25, 2025
79524b0
setuptools plugin stub
tlambert03 Apr 25, 2025
9bdba53
Merge branch 'main' into stubs
tlambert03 Apr 30, 2025
51937b5
remove repr test
tlambert03 May 1, 2025
192be35
skip in jep
tlambert03 May 1, 2025
e714abb
remove setuptools plugin
tlambert03 May 1, 2025
e7bc894
Merge branch 'main' into stubs
tlambert03 Aug 20, 2025
a18d9d9
remove hatch plugin
tlambert03 Aug 22, 2025
b53e795
newlines
tlambert03 Aug 22, 2025
ed0add6
update docs
tlambert03 Aug 22, 2025
4f54611
Rewrite type references in stubs to include prefix
ctrueden Dec 10, 2025
6783f57
Fix typo: _get_ouput_dir -> _get_output_dir
ctrueden Dec 10, 2025
fb8c3ef
Fix printf-style string syntax typo
ctrueden Dec 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: rename dynamic_import to setup_java_imports and update usag…
…e in stubs
  • Loading branch information
tlambert03 committed Apr 23, 2025
commit 2cb4836c370a678b307849891300d28a9777b6e1
4 changes: 2 additions & 2 deletions src/scyjava/_stubs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._dynamic_import import dynamic_import
from ._dynamic_import import setup_java_imports
from ._genstubs import generate_stubs

__all__ = ["dynamic_import", "generate_stubs"]
__all__ = ["setup_java_imports", "generate_stubs"]
41 changes: 40 additions & 1 deletion src/scyjava/_stubs/_dynamic_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,51 @@
from typing import Any, Callable, Sequence


def dynamic_import(
def setup_java_imports(
module_name: str,
module_file: str,
endpoints: Sequence[str] = (),
base_prefix: str = "",
) -> tuple[list[str], Callable[[str], Any]]:
"""Setup a module to dynamically import Java class names.

This function creates a `__getattr__` function that, when called, will dynamically
import the requested class from the Java namespace corresponding to this module.

:param module_name: The dotted name/identifier of the module that is calling this
function (usually `__name__` in the calling module).
:param module_file: The path to the module file (usually `__file__` in the calling
module).
:param endpoints: A list of Java endpoints to add to the scyjava configuration.
:param base_prefix: The base prefix for the Java package name. This is used when
determining the Java class path for the requested class. The java class path
will be truncated to only the part including the base_prefix and after. This
makes it possible to embed a module in a subpackage (like `scyjava.types`) and
still have the correct Java class path.
:return: A 2-tuple containing:
- A list of all classes in the module (as defined in the stub file), to be
assigned to `__all__`.
- A callable that takes a class name and returns a proxy for the Java class.
This callable should be assigned to `__getattr__` in the calling module.
The proxy object, when called, will start the JVM, import the Java class,
and return an instance of the class. The JVM will *only* be started when
the object is called.

Example:
If the module calling this function is named `scyjava.types.org.scijava.parsington`,
then it should invoke this function as:

.. code-block:: python

from scyjava._stubs import setup_java_imports

__all__, __getattr__ = setup_java_imports(
__name__,
__file__,
endpoints=["org.scijava:parsington:3.1.0"],
base_prefix="org"
)
"""
import scyjava
import scyjava.config

Expand Down
13 changes: 10 additions & 3 deletions src/scyjava/_stubs/_genstubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import scyjava
import scyjava.config
import stubgenj

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -69,6 +68,14 @@ def generate_stubs(
the `__init__.pyi` for any given module will be whatever whatever the *last*
stub generator wrote to it (and therefore inaccurate).
"""
try:
import stubgenj
except ImportError as e:
raise ImportError(
"stubgenj is not installed, but is required to generate java stubs. "
"Please install it with `pip/conda install stubgenj`."
) from e

import jpype

startJVM = jpype.startJVM
Expand Down Expand Up @@ -145,9 +152,9 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
# it creates a __getattr__ function that will dynamically import
# the requested class from the Java namespace corresponding to this module.
# see scyjava._stubs for implementation details.
from scyjava._stubs import dynamic_import
from scyjava._stubs import setup_java_imports

__all__, __getattr__ = dynamic_import(
__all__, __getattr__ = setup_java_imports(
__name__,
__file__,
endpoints={endpoints},
Expand Down
Loading