2929from colors import colored , print_error , print_success_msg
3030
3131SUPPORTED_VERSIONS = [(3 , 11 ), (3 , 10 ), (3 , 9 ), (3 , 8 ), (3 , 7 )]
32- SUPPORTED_PLATFORMS = frozenset ({ "linux" , "win32" , "darwin" } )
33- TYPESHED_DIRECTORIES = frozenset ({"stdlib" , "stubs" , "tests" , " test_cases" , "scripts " })
32+ SUPPORTED_PLATFORMS = ( "linux" , "win32" , "darwin" )
33+ TYPESHED_DIRECTORIES = frozenset ({"stdlib" , "stubs" , "test_cases" })
3434
35+ ReturnCode : TypeAlias = int
3536MajorVersion : TypeAlias = int
3637MinorVersion : TypeAlias = int
38+ MinVersion : TypeAlias = tuple [MajorVersion , MinorVersion ]
39+ MaxVersion : TypeAlias = tuple [MajorVersion , MinorVersion ]
3740Platform : TypeAlias = Annotated [str , "Must be one of the entries in SUPPORTED_PLATFORMS" ]
3841Directory : TypeAlias = Annotated [str , "Must be one of the entries in TYPESHED_DIRECTORIES" ]
3942
@@ -122,8 +125,6 @@ def match(fn: str, args: TestConfig) -> bool:
122125
123126
124127_VERSION_LINE_RE = re .compile (r"^([a-zA-Z_][a-zA-Z0-9_.]*): ([23]\.\d{1,2})-([23]\.\d{1,2})?$" )
125- MinVersion : TypeAlias = tuple [MajorVersion , MinorVersion ]
126- MaxVersion : TypeAlias = tuple [MajorVersion , MinorVersion ]
127128
128129
129130def parse_versions (fname : StrPath ) -> dict [str , tuple [MinVersion , MaxVersion ]]:
@@ -210,7 +211,7 @@ def add_configuration(configurations: list[MypyDistConf], distribution: str) ->
210211 configurations .append (MypyDistConf (module_name , values .copy ()))
211212
212213
213- def run_mypy (args : TestConfig , configurations : list [MypyDistConf ], files : list [str ], * , custom_typeshed : bool = False ) -> int :
214+ def run_mypy (args : TestConfig , configurations : list [MypyDistConf ], files : list [str ]) -> ReturnCode :
214215 try :
215216 from mypy .api import run as mypy_run
216217 except ImportError :
@@ -225,7 +226,7 @@ def run_mypy(args: TestConfig, configurations: list[MypyDistConf], files: list[s
225226 temp .write (f"{ k } = { v } \n " )
226227 temp .flush ()
227228
228- flags = get_mypy_flags (args , temp .name , custom_typeshed = custom_typeshed )
229+ flags = get_mypy_flags (args , temp .name )
229230 mypy_args = [* flags , * files ]
230231 if args .verbose :
231232 print ("running mypy" , " " .join (mypy_args ))
@@ -253,9 +254,6 @@ def run_mypy(args: TestConfig, configurations: list[MypyDistConf], files: list[s
253254 return exit_code
254255
255256
256- ReturnCode : TypeAlias = int
257-
258-
259257def run_mypy_as_subprocess (directory : StrPath , flags : Iterable [str ]) -> ReturnCode :
260258 result = subprocess .run ([sys .executable , "-m" , "mypy" , directory , * flags ], capture_output = True )
261259 stdout , stderr = result .stdout , result .stderr
@@ -267,14 +265,7 @@ def run_mypy_as_subprocess(directory: StrPath, flags: Iterable[str]) -> ReturnCo
267265
268266
269267def get_mypy_flags (
270- args : TestConfig ,
271- temp_name : str | None ,
272- * ,
273- custom_typeshed : bool = False ,
274- strict : bool = False ,
275- test_suite_run : bool = False ,
276- enforce_error_codes : bool = True ,
277- ignore_missing_imports : bool = False ,
268+ args : TestConfig , temp_name : str | None , * , strict : bool = False , enforce_error_codes : bool = True
278269) -> list [str ]:
279270 flags = [
280271 "--python-version" ,
@@ -285,27 +276,18 @@ def get_mypy_flags(
285276 "--no-error-summary" ,
286277 "--platform" ,
287278 args .platform ,
279+ "--no-site-packages" ,
280+ "--custom-typeshed-dir" ,
281+ os .path .dirname (os .path .dirname (__file__ )),
288282 ]
289283 if strict :
290284 flags .append ("--strict" )
291285 else :
292286 flags .extend (["--no-implicit-optional" , "--disallow-untyped-decorators" , "--disallow-any-generics" , "--strict-equality" ])
293287 if temp_name is not None :
294288 flags .extend (["--config-file" , temp_name ])
295- if custom_typeshed :
296- # Setting custom typeshed dir prevents mypy from falling back to its bundled
297- # typeshed in case of stub deletions
298- flags .extend (["--custom-typeshed-dir" , os .path .dirname (os .path .dirname (__file__ ))])
299- if test_suite_run :
300- flags .append ("--namespace-packages" )
301- if args .platform == "win32" :
302- flags .extend (["--exclude" , "tests/pytype_test.py" ])
303- else :
304- flags .append ("--no-site-packages" )
305289 if enforce_error_codes :
306290 flags .extend (["--enable-error-code" , "ignore-without-code" ])
307- if ignore_missing_imports :
308- flags .append ("--ignore-missing-imports" )
309291 return flags
310292
311293
@@ -389,8 +371,8 @@ def test_stdlib(code: int, args: TestConfig) -> TestResults:
389371
390372 if files :
391373 print (f"Testing stdlib ({ len (files )} files)..." )
392- print ("Running mypy " + " " .join (get_mypy_flags (args , "/tmp/..." , custom_typeshed = True )))
393- this_code = run_mypy (args , [], files , custom_typeshed = True )
374+ print ("Running mypy " + " " .join (get_mypy_flags (args , "/tmp/..." )))
375+ this_code = run_mypy (args , [], files )
394376 code = max (code , this_code )
395377
396378 return TestResults (code , len (files ))
@@ -414,44 +396,10 @@ def test_third_party_stubs(code: int, args: TestConfig) -> TestResults:
414396 return TestResults (code , files_checked )
415397
416398
417- def test_the_test_scripts (code : int , args : TestConfig ) -> TestResults :
418- files_to_test = list (Path ("tests" ).rglob ("*.py" ))
419- if args .platform == "win32" :
420- files_to_test .remove (Path ("tests/pytype_test.py" ))
421- num_test_files_to_test = len (files_to_test )
422- flags = get_mypy_flags (args , None , strict = True , test_suite_run = True )
423- print (f"Testing the test suite ({ num_test_files_to_test } files)..." )
424- print ("Running mypy " + " " .join (flags ))
425- if args .dry_run :
426- this_code = 0
427- else :
428- this_code = run_mypy_as_subprocess ("tests" , flags )
429- if not this_code :
430- print_success_msg ()
431- code = max (code , this_code )
432- return TestResults (code , num_test_files_to_test )
433-
434-
435- def test_scripts_directory (code : int , args : TestConfig ) -> TestResults :
436- files_to_test = list (Path ("scripts" ).rglob ("*.py" ))
437- num_test_files_to_test = len (files_to_test )
438- flags = get_mypy_flags (args , None , strict = True , ignore_missing_imports = True )
439- print (f"Testing the scripts directory ({ num_test_files_to_test } files)..." )
440- print ("Running mypy " + " " .join (flags ))
441- if args .dry_run :
442- this_code = 0
443- else :
444- this_code = run_mypy_as_subprocess ("scripts" , flags )
445- if not this_code :
446- print_success_msg ()
447- code = max (code , this_code )
448- return TestResults (code , num_test_files_to_test )
449-
450-
451399def test_the_test_cases (code : int , args : TestConfig ) -> TestResults :
452400 test_case_files = list (map (str , Path ("test_cases" ).rglob ("*.py" )))
453401 num_test_case_files = len (test_case_files )
454- flags = get_mypy_flags (args , None , strict = True , custom_typeshed = True , enforce_error_codes = False )
402+ flags = get_mypy_flags (args , None , strict = True , enforce_error_codes = False )
455403 print (f"Running mypy on the test_cases directory ({ num_test_case_files } files)..." )
456404 print ("Running mypy " + " " .join (flags ))
457405 if args .dry_run :
@@ -481,21 +429,6 @@ def test_typeshed(code: int, args: TestConfig) -> TestResults:
481429 files_checked_this_version += third_party_files_checked
482430 print ()
483431
484- if args .minor >= 9 :
485- # Run mypy against our own test suite and the scripts directory
486- #
487- # Skip this on earlier Python versions,
488- # as we're using new syntax and new functions in some test files
489- if "tests" in args .directories :
490- code , test_script_files_checked = test_the_test_scripts (code , args )
491- files_checked_this_version += test_script_files_checked
492- print ()
493-
494- if "scripts" in args .directories :
495- code , script_files_checked = test_scripts_directory (code , args )
496- files_checked_this_version += script_files_checked
497- print ()
498-
499432 if "test_cases" in args .directories :
500433 code , test_case_files_checked = test_the_test_cases (code , args )
501434 files_checked_this_version += test_case_files_checked
0 commit comments