diff options
author | Bas van Beek <b.f.van.beek@vu.nl> | 2021-01-21 18:35:14 +0100 |
---|---|---|
committer | Bas van Beek <b.f.van.beek@vu.nl> | 2021-01-21 19:18:01 +0100 |
commit | a6289b5c2b60b15371605ab95f5ed53dd60d7f14 (patch) | |
tree | 0de859b802613ec0b65b4ce71561441ebdc4ad99 | |
parent | b91f3c00ee113596acd3e508a593187258291f61 (diff) | |
download | numpy-a6289b5c2b60b15371605ab95f5ed53dd60d7f14.tar.gz |
TST: Run mypy once and cache the results
-rw-r--r-- | numpy/typing/tests/test_typing.py | 78 |
1 files changed, 33 insertions, 45 deletions
diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 18520a757..ef169b4ed 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -25,15 +25,40 @@ REVEAL_DIR = os.path.join(DATA_DIR, "reveal") MYPY_INI = os.path.join(DATA_DIR, "mypy.ini") CACHE_DIR = os.path.join(DATA_DIR, ".mypy_cache") +#: A dictionary with file names as keys and lists of the mypy stdout as values. +#: To-be populated by `run_mypy`. +OUTPUT_MYPY: Dict[str, List[str]] = {} + @pytest.mark.slow @pytest.mark.skipif(NO_MYPY, reason="Mypy is not installed") -@pytest.fixture(scope="session", autouse=True) -def clear_cache() -> None: - """Clears the mypy cache before running any of the typing tests.""" +@pytest.fixture(scope="module", autouse=True) +def run_mypy() -> None: + """Clears the cache and run mypy before running any of the typing tests. + + The mypy results are cached in `OUTPUT_MYPY` for further use. + + """ if os.path.isdir(CACHE_DIR): shutil.rmtree(CACHE_DIR) + for directory in (PASS_DIR, REVEAL_DIR, FAIL_DIR): + # Run mypy + stdout, stderr, _ = api.run([ + "--config-file", + MYPY_INI, + "--cache-dir", + CACHE_DIR, + directory, + ]) + assert not stderr, directory + stdout = stdout.replace('*', '') + + # Parse the output + key = lambda n: n.split(':', 1)[0] + iterator = itertools.groupby(stdout.split("\n"), key=key) + OUTPUT_MYPY.update((os.path.abspath(k), list(v)) for k, v in iterator) + def get_test_cases(directory): for root, _, files in os.walk(directory): @@ -54,15 +79,7 @@ def get_test_cases(directory): @pytest.mark.skipif(NO_MYPY, reason="Mypy is not installed") @pytest.mark.parametrize("path", get_test_cases(PASS_DIR)) def test_success(path): - stdout, stderr, exitcode = api.run([ - "--config-file", - MYPY_INI, - "--cache-dir", - CACHE_DIR, - path, - ]) - assert exitcode == 0, stdout - assert re.match(r"Success: no issues found in \d+ source files?", stdout.strip()) + assert path not in OUTPUT_MYPY @pytest.mark.slow @@ -71,29 +88,12 @@ def test_success(path): def test_fail(path): __tracebackhide__ = True - stdout, stderr, exitcode = api.run([ - "--config-file", - MYPY_INI, - "--cache-dir", - CACHE_DIR, - path, - ]) - assert exitcode != 0 - with open(path) as fin: lines = fin.readlines() errors = defaultdict(lambda: "") - error_lines = stdout.rstrip("\n").split("\n") - assert re.match( - r"Found \d+ errors? in \d+ files? \(checked \d+ source files?\)", - error_lines[-1].strip(), - ) - for error_line in error_lines[:-1]: - error_line = error_line.strip() - if not error_line: - continue - + assert path in OUTPUT_MYPY + for error_line in OUTPUT_MYPY[path]: match = re.match( r"^.+\.py:(?P<lineno>\d+): (error|note): .+$", error_line, @@ -215,23 +215,11 @@ def _parse_reveals(file: IO[str]) -> List[str]: def test_reveal(path): __tracebackhide__ = True - stdout, stderr, exitcode = api.run([ - "--config-file", - MYPY_INI, - "--cache-dir", - CACHE_DIR, - path, - ]) - with open(path) as fin: lines = _parse_reveals(fin) - stdout_list = stdout.replace('*', '').split("\n") - for error_line in stdout_list: - error_line = error_line.strip() - if not error_line: - continue - + assert path in OUTPUT_MYPY + for error_line in OUTPUT_MYPY[path]: match = re.match( r"^.+\.py:(?P<lineno>\d+): note: .+$", error_line, |