Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 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
43cb06d
initial
tlambert03 Aug 22, 2025
b181bd7
working principle
tlambert03 Aug 23, 2025
76d9bfa
remove readme
tlambert03 Aug 23, 2025
c579490
remove x
tlambert03 Aug 23, 2025
a519d35
Merge branch 'delay-import' into stubs-metafinder
tlambert03 Aug 23, 2025
93e4e51
cleaner
tlambert03 Aug 23, 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
setuptools plugin stub
  • Loading branch information
tlambert03 committed Apr 25, 2025
commit 79524b0065c0e6bdbb57c63d9d2cbe9bc8cc6c8b
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ dev = [
scyjava-stubgen = "scyjava._stubs._cli:main"

[project.entry-points.hatch]
mypyc = "scyjava._stubs._hatchling"

scyjava = "scyjava._stubs._hatchling_plugin"
[project.entry-points."distutils.commands"]
build_py = "scyjava_stubgen.build:build_py"

[project.urls]
homepage = "https://github.com/scijava/scyjava"
Expand Down
79 changes: 79 additions & 0 deletions src/scyjava/_stubs/_setuptools_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Setuptools build hook for generating Java stubs.

To use this hook, add the following to your `pyproject.toml`:

```toml
[build-system]
requires = ["setuptools>=69", "wheel", "scyjava"]
build-backend = "setuptools.build_meta"

[tool.setuptools.cmdclass]
build_py = "scyjava_stubgen.build:build_py"
# optional project-specific defaults
maven_coordinates = ["org.scijava:parsington:3.1.0"]
prefixes = ["org.scijava"]
```

This will generate stubs for the given maven coordinates and prefixes. The generated
stubs will be placed in `src/scyjava/types` and will be included in the wheel package.
This hook is only run when building a wheel package.
"""

from __future__ import annotations

import logging
from pathlib import Path
from typing import List

from setuptools.command.build_py import build_py as _build_py
from scyjava._stubs._genstubs import generate_stubs

log = logging.getLogger("scyjava")


class build_py(_build_py): # type: ignore[misc]
"""
A drop-in replacement for setuptools' build_py that
generates Java type stubs before Python sources are copied
into *build_lib*.
"""

# expose two optional CLI/pyproject options so users can override defaults
user_options: List[tuple[str, str | None, str]] = _build_py.user_options + [
("maven-coordinates=", None, "List of Maven coordinates to stub"),
("prefixes=", None, "Java package prefixes to include"),
]

def initialize_options(self) -> None: # noqa: D401
super().initialize_options()
self.maven_coordinates: list[str] | None = None
self.prefixes: list[str] | None = None

def finalize_options(self) -> None: # noqa: D401
"""Fill in options that may come from pyproject metadata."""
super().finalize_options()
dist = self.distribution # alias
if self.maven_coordinates is None:
self.maven_coordinates = getattr(dist, "maven_coordinates", [])
if self.prefixes is None:
self.prefixes = getattr(dist, "prefixes", [])

def run(self) -> None: # noqa: D401
"""Generate stubs, then let the normal build_py proceed."""
if self.maven_coordinates:
dest = Path(self.build_lib, "scyjava", "types")
dest.parent.mkdir(parents=True, exist_ok=True)
(dest.parent / "py.typed").touch()

generate_stubs(
endpoints=self.maven_coordinates,
prefixes=self.prefixes,
output_dir=dest,
remove_namespace_only_stubs=True,
)
log.info("Generated stubs for %s", ", ".join(self.maven_coordinates))

# make sure the wheel knows about them
self.package_data.setdefault("scyjava", []).append("types/**/*.pyi")

super().run()
Loading