Skip to content

Commit 3c027ea

Browse files
committed
restore desired pyi signatures with stub overrides
1 parent 5f16e21 commit 3c027ea

7 files changed

Lines changed: 59 additions & 6 deletions

‎cuda_core/cuda/core/_memory/_device_memory_resource.pyi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class DeviceMemoryResource(_MemPool):
120120
def __cinit__(self, *args, **kwargs) -> None:
121121
...
122122

123-
def __init__(self, device_id: Device | int, options=None) -> None:
123+
def __init__(self, device_id: Device | int, options: DeviceMemoryResourceOptions | None=None) -> None:
124124
...
125125

126126
def __reduce__(self) -> tuple[object, ...]:

‎cuda_core/cuda/core/_memory/_device_memory_resource.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ cdef class DeviceMemoryResource(_MemPool):
148148
def __init__(
149149
self,
150150
device_id: Device | int,
151-
options=None
151+
options: DeviceMemoryResourceOptions | dict[str, object] | None = None # stub: options: DeviceMemoryResourceOptions | None
152152
) -> None:
153153
_DMR_init(self, device_id, options)
154154

‎cuda_core/cuda/core/_memory/_managed_memory_resource.pyi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ManagedMemoryResource(_MemPool):
7777
memory pools.
7878
"""
7979

80-
def __init__(self, options=None) -> None:
80+
def __init__(self, options: ManagedMemoryResourceOptions | None=None) -> None:
8181
...
8282

8383
def allocate(self, size: int, *, stream: Stream | GraphBuilder) -> ManagedBuffer:

‎cuda_core/cuda/core/_memory/_managed_memory_resource.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ cdef class ManagedMemoryResource(_MemPool):
9797
memory pools.
9898
"""
9999

100-
def __init__(self, options=None) -> None:
100+
def __init__(self, options: ManagedMemoryResourceOptions | dict[str, object] | None = None) -> None: # stub: options: ManagedMemoryResourceOptions | None
101101
_MMR_init(self, options)
102102

103103
def allocate(self, size_t size, *, stream: Stream | GraphBuilder) -> ManagedBuffer:

‎cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class PinnedMemoryResource(_MemPool):
8484
See :class:`DeviceMemoryResource` for more details on IPC usage patterns.
8585
"""
8686

87-
def __init__(self, options=None) -> None:
87+
def __init__(self, options: PinnedMemoryResourceOptions | None=None) -> None:
8888
...
8989

9090
def allocate(self, size: int, *, stream: Stream | GraphBuilder) -> Buffer:

‎cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ cdef class PinnedMemoryResource(_MemPool):
103103
See :class:`DeviceMemoryResource` for more details on IPC usage patterns.
104104
"""
105105

106-
def __init__(self, options=None) -> None:
106+
def __init__(self, options: PinnedMemoryResourceOptions | dict[str, object] | None = None) -> None: # stub: options: PinnedMemoryResourceOptions | None
107107
_PMR_init(self, options)
108108

109109
def allocate(self, size_t size, *, stream: Stream | GraphBuilder) -> Buffer:

‎toolshed/run_stubgen_pyx.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
import os
2222
import pathlib
23+
import re
2324
import subprocess
2425
import 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

2932
def _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+
3989
def 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

Comments
 (0)