Skip to content

Commit

Permalink
Extend nb stubs to all arts submodules (#866)
Browse files Browse the repository at this point in the history
Adds completion support for all nb submodules inside the arts module.
.pyi files are now written to an `arts` subfolder. Getting access to the
new completions requires deleting either the whole build directory or
removing `build/python/src/pyarts/arts.pyi` manually.
  • Loading branch information
riclarsson authored Nov 21, 2024
2 parents 57202ba + 2c4483f commit dabc79e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
27 changes: 27 additions & 0 deletions cmake/modules/ArtsNanobindStubs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function (ARTS_ADD_CPP_STUBS)

foreach (MODULENAME IN LISTS ARGN)
nanobind_add_stub(
pyarts_${MODULENAME}_cpp_stub
MODULE arts.${MODULENAME}
OUTPUT ${ARTS_BINARY_DIR}/python/src/pyarts/arts/${MODULENAME}.pyi
PYTHON_PATH ${ARTS_BINARY_DIR}/python/src/pyarts
DEPENDS pyarts_cpp
)
list(APPEND deplist "pyarts_${MODULENAME}_cpp_stub")
endforeach()

nanobind_add_stub(
pyarts_cpp_stub
MODULE arts
OUTPUT ${ARTS_BINARY_DIR}/python/src/pyarts/arts/__init__.pyi
PYTHON_PATH ${ARTS_BINARY_DIR}/python/src/pyarts
DEPENDS pyarts_cpp "${deplist}"
)
set_property(
TARGET pyarts_cpp_stub
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${ARTS_BINARY_DIR}/python/src/pyarts/arts
)

endfunction()
4 changes: 2 additions & 2 deletions environment-dev-mac.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
dependencies:
- boost
- ccache
- clang
- clangxx
- clang>=19
- clangxx>=19
- cmake
- docutils
- doxygen
Expand Down
36 changes: 36 additions & 0 deletions python/test/test_pyi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from inspect import getmembers, ismodule
from pathlib import Path

import pyarts
from pyarts import arts


class TestStubFiles:
def walk_modules(self, m):
submodules = getmembers(m, ismodule)
for submodule in submodules:
if submodule[1].__name__.startswith("pyarts.arts."):
yield submodule[1].__name__
yield from self.walk_modules(submodule[1])

def test_pyi(self):
missing_stubs = []
for module in self.walk_modules(arts):
if (
not Path(pyarts.__path__[0])
.joinpath(module.replace("pyarts.", "").replace(".", "/") + ".pyi")
.is_file()
):
missing_stubs.append(module.replace("pyarts.arts.", ""))

assert len(missing_stubs) == 0, (
f"Missing stub file(s) for arts submodule(s): \n"
f" {"\n ".join(missing_stubs)}\n"
f"Add them to the `arts_add_cpp_stubs` call in "
f"src/python_interface/CMakeLists.txt"
)


if __name__ == "__main__":
a = TestStubFiles()
a.test_pyi()
22 changes: 15 additions & 7 deletions src/python_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,19 @@ set_target_properties(
pyarts_cpp PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${ARTS_BINARY_DIR}/python/src/pyarts/")

nanobind_add_stub(
pyarts_cpp_stub
MODULE arts
OUTPUT ${ARTS_BINARY_DIR}/python/src/pyarts/arts.pyi
PYTHON_PATH ${ARTS_BINARY_DIR}/python/src/pyarts
DEPENDS pyarts_cpp
include (ArtsNanobindStubs)
arts_add_cpp_stubs(
constants
convert
disort
file
globals
hitran
interp
lbl
math
physics
predef
rtepack
zeeman
)

0 comments on commit dabc79e

Please sign in to comment.