Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to generate the stub file #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 78 additions & 0 deletions generate_spherely_vfunc_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import itertools
import string
from pathlib import Path

STUB_FILE_PATH = Path(__file__).parent / "src" / "spherely.pyi"
BEGIN_MARKER = "# /// Begin types"
END_MARKER = "# /// End types"


def update_stub_file(path=STUB_FILE_PATH, **type_specs):
stub_text = path.read_text(encoding="utf-8")
try:
start_idx = stub_text.index(BEGIN_MARKER)
end_idx = stub_text.index(END_MARKER)
except ValueError:
raise SystemExit(
f"Error: Markers '{BEGIN_MARKER}' and '{END_MARKER}' "
f"were not found in stub file '{path}'"
) from None

header = f"{BEGIN_MARKER}\n"
code = "\n\n".join(
_vfunctype_factory(name, **args) for name, args in type_specs.items()
)
updated_stub_text = stub_text[:start_idx] + header + code + stub_text[end_idx:]
path.write_text(updated_stub_text, encoding="utf-8")


def _vfunctype_factory(class_name, n_in, **optargs):
"""Create new VFunc types.

Based on the number of input arrays and optional arguments and their types."""
arg_names = ["geography"] if n_in == 1 else list(string.ascii_lowercase[:n_in])
class_code = [
f"class {class_name}(",
" Generic[_NameType, _ScalarReturnType, _ArrayReturnDType]",
"):",
" @property",
" def __name__(self) -> _NameType: ...",
"",
]
optarg_str = ", ".join(
f"{arg_name}: {arg_type} = ..." for arg_name, arg_type in optargs.items()
)

geog_types = ["Geography", "npt.ArrayLike"]
for arg_types in itertools.product(geog_types, repeat=n_in):
arg_str = ", ".join(
f"{arg_name}: {arg_type}"
for arg_name, arg_type in zip(arg_names, arg_types)
)
return_type = (
"_ScalarReturnType"
if all(t == geog_types[0] for t in arg_types)
else "npt.NDArray[_ArrayReturnDType]"
)
class_code.extend(
[
" @overload",
" def __call__(",
(
f" self, {arg_str}, {optarg_str}"
if optarg_str
else f" self, {arg_str}"
),
f" ) -> {return_type}: ...",
"",
]
)
return "\n".join(class_code)


if __name__ == "__main__":
update_stub_file(
_VFunc_Nin1_Nout1={"n_in": 1},
_VFunc_Nin2_Nout1={"n_in": 2},
# _VFunc_Nin2radius_Nout1={"n_in": 2, "radius": "float"},
)
13 changes: 10 additions & 3 deletions src/spherely.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ _NameType = TypeVar("_NameType", bound=str)
_ScalarReturnType = TypeVar("_ScalarReturnType", bound=Any)
_ArrayReturnDType = TypeVar("_ArrayReturnDType", bound=Any)

# The following types are auto-generated. Please don't edit them by hand.
# Instead, update the generate_spherely_vfunc_types.py script and run it
# to update the types.
#
# /// Begin types
class _VFunc_Nin1_Nout1(Generic[_NameType, _ScalarReturnType, _ArrayReturnDType]):
@property
def __name__(self) -> _NameType: ...
Expand All @@ -76,17 +81,19 @@ class _VFunc_Nin2_Nout1(Generic[_NameType, _ScalarReturnType, _ArrayReturnDType]
def __call__(self, a: Geography, b: Geography) -> _ScalarReturnType: ...
@overload
def __call__(
self, a: npt.ArrayLike, b: npt.ArrayLike
self, a: Geography, b: npt.ArrayLike
) -> npt.NDArray[_ArrayReturnDType]: ...
@overload
def __call__(
self, a: Geography, b: npt.ArrayLike
self, a: npt.ArrayLike, b: Geography
) -> npt.NDArray[_ArrayReturnDType]: ...
@overload
def __call__(
self, a: npt.ArrayLike, b: Geography
self, a: npt.ArrayLike, b: npt.ArrayLike
) -> npt.NDArray[_ArrayReturnDType]: ...

# /// End types

# Geography properties

get_dimensions: _VFunc_Nin1_Nout1[Literal["get_dimensions"], Geography, Any]
Expand Down
Loading