4444import textwrap
4545import time
4646from argparse import ArgumentParser , Namespace , RawDescriptionHelpFormatter
47- from typing import Any , Dict , List , Optional , Tuple
47+ from typing import Any , Dict , Tuple
4848from typing_extensions import TypeAlias as _TypeAlias
4949
5050CACHE_PATH : Final = ".incremental_checker_cache.json"
@@ -66,7 +66,7 @@ def delete_folder(folder_path: str) -> None:
6666 shutil .rmtree (folder_path )
6767
6868
69- def execute (command : List [str ], fail_on_error : bool = True ) -> Tuple [str , str , int ]:
69+ def execute (command : list [str ], fail_on_error : bool = True ) -> tuple [str , str , int ]:
7070 proc = subprocess .Popen (
7171 " " .join (command ), stderr = subprocess .PIPE , stdout = subprocess .PIPE , shell = True
7272 )
@@ -98,7 +98,7 @@ def initialize_repo(repo_url: str, temp_repo_path: str, branch: str) -> None:
9898 execute (["git" , "-C" , temp_repo_path , "checkout" , branch ])
9999
100100
101- def get_commits (repo_folder_path : str , commit_range : str ) -> List [ Tuple [str , str ]]:
101+ def get_commits (repo_folder_path : str , commit_range : str ) -> list [ tuple [str , str ]]:
102102 raw_data , _stderr , _errcode = execute (
103103 ["git" , "-C" , repo_folder_path , "log" , "--reverse" , "--oneline" , commit_range ]
104104 )
@@ -109,25 +109,25 @@ def get_commits(repo_folder_path: str, commit_range: str) -> List[Tuple[str, str
109109 return output
110110
111111
112- def get_commits_starting_at (repo_folder_path : str , start_commit : str ) -> List [ Tuple [str , str ]]:
112+ def get_commits_starting_at (repo_folder_path : str , start_commit : str ) -> list [ tuple [str , str ]]:
113113 print (f"Fetching commits starting at { start_commit } " )
114114 return get_commits (repo_folder_path , f"{ start_commit } ^..HEAD" )
115115
116116
117- def get_nth_commit (repo_folder_path : str , n : int ) -> Tuple [str , str ]:
117+ def get_nth_commit (repo_folder_path : str , n : int ) -> tuple [str , str ]:
118118 print (f"Fetching last { n } commits (or all, if there are fewer commits than n)" )
119119 return get_commits (repo_folder_path , f"-{ n } " )[0 ]
120120
121121
122122def run_mypy (
123- target_file_path : Optional [ str ] ,
123+ target_file_path : str | None ,
124124 mypy_cache_path : str ,
125- mypy_script : Optional [ str ] ,
125+ mypy_script : str | None ,
126126 * ,
127127 incremental : bool = False ,
128128 daemon : bool = False ,
129129 verbose : bool = False ,
130- ) -> Tuple [float , str , Dict [str , Any ]]:
130+ ) -> tuple [float , str , dict [str , Any ]]:
131131 """Runs mypy against `target_file_path` and returns what mypy prints to stdout as a string.
132132
133133 If `incremental` is set to True, this function will use store and retrieve all caching data
@@ -136,7 +136,7 @@ def run_mypy(
136136
137137 If `daemon` is True, we use daemon mode; the daemon must be started and stopped by the caller.
138138 """
139- stats = {} # type: Dict [str, Any]
139+ stats : dict [str , Any ] = {}
140140 if daemon :
141141 command = DAEMON_CMD + ["check" , "-v" ]
142142 else :
@@ -162,8 +162,8 @@ def run_mypy(
162162 return runtime , output , stats
163163
164164
165- def filter_daemon_stats (output : str ) -> Tuple [str , Dict [str , Any ]]:
166- stats = {} # type: Dict [str, Any]
165+ def filter_daemon_stats (output : str ) -> tuple [str , dict [str , Any ]]:
166+ stats : dict [str , Any ] = {}
167167 lines = output .splitlines ()
168168 output_lines = []
169169 for line in lines :
@@ -208,12 +208,12 @@ def save_cache(cache: JsonDict, incremental_cache_path: str = CACHE_PATH) -> Non
208208
209209
210210def set_expected (
211- commits : List [ Tuple [str , str ]],
211+ commits : list [ tuple [str , str ]],
212212 cache : JsonDict ,
213213 temp_repo_path : str ,
214- target_file_path : Optional [ str ] ,
214+ target_file_path : str | None ,
215215 mypy_cache_path : str ,
216- mypy_script : Optional [ str ] ,
216+ mypy_script : str | None ,
217217) -> None :
218218 """Populates the given `cache` with the expected results for all of the given `commits`.
219219
@@ -241,13 +241,13 @@ def set_expected(
241241
242242
243243def test_incremental (
244- commits : List [ Tuple [str , str ]],
244+ commits : list [ tuple [str , str ]],
245245 cache : JsonDict ,
246246 temp_repo_path : str ,
247- target_file_path : Optional [ str ] ,
247+ target_file_path : str | None ,
248248 mypy_cache_path : str ,
249249 * ,
250- mypy_script : Optional [ str ] = None ,
250+ mypy_script : str | None = None ,
251251 daemon : bool = False ,
252252 exit_on_error : bool = False ,
253253) -> None :
@@ -258,16 +258,16 @@ def test_incremental(
258258 """
259259 print ("Note: first commit is evaluated twice to warm up cache" )
260260 commits = [commits [0 ]] + commits
261- overall_stats = {} # type: Dict [str, float]
261+ overall_stats : dict [str , float ] = {}
262262 for commit_id , message in commits :
263263 print (f'Now testing commit { commit_id } : "{ message } "' )
264264 execute (["git" , "-C" , temp_repo_path , "checkout" , commit_id ])
265265 runtime , output , stats = run_mypy (
266266 target_file_path , mypy_cache_path , mypy_script , incremental = True , daemon = daemon
267267 )
268268 relevant_stats = combine_stats (overall_stats , stats )
269- expected_runtime = cache [commit_id ]["runtime" ] # type: float
270- expected_output = cache [commit_id ]["output" ] # type: str
269+ expected_runtime : float = cache [commit_id ]["runtime" ]
270+ expected_output : str = cache [commit_id ]["output" ]
271271 if output != expected_output :
272272 print (" Output does not match expected result!" )
273273 print (f" Expected output ({ expected_runtime :.3f} sec):" )
@@ -286,10 +286,10 @@ def test_incremental(
286286 print ("Overall stats:" , overall_stats )
287287
288288
289- def combine_stats (overall_stats : Dict [str , float ], new_stats : Dict [str , Any ]) -> Dict [str , float ]:
289+ def combine_stats (overall_stats : dict [str , float ], new_stats : dict [str , Any ]) -> dict [str , float ]:
290290 INTERESTING_KEYS = ["build_time" , "gc_time" ]
291291 # For now, we only support float keys
292- relevant_stats = {} # type: Dict [str, float]
292+ relevant_stats : dict [str , float ] = {}
293293 for key in INTERESTING_KEYS :
294294 if key in new_stats :
295295 value = float (new_stats [key ])
@@ -306,7 +306,7 @@ def cleanup(temp_repo_path: str, mypy_cache_path: str) -> None:
306306def test_repo (
307307 target_repo_url : str ,
308308 temp_repo_path : str ,
309- target_file_path : Optional [ str ] ,
309+ target_file_path : str | None ,
310310 mypy_path : str ,
311311 incremental_cache_path : str ,
312312 mypy_cache_path : str ,
@@ -391,9 +391,7 @@ def test_repo(
391391
392392
393393def main () -> None :
394- help_factory = lambda prog : RawDescriptionHelpFormatter (
395- prog = prog , max_help_position = 32
396- ) # type: Any
394+ help_factory : Any = lambda prog : RawDescriptionHelpFormatter (prog = prog , max_help_position = 32 )
397395 parser = ArgumentParser (
398396 prog = "incremental_checker" , description = __doc__ , formatter_class = help_factory
399397 )
0 commit comments