2020
2121import os
2222import pathlib
23+ import re
2324import subprocess
2425import sys
2526
2627_HEADER_PREFIX = b"# This file was generated by stubgen-pyx"
28+ # Marker for per-parameter stub-public annotation overrides; see _collect_stub_overrides.
29+ _STUB_DIRECTIVE = "# stub:"
2730
2831
2932def _normalize_stub_headers (root : pathlib .Path ) -> None :
@@ -36,17 +39,67 @@ def _normalize_stub_headers(root: pathlib.Path) -> None:
3639 stub .write_bytes (first_line .replace (b"\\ " , b"/" ) + data [newline :])
3740
3841
42+ def _collect_stub_overrides (root : pathlib .Path ) -> dict [pathlib .Path , list [tuple [str , str ]]]:
43+ """Collect # stub: annotation overrides declared in .pyx source files.
44+
45+ A line in a .pyx file of the form
46+ param: OrigType = default # stub: NewType
47+ causes the postprocessor to replace "param: OrigType" with "param: NewType"
48+ in the corresponding generated .pyi, so the public stub can differ from the
49+ runtime annotation without editing generated files by hand.
50+ """
51+ overrides : dict [pathlib .Path , list [tuple [str , str ]]] = {}
52+ for pyx in sorted (root .rglob ("*.pyx" )):
53+ pyi = pyx .with_suffix (".pyi" )
54+ pairs = []
55+ for line in pyx .read_text (encoding = "utf-8" ).splitlines ():
56+ if _STUB_DIRECTIVE not in line :
57+ continue
58+ code_part , _ , directive = line .partition (_STUB_DIRECTIVE )
59+ # Directive format: "param_name: NewType"
60+ m_dir = re .match (r"^(\w+)\s*:\s*(.+)$" , directive .strip ())
61+ if not m_dir :
62+ continue
63+ param_name , new_type = m_dir .group (1 ), m_dir .group (2 ).strip ()
64+ # Locate "param_name: " in the source line and extract the type.
65+ # str.find + partition avoids regex bracket-balance issues (e.g. dict[str, object]).
66+ marker = f"{ param_name } : "
67+ idx = code_part .find (marker )
68+ if idx == - 1 :
69+ continue
70+ after_param = code_part [idx + len (marker ) :]
71+ original_type = after_param .partition (" = " )[0 ].strip ()
72+ pairs .append ((f"{ param_name } : { original_type } " , f"{ param_name } : { new_type } " ))
73+ if pairs :
74+ overrides [pyi ] = pairs
75+ return overrides
76+
77+
78+ def _apply_stub_overrides (overrides : dict [pathlib .Path , list [tuple [str , str ]]]) -> None :
79+ """Apply annotation overrides to generated .pyi files."""
80+ for pyi , pairs in overrides .items ():
81+ if not pyi .exists ():
82+ continue
83+ content = pyi .read_text (encoding = "utf-8" )
84+ for original , replacement in pairs :
85+ content = content .replace (original , replacement )
86+ pyi .write_text (content , encoding = "utf-8" )
87+
88+
3989def main () -> int :
4090 env = os .environ .copy ()
4191 env .setdefault ("PYTHONUTF8" , "1" )
4292 env .setdefault ("PYTHONIOENCODING" , "utf-8" )
93+ # Collect overrides before running stubgen so we read clean source.
94+ overrides = _collect_stub_overrides (pathlib .Path ("cuda_core/cuda" ))
4395 result = subprocess .run (
4496 ["stubgen-pyx" , "cuda_core/cuda" , "--continue-on-error" , "--include-private" ], # noqa: S607
4597 env = env ,
4698 )
4799 if result .returncode != 0 :
48100 return result .returncode
49101 _normalize_stub_headers (pathlib .Path ("cuda_core/cuda" ))
102+ _apply_stub_overrides (overrides )
50103 return 0
51104
52105
0 commit comments