Skip to content

Commit f56185e

Browse files
Update local Pyright config generation
1 parent b94eee1 commit f56185e

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

.tools/scripts/local_pyrightconfig.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1-
"""Generate a local `pyrightconfig.json` variation for development."""
1+
"""Make a local `pyrightconfig.json` that includes the specified paths."""
22

3+
from collections.abc import Iterable
34
from json import dumps
45
from pathlib import Path
56
from tomllib import loads
67

8+
PYPROJECT = Path("pyproject.toml")
9+
"""Default `pyproject.toml`."""
10+
PYRIGHTCONFIG = Path("pyrightconfig.json")
11+
"""Default `pyrightconfig.json`."""
12+
OTHERS = [Path("../boilercore/src")]
13+
"""Default local paths to append."""
714

8-
def main():
9-
pyright = loads(Path("pyproject.toml").read_text("utf-8"))["tool"]["pyright"]
10-
pyright["include"] += [f"../boilercore/{path}" for path in pyright["include"]]
11-
Path("pyrightconfig.json").write_text(
12-
encoding="utf-8", data=f"{dumps(indent=2, obj=pyright)}\n"
13-
)
15+
16+
def make_local_pyrightconfig(
17+
pyproject: Path | str = PYPROJECT,
18+
pyrightconfig: Path = PYRIGHTCONFIG,
19+
others: Iterable[Path | str] = OTHERS,
20+
):
21+
"""Make a local `pyrightconfig.json` that includes the specified paths.
22+
23+
Duplicates config from `pyproject.toml` to `pyrightconfig.json` and appends
24+
specified paths to `include`. Enables type checking and refactoring across project
25+
boundaries, for instance with editable local dependencies during development of
26+
related projects in a multi-repo workflow.
27+
28+
Args:
29+
pyproject: Source `pyproject.toml`. Search project root by default.
30+
pyrightconfig: Destination `pyrightconfig.json`. Search project root by default.
31+
others: Local paths to append. Defaults to `[Path("../boilercore/src")]`.
32+
"""
33+
config = loads(Path(pyproject).read_text("utf-8"))["tool"]["pyright"]
34+
config = {
35+
"include": [
36+
*config.pop("include", []),
37+
*[str(Path(other).as_posix()) for other in others],
38+
],
39+
**config,
40+
}
41+
data = f"{dumps(indent=2, obj=config)}\n"
42+
Path(pyrightconfig).write_text(encoding="utf-8", data=data)
1443

1544

1645
if __name__ == "__main__":
17-
main()
46+
make_local_pyrightconfig()

0 commit comments

Comments
 (0)