Skip to content

Commit bfe56cd

Browse files
authored
create_baseline_stubs: fix pyright config on windows, write to stubs (#8653)
1 parent 1195bba commit bfe56cd

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

scripts/create_baseline_stubs.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import argparse
1414
import os
1515
import re
16-
import shutil
1716
import subprocess
1817
import sys
1918

@@ -46,24 +45,9 @@ def get_installed_package_info(project: str) -> tuple[str, str] | None:
4645
return search_pip_freeze_output(project, r.stdout)
4746

4847

49-
def run_stubgen(package: str) -> None:
50-
print(f"Running stubgen: stubgen -p {package}")
51-
subprocess.run(["stubgen", "-p", package], check=True)
52-
53-
54-
def copy_stubs(src_base_dir: str, package: str, stub_dir: str) -> None:
55-
"""Copy generated stubs to the target directory under stub_dir/."""
56-
print(f"Copying stubs to {stub_dir}")
57-
if not os.path.isdir(stub_dir):
58-
os.mkdir(stub_dir)
59-
src_dir = os.path.join(src_base_dir, package)
60-
if os.path.isdir(src_dir):
61-
shutil.copytree(src_dir, os.path.join(stub_dir, package))
62-
else:
63-
src_file = os.path.join("out", f"{package}.pyi")
64-
if not os.path.isfile(src_file):
65-
sys.exit("Error: Cannot find generated stubs")
66-
shutil.copy(src_file, stub_dir)
48+
def run_stubgen(package: str, output: str) -> None:
49+
print(f"Running stubgen: stubgen -o {output} -p {package}")
50+
subprocess.run(["stubgen", "-o", output, "-p", package], check=True)
6751

6852

6953
def run_black(stub_dir: str) -> None:
@@ -106,7 +90,8 @@ def add_pyright_exclusion(stub_dir: str) -> None:
10690
assert i < len(lines), f"Error parsing {PYRIGHT_CONFIG}"
10791
while not lines[i].strip().startswith("]"):
10892
i += 1
109-
line_to_add = f' "{stub_dir}",'
93+
# Must use forward slash in the .json file
94+
line_to_add = f' "{stub_dir}",'.replace("\\", "/")
11095
initial = i - 1
11196
while lines[i].lower() > line_to_add.lower():
11297
i -= 1
@@ -171,10 +156,7 @@ def main() -> None:
171156
if os.path.exists(stub_dir):
172157
sys.exit(f"Error: {stub_dir} already exists (delete it first)")
173158

174-
run_stubgen(package)
175-
176-
# Stubs were generated under out/. Copy them to stubs/.
177-
copy_stubs("out", package, stub_dir)
159+
run_stubgen(package, stub_dir)
178160

179161
run_isort(stub_dir)
180162
run_black(stub_dir)

0 commit comments

Comments
 (0)