Skip to content

Commit e1f6fe9

Browse files
authored
Add support for running Zuban as a type checker (#265)
1 parent 6cb3d1d commit e1f6fe9

5 files changed

Lines changed: 111 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mypy_primer is currently used in the CI of the following projects:
1717
- numpy
1818
- scipy-stubs
1919
- django-stubs
20+
- zuban
2021

2122
It has also inspired similar tooling for other projects in the code quality ecosystem.
2223
If you maintain a tool or project that could benefit from similar regression testing, let me know

‎mypy_primer/globals.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def parse_options(argv: list[str]) -> _Args:
8383
type_checker_group.add_argument(
8484
"--type-checker",
8585
default="mypy",
86-
choices=["mypy", "pyright", "ty", "pyrefly"],
86+
choices=["mypy", "pyright", "ty", "pyrefly", "zuban"],
8787
help="type checker to use",
8888
)
8989
type_checker_group.add_argument(
@@ -95,7 +95,7 @@ def parse_options(argv: list[str]) -> _Args:
9595
)
9696
type_checker_group.add_argument(
9797
"--cargo-profile",
98-
help="cargo build profile to use (e.g., 'dev', 'release', or custom profiles; only relevant for pyrefly/ty)",
98+
help="cargo build profile to use (e.g., 'dev', 'release', or custom profiles; only relevant for pyrefly/ty/zuban)",
9999
)
100100
type_checker_group.add_argument(
101101
"--mypyc-compile-level",
@@ -239,9 +239,9 @@ def parse_options(argv: list[str]) -> _Args:
239239
parser.error("--shard-index and --num-shards must be used together")
240240
if ret.type_checker != "mypy" and ret.mypyc_compile_level is not None:
241241
parser.error("--mypyc-compile-level can only be used with --type-checker=mypy")
242-
if ret.cargo_profile is not None and ret.type_checker not in {"pyrefly", "ty"}:
242+
if ret.cargo_profile is not None and ret.type_checker not in {"pyrefly", "ty", "zuban"}:
243243
parser.error(
244-
"`--cargo-profile` can only be used with `--type-checker=pyrefly` or `--type-checker=ty`"
244+
"`--cargo-profile` can only be used with `--type-checker=pyrefly`, `--type-checker=ty` or `--type-checker=zuban`"
245245
)
246246

247247
return ret

‎mypy_primer/main.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
setup_pyright,
2525
setup_ty,
2626
setup_typeshed,
27+
setup_zuban,
2728
)
2829
from mypy_primer.utils import Style, debug_print, get_npm, line_count, run, strip_colour_code
2930

@@ -56,6 +57,9 @@ def setup_type_checker(
5657
"typeshed_dir": typeshed_dir,
5758
"build_profile": ARGS.cargo_profile or "release",
5859
}
60+
elif ARGS.type_checker == "zuban":
61+
setup_fn = setup_zuban
62+
kwargs = {"repo": ARGS.repo, "build_profile": ARGS.cargo_profile or "release"}
5963
else:
6064
raise ValueError(f"Unknown type checker {ARGS.type_checker}")
6165

‎mypy_primer/model.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Project:
3737
pyright_cmd: str | None
3838
ty_cmd: str | None = None # TODO: remove this default
3939
pyrefly_cmd: str | None = None # TODO: remove this default
40+
zuban_cmd: str | None = None # TODO: remove this default
4041
paths: list[str] | None = None
4142

4243
install_cmd: str | None = None
@@ -68,6 +69,8 @@ def __repr__(self) -> str:
6869
result += f", ty_cmd={self.ty_cmd!r}"
6970
if self.pyrefly_cmd:
7071
result += f", pyrefly_cmd={self.pyrefly_cmd!r}"
72+
if self.zuban_cmd:
73+
result += f", zuban_cmd={self.zuban_cmd!r}"
7174
if self.paths:
7275
result += f", paths={self.paths!r}"
7376
if self.install_cmd:
@@ -404,6 +407,74 @@ async def run_pyrefly(
404407
runtime=runtime,
405408
)
406409

410+
def get_zuban_cmd(
411+
self, zuban: Path, typeshed_dir: Path | None, prepend_path: Path | None
412+
) -> str:
413+
zuban_cmd = self.zuban_cmd
414+
if zuban_cmd is None:
415+
zuban_cmd = "{zuban} check {paths}" if self.paths else "{zuban} check"
416+
assert "{zuban}" in zuban_cmd
417+
additional_flags = ctx.get().additional_flags.copy()
418+
if additional_flags:
419+
zuban_cmd += " " + " ".join(additional_flags)
420+
421+
zuban_cmd = zuban_cmd.format_map(_FormatMap(zuban=zuban, paths=self.paths))
422+
423+
zuban_cmd += f" --python-executable {quote_path(self.venv.dir)}/bin/python"
424+
if prepend_path is not None:
425+
zuban_cmd += f" --extra-search-path {quote_path(prepend_path)}"
426+
if typeshed_dir is None:
427+
# Zuban unfortunately needs to know where its typeshed dir is. This
428+
# is not needed if it was installed by a wheel, but since we
429+
# haven't technically installed Zuban we have to "guess" the
430+
# typeshed path, which is inside of the Zuban repository.
431+
base = zuban.parent.parent.parent
432+
repo_name = next(
433+
entry for entry in base.iterdir() if (entry / "third_party" / "typeshed").exists()
434+
)
435+
436+
typeshed_dir = base / repo_name / "third_party" / "typeshed"
437+
zuban_cmd += f" --custom-typeshed-dir {quote_path(typeshed_dir)}"
438+
return zuban_cmd
439+
440+
async def run_zuban(
441+
self, zuban: Path, typeshed_dir: Path | None, prepend_path: Path | None
442+
) -> TypeCheckResult:
443+
env = os.environ.copy()
444+
445+
zuban_cmd = self.get_zuban_cmd(zuban, typeshed_dir, prepend_path)
446+
proc, runtime = await run(
447+
zuban_cmd,
448+
shell=True,
449+
output=True,
450+
check=False,
451+
cwd=ctx.get().projects_dir / self.name,
452+
env=env,
453+
)
454+
if ctx.get().debug:
455+
debug_print(f"{Style.BLUE}{zuban} on {self.name} took {runtime:.2f}s{Style.RESET}")
456+
457+
if proc.returncode not in (0, 1):
458+
debug_print(proc.stderr + proc.stdout)
459+
if proc.returncode == 2:
460+
raise RuntimeError(
461+
f"Zuban exited with code 2 when checking {self.name!r}. This may indicate an internal problem (e.g. IO error)"
462+
)
463+
else:
464+
raise RuntimeError(
465+
f"Zuban did not exit with code 0, 1 or 2 when checking {self.name!r}. Panic?"
466+
)
467+
468+
output = proc.stderr + proc.stdout
469+
470+
return TypeCheckResult(
471+
zuban_cmd,
472+
output=output,
473+
success=not bool(proc.returncode),
474+
expected_success="zuban" in self.expected_success,
475+
runtime=runtime,
476+
)
477+
407478
async def run_typechecker(
408479
self, type_checker: Path, typeshed_dir: Path | None, *, prepend_path: Path | None
409480
) -> TypeCheckResult:
@@ -415,6 +486,8 @@ async def run_typechecker(
415486
return await self.run_ty(type_checker, typeshed_dir, prepend_path)
416487
elif ctx.get().type_checker == "pyrefly":
417488
return await self.run_pyrefly(type_checker, typeshed_dir, prepend_path)
489+
elif ctx.get().type_checker == "zuban":
490+
return await self.run_zuban(type_checker, typeshed_dir, prepend_path)
418491
else:
419492
raise ValueError(f"Unknown type checker: {ctx.get().type_checker}")
420493

‎mypy_primer/type_checker.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,35 @@ async def setup_pyrefly(
208208
return pyrefly_exe
209209

210210

211+
async def setup_zuban(
212+
zuban_dir: Path, revision_like: RevisionLike, *, build_profile: str, repo: str | None
213+
) -> Path:
214+
zuban_dir.mkdir(parents=True, exist_ok=True)
215+
216+
if repo is None:
217+
repo = "https://github.com/zubanls/zuban"
218+
repo_dir = await ensure_repo_at_revision(repo, zuban_dir, revision_like)
219+
220+
cargo_target_dir = zuban_dir / "target"
221+
env = os.environ.copy()
222+
env["CARGO_TARGET_DIR"] = str(cargo_target_dir)
223+
224+
if not os.environ.get("MYPY_PRIMER_NO_REBUILD", False):
225+
try:
226+
await run(
227+
["cargo", "build", "--profile", build_profile], cwd=repo_dir, env=env, output=True
228+
)
229+
except subprocess.CalledProcessError as e:
230+
print("Error while building 'zuban'", file=sys.stderr)
231+
print(e.stdout, file=sys.stderr)
232+
print(e.stderr, file=sys.stderr)
233+
raise e
234+
235+
zuban_exe = cargo_target_dir / _cargo_build_artifact_directory(build_profile) / "zuban"
236+
assert zuban_exe.exists()
237+
return zuban_exe
238+
239+
211240
async def setup_typeshed(parent_dir: Path, *, repo: str, revision_like: RevisionLike) -> Path:
212241
if parent_dir.exists():
213242
shutil.rmtree(parent_dir)

0 commit comments

Comments
 (0)