Return the module and program names for a test case. Normally, the unit tests will parse the default ('__main__') module and follow all the imports listed there. You can override this behavior and instruct the tests to check multiple modules by using a comment like t
(
self, program_text: str, incremental_step: int = 0
)
| 313 | return set(missing.values()) |
| 314 | |
| 315 | def parse_module( |
| 316 | self, program_text: str, incremental_step: int = 0 |
| 317 | ) -> list[tuple[str, str, str]]: |
| 318 | """Return the module and program names for a test case. |
| 319 | |
| 320 | Normally, the unit tests will parse the default ('__main__') |
| 321 | module and follow all the imports listed there. You can override |
| 322 | this behavior and instruct the tests to check multiple modules |
| 323 | by using a comment like this in the test case input: |
| 324 | |
| 325 | # cmd: mypy -m foo.bar foo.baz |
| 326 | |
| 327 | You can also use `# cmdN:` to have a different cmd for incremental |
| 328 | step N (2, 3, ...). |
| 329 | |
| 330 | Return a list of tuples (module name, file name, program text). |
| 331 | """ |
| 332 | m = re.search("# cmd: mypy -m ([a-zA-Z0-9_. ]+)$", program_text, flags=re.MULTILINE) |
| 333 | if incremental_step > 1: |
| 334 | alt_regex = f"# cmd{incremental_step}: mypy -m ([a-zA-Z0-9_. ]+)$" |
| 335 | alt_m = re.search(alt_regex, program_text, flags=re.MULTILINE) |
| 336 | if alt_m is not None: |
| 337 | # Optionally return a different command if in a later step |
| 338 | # of incremental mode, otherwise default to reusing the |
| 339 | # original cmd. |
| 340 | m = alt_m |
| 341 | |
| 342 | if m: |
| 343 | # The test case wants to use a non-default main |
| 344 | # module. Look up the module and give it as the thing to |
| 345 | # analyze. |
| 346 | module_names = m.group(1) |
| 347 | out = [] |
| 348 | search_paths = SearchPaths((test_temp_dir,), (), (), ()) |
| 349 | cache = FindModuleCache(search_paths, fscache=None, options=None) |
| 350 | for module_name in module_names.split(" "): |
| 351 | path = cache.find_module(module_name) |
| 352 | assert isinstance(path, str), f"Can't find ad hoc case file: {module_name}" |
| 353 | with open(path, encoding="utf8") as f: |
| 354 | program_text = f.read() |
| 355 | out.append((module_name, path, program_text)) |
| 356 | return out |
| 357 | else: |
| 358 | return [("__main__", "main", program_text)] |
no test coverage detected