1919
2020if TYPE_CHECKING :
2121 assert sys .platform != "win32" , "pytype isn't yet installed in CI, but wheels can be built on Windows"
22+ from _typeshed import StrPath
2223if sys .version_info >= (3 , 13 ):
2324 print ("pytype does not support Python 3.13+ yet." , file = sys .stderr )
2425 sys .exit (1 )
3031import os
3132import traceback
3233from collections .abc import Iterable , Sequence
34+ from pathlib import Path
3335
3436# pytype is not py.typed https://github.com/google/pytype/issues/1325
3537from pytype import config as pytype_config , load_pytd # type: ignore[import]
@@ -94,21 +96,19 @@ def run_pytype(*, filename: str, python_version: str, missing_modules: Iterable[
9496 return stderr
9597
9698
97- def _get_relative (filename : str ) -> str :
98- top = 0
99+ def _get_relative (filename : StrPath ) -> Path :
100+ filepath = Path ( filename )
99101 for d in TYPESHED_SUBDIRS :
100102 try :
101- top = filename . index ( d + os . path . sep )
103+ return filepath . absolute (). relative_to ( Path ( d ). absolute (). parent )
102104 except ValueError :
103105 continue
104- else :
105- break
106- return filename [top :]
106+ raise ValueError (f"{ filepath } not relative to { TYPESHED_SUBDIRS } " )
107107
108108
109109def _get_module_name (filename : str ) -> str :
110110 """Convert a filename {subdir}/m.n/module/foo to module.foo."""
111- parts = _get_relative (filename ).split ( os . path . sep )
111+ parts = _get_relative (filename ).parts
112112 if parts [0 ] == "stdlib" :
113113 module_parts = parts [1 :]
114114 else :
@@ -134,7 +134,7 @@ def determine_files_to_test(*, paths: Sequence[str]) -> list[str]:
134134 stdlib_module_versions = parse_stdlib_versions_file ()
135135 files = []
136136 for f in sorted (filenames ):
137- if _get_relative (f ) in exclude_list :
137+ if _get_relative (f ). as_posix () in exclude_list :
138138 continue
139139 if not _is_supported_stdlib_version (stdlib_module_versions , f ):
140140 continue
@@ -154,7 +154,7 @@ def find_stubs_in_paths(paths: Sequence[str]) -> list[str]:
154154
155155
156156def _is_supported_stdlib_version (module_versions : SupportedVersionsDict , filename : str ) -> bool :
157- parts = _get_relative (filename ).split ( os . path . sep )
157+ parts = _get_relative (filename ).parts
158158 if parts [0 ] != "stdlib" :
159159 return True
160160 module_name = _get_module_name (filename )
@@ -227,17 +227,17 @@ def run_all_tests(*, files_to_test: Sequence[str], print_stderr: bool, dry_run:
227227 missing_modules = get_missing_modules (files_to_test )
228228 python_version = f"{ sys .version_info .major } .{ sys .version_info .minor } "
229229 print ("Testing files with pytype..." )
230- for i , f in enumerate (files_to_test ):
230+ for i , file_to_test in enumerate (files_to_test ):
231231 if dry_run :
232232 stderr = None
233233 else :
234- stderr = run_pytype (filename = f , python_version = python_version , missing_modules = missing_modules )
234+ stderr = run_pytype (filename = file_to_test , python_version = python_version , missing_modules = missing_modules )
235235 if stderr :
236236 if print_stderr :
237237 print (f"\n { stderr } " )
238238 errors += 1
239239 stacktrace_final_line = stderr .rstrip ().rsplit ("\n " , 1 )[- 1 ]
240- bad .append ((_get_relative (f ), python_version , stacktrace_final_line ))
240+ bad .append ((_get_relative (file_to_test ), python_version , stacktrace_final_line ))
241241
242242 runs = i + 1
243243 if runs % 25 == 0 :
0 commit comments