Skip to content

Commit 26a97ec

Browse files
authored
Add --stub-extension CLI option (#142)
The option could be helpful for downstream doc-generation tools that expect `.py` files only.
1 parent afb3fdb commit 26a97ec

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ pybind11-stubgen [-h]
2828
[--ignore-invalid-identifiers REGEX]
2929
[--ignore-unresolved-names REGEX]
3030
[--ignore-all-errors]
31-
[--numpy-array-wrap-with-annotated-fixed-size| --numpy-array-remove-parameters]
31+
[--numpy-array-wrap-with-annotated|
32+
--numpy-array-remove-parameters]
3233
[--print-invalid-expressions-as-is]
3334
[--exit-code]
35+
[--stub-extension EXT]
3436
MODULE_NAME
3537
```

pybind11_stubgen/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ def regex(pattern_str: str) -> re.Pattern:
147147
help="Don't write stubs. Parses module and report errors",
148148
)
149149

150+
parser.add_argument(
151+
"--stub-extension",
152+
type=str,
153+
default="pyi",
154+
metavar="EXT",
155+
choices=["pyi", "py"],
156+
help="The file extension of the generated stubs. "
157+
"Must be 'pyi' (default) or 'py'",
158+
)
159+
150160
parser.add_argument(
151161
"module_name",
152162
metavar="MODULE_NAME",
@@ -246,6 +256,7 @@ def main():
246256
out_dir,
247257
sub_dir=sub_dir,
248258
dry_run=args.dry_run,
259+
writer=Writer(stub_ext=args.stub_extension),
249260
)
250261

251262

@@ -274,6 +285,7 @@ def run(
274285
out_dir: Path,
275286
sub_dir: Path | None,
276287
dry_run: bool,
288+
writer: Writer,
277289
):
278290
module = parser.handle_module(
279291
QualifiedName.from_str(module_name), importlib.import_module(module_name)
@@ -286,8 +298,6 @@ def run(
286298
if dry_run:
287299
return
288300

289-
writer = Writer()
290-
291301
out_dir.mkdir(exist_ok=True, parents=True)
292302
writer.write_module(module, printer, to=out_dir, sub_dir=sub_dir)
293303

pybind11_stubgen/writer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
class Writer:
10+
def __init__(self, stub_ext: str = "pyi"):
11+
# assert stub_extension in ["pyi", "py"]
12+
self.stub_ext: str = stub_ext
13+
1014
def write_module(
1115
self, module: Module, printer: Printer, to: Path, sub_dir: Path | None = None
1216
):
@@ -17,10 +21,9 @@ def write_module(
1721
sub_dir = Path(module.name)
1822
module_dir = to / sub_dir
1923
module_dir.mkdir(exist_ok=True)
20-
module_file = module_dir / "__init__.pyi"
24+
module_file = module_dir / f"__init__.{self.stub_ext}"
2125
else:
22-
module_file = to / f"{module.name}.pyi"
23-
26+
module_file = to / f"{module.name}.{self.stub_ext}"
2427
with open(module_file, "w", encoding="utf-8") as f:
2528
f.writelines(line + "\n" for line in printer.print_module(module))
2629

0 commit comments

Comments
 (0)