Skip to content

Commit

Permalink
Test stubs (mamba-org#2025)
Browse files Browse the repository at this point in the history
test pybind11-stubgen generation
  • Loading branch information
wolfv authored Oct 14, 2022
1 parent 51fdd6d commit d839af7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ jobs:
run: |
micromamba activate build_env
pip install -e ./libmambapy/ --no-deps
- name: check libmambapy stubs
shell: bash -l {0}
run: |
pybind11-stubgen libmambapy.bindings
python compare_stubs.py libmambapy/libmambapy/__init__.pyi stubs/libmambapy/bindings-stubs/__init__.pyi
- name: build cache statistics
run: sccache --show-stats
- name: install mamba
Expand Down
42 changes: 42 additions & 0 deletions compare_stubs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ast
import sys
from itertools import zip_longest


# https://stackoverflow.com/a/66733795
# original autho Seanny123, CC BY-SA 4.0
def compare_ast(node1, node2) -> bool:
if type(node1) is not type(node2):
return False

if isinstance(node1, ast.AST):
for k, v in vars(node1).items():
if k in {"lineno", "end_lineno", "col_offset", "end_col_offset", "ctx"}:
continue
if not compare_ast(v, getattr(node2, k)):
return False
return True

elif isinstance(node1, list) and isinstance(node2, list):
return all(compare_ast(n1, n2) for n1, n2 in zip_longest(node1, node2))
else:
return node1 == node2


if __name__ == "__main__":
print(sys.argv)

f1, f2 = sys.argv[1:3]

with open(f1) as fi:
a1 = ast.parse(fi.read())

with open(f2) as fi:
a2 = ast.parse(fi.read())

if not compare_ast(a1, a2):
print("Stubs are different! Please rerun pybind11-stubgen")
print("CI has these: \n\n")
with open(f2) as fi:
print(fi.read())
sys.exit(1)
1 change: 1 addition & 0 deletions libmambapy/environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ dependencies:
- cli11 >=2.2
- spdlog
- pybind11
- pybind11-stubgen
- pytest
- sel(win): winreg
1 change: 1 addition & 0 deletions mamba/environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
- cli11 >=2.2
- spdlog
- pybind11
- pybind11-stubgen
- pytest
- conda
- sel(win): winreg

0 comments on commit d839af7

Please sign in to comment.