-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_nilearn_stubs.py
More file actions
64 lines (49 loc) · 2.07 KB
/
Copy pathgenerate_nilearn_stubs.py
File metadata and controls
64 lines (49 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import subprocess
from pathlib import Path
from rich import print
# Root directory of the nilearn source code
NILEARN_SRC_DIR = Path("nilearn-stubs")
# List of modules to explicitly exclude
EXCLUDE_LIST = [
"tests",
]
# List of modules to explicitly include (leave empty to include all except excluded)
INCLUDE_LIST = ["nilearn._utils"]
def get_traced_modules() -> list[str]:
"""Return a list of modules traced by MonkeyType that are in the nilearn package."""
result = subprocess.run(["monkeytype", "list-modules"], capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Failed to run monkeytype list-modules: {result.stderr}")
return [line.strip() for line in result.stdout.splitlines() if line.startswith("nilearn.")]
def should_include(module: str) -> bool:
exclude = any(x in module for x in EXCLUDE_LIST) if EXCLUDE_LIST else False
if exclude:
return False
if not INCLUDE_LIST:
return True
return any(x in module for x in INCLUDE_LIST)
def generate_stub(module: str):
"""Generate stub for a module using monkeytype and write to correct .pyi path."""
module_path = NILEARN_SRC_DIR / Path(module.replace(".", "/").replace("nilearn/", "")).with_suffix(".pyi")
module_path.parent.mkdir(parents=True, exist_ok=True)
try:
print(f"Generating stub for {module} -> {module_path}")
result = subprocess.run(["monkeytype", "stub", module], capture_output=True, text=True)
if result.returncode != 0:
print(f"Failed to generate stub for {module}: {result.stderr}")
return
if not result.stdout:
print("nothing to write")
with module_path.open("w", encoding="utf-8") as f:
f.write(result.stdout)
except Exception as e:
print(f"Error processing {module}: {e}")
def main() -> None:
modules = get_traced_modules()
for module in modules:
if should_include(module):
generate_stub(module)
else:
print(f"Skipping module: {module}")
if __name__ == "__main__":
main()