Skip to content

Commit 11649fa

Browse files
committed
refactor: enhance dynamic_import function to accept base_prefix and improve stub generation
1 parent ab1bc2d commit 11649fa

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/scyjava/_stubs/_dynamic_import.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import ast
22
from logging import warning
33
from pathlib import Path
4-
from typing import Any, Callable
4+
from typing import Any, Callable, Sequence
55

66

77
def dynamic_import(
8-
module_name: str, module_file: str, *endpoints: str
8+
module_name: str,
9+
module_file: str,
10+
endpoints: Sequence[str] = (),
11+
base_prefix: str = "",
912
) -> tuple[list[str], Callable[[str], Any]]:
1013
import scyjava
1114
import scyjava.config
@@ -35,27 +38,25 @@ def module_getattr(name: str, mod_name: str = module_name) -> Any:
3538
if module_all and name not in module_all:
3639
raise AttributeError(f"module {module_name!r} has no attribute {name!r}")
3740

38-
# this strip is important... and tricky, because it depends on the
39-
# namespace that we intend to install the stubs into.
40-
install_path = "scyjava.types."
41-
if mod_name.startswith(install_path):
42-
mod_name = mod_name[len(install_path) :]
41+
# cut the mod_name to only the part including the base_prefix and after
42+
if base_prefix in mod_name:
43+
mod_name = mod_name[mod_name.index(base_prefix) :]
4344

44-
full_name = f"{mod_name}.{name}"
45+
class_path = f"{mod_name}.{name}"
4546

4647
class ProxyMeta(type):
4748
def __repr__(self) -> str:
48-
return f"<scyjava class {full_name!r}>"
49+
return f"<scyjava class {class_path!r}>"
4950

5051
class Proxy(metaclass=ProxyMeta):
5152
def __new__(_cls_, *args: Any, **kwargs: Any) -> Any:
52-
cls = scyjava.jimport(full_name)
53+
cls = scyjava.jimport(class_path)
5354
return cls(*args, **kwargs)
5455

5556
Proxy.__name__ = name
5657
Proxy.__qualname__ = name
5758
Proxy.__module__ = module_name
58-
Proxy.__doc__ = f"Proxy for {full_name}"
59+
Proxy.__doc__ = f"Proxy for {class_path}"
5960
return Proxy
6061

6162
return module_all, module_getattr

src/scyjava/_stubs/_genstubs.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
116116
output_dir = Path(output_dir)
117117
if add_runtime_imports:
118118
logger.info("Adding runtime imports to generated stubs")
119+
119120
for stub in output_dir.rglob("*.pyi"):
120121
stub_ast = ast.parse(stub.read_text())
121122
members = {node.name for node in stub_ast.body if hasattr(node, "name")}
@@ -127,8 +128,13 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
127128
continue
128129
if add_runtime_imports:
129130
real_import = stub.with_suffix(".py")
130-
endpoint_args = ", ".join(repr(x) for x in endpoints)
131-
real_import.write_text(INIT_TEMPLATE.format(endpoints=endpoint_args))
131+
base_prefix = stub.relative_to(output_dir).parts[0]
132+
real_import.write_text(
133+
INIT_TEMPLATE.format(
134+
endpoints=repr(endpoints),
135+
base_prefix=repr(base_prefix),
136+
)
137+
)
132138

133139
ruff_check(output_dir.absolute())
134140

@@ -141,7 +147,12 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
141147
# see scyjava._stubs for implementation details.
142148
from scyjava._stubs import dynamic_import
143149
144-
__all__, __getattr__ = dynamic_import(__name__, __file__, {endpoints})
150+
__all__, __getattr__ = dynamic_import(
151+
__name__,
152+
__file__,
153+
endpoints={endpoints},
154+
base_prefix={base_prefix},
155+
)
145156
"""
146157

147158

0 commit comments

Comments
 (0)