@@ -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
0 commit comments