|
1 |
| -"""Generate a local `pyrightconfig.json` variation for development.""" |
| 1 | +"""Make a local `pyrightconfig.json` that includes the specified paths.""" |
2 | 2 |
|
| 3 | +from collections.abc import Iterable |
3 | 4 | from json import dumps
|
4 | 5 | from pathlib import Path
|
5 | 6 | from tomllib import loads
|
6 | 7 |
|
| 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.""" |
7 | 14 |
|
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) |
14 | 43 |
|
15 | 44 |
|
16 | 45 | if __name__ == "__main__":
|
17 |
| - main() |
| 46 | + make_local_pyrightconfig() |
0 commit comments