From 8d396cb5067dc404fb1624a6de8f747f23bda114 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 10 Apr 2020 17:01:48 +0200 Subject: [PATCH 1/5] Fixed an issue where .pyi files were ignored --- src/pytest_mypy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_mypy.py b/src/pytest_mypy.py index 9714cab..551aea9 100644 --- a/src/pytest_mypy.py +++ b/src/pytest_mypy.py @@ -75,7 +75,7 @@ def pytest_configure_node(self, node): # xdist hook def pytest_collect_file(path, parent): """Create a MypyFileItem for every file mypy should run on.""" - if path.ext == '.py' and any([ + if path.ext in {'.py', '.pyi'} and any([ parent.config.option.mypy, parent.config.option.mypy_ignore_missing_imports, ]): From f6aca0a355997d4e913f26bf4de14066c215723e Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 10 Apr 2020 18:14:08 +0200 Subject: [PATCH 2/5] Skip a .py file if an identically named .pyi file already exists Skip a .py file if an identically named .pyi file already exists as pytest will complain about duplicate modules otherwise. --- src/pytest_mypy.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pytest_mypy.py b/src/pytest_mypy.py index 551aea9..fc4d9b2 100644 --- a/src/pytest_mypy.py +++ b/src/pytest_mypy.py @@ -79,7 +79,17 @@ def pytest_collect_file(path, parent): parent.config.option.mypy, parent.config.option.mypy_ignore_missing_imports, ]): - return MypyFile.from_parent(parent=parent, fspath=path) + if path.ext == '.pyi': + return MypyFile.from_parent(parent=parent, fspath=path) + + # Do not create MypyFile instance for a .py file if a + # .pyi file with the same name already exists; + # pytest will complain about duplicate modules otherwise + path_pyi = str(path) + 'i' # i.e. a .pyi instead of a .py file + if os.path.isfile(path_pyi): + return None + else: + return MypyFile.from_parent(parent=parent, fspath=path) return None From f59e36c3b9a9db25323b7eb3862fde42bc815607 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 10 Apr 2020 18:16:56 +0200 Subject: [PATCH 3/5] Removed a redundant if/else statement --- src/pytest_mypy.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pytest_mypy.py b/src/pytest_mypy.py index fc4d9b2..c6e4b9b 100644 --- a/src/pytest_mypy.py +++ b/src/pytest_mypy.py @@ -86,9 +86,7 @@ def pytest_collect_file(path, parent): # .pyi file with the same name already exists; # pytest will complain about duplicate modules otherwise path_pyi = str(path) + 'i' # i.e. a .pyi instead of a .py file - if os.path.isfile(path_pyi): - return None - else: + if not os.path.isfile(path_pyi): return MypyFile.from_parent(parent=parent, fspath=path) return None From b2e175099b96d0a9b0135db7673b569c5f928903 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 10 Apr 2020 21:28:39 +0200 Subject: [PATCH 4/5] Added a test for .pyi files --- tests/test_pytest_mypy.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_pytest_mypy.py b/tests/test_pytest_mypy.py index 789405d..d00e824 100644 --- a/tests/test_pytest_mypy.py +++ b/tests/test_pytest_mypy.py @@ -36,6 +36,36 @@ def pyfunc(x: int) -> int: assert result.ret == 0 +def test_mypy_pyi(testdir, xdist_args): + """ + Verify that a .py file will be skipped if + a .pyi file exists with the same filename. + """ + # The incorrect signature below should be ignored + # as the .pyi file takes priority + testdir.makefile( + '.py', pyfile=''' + def pyfunc(x: int) -> str: + return x * 2 + ''' + ) + + testdir.makefile( + '.pyi', pyfile=''' + def pyfunc(x: int) -> int: ... + ''' + ) + + result = testdir.runpytest_subprocess(*xdist_args) + result.assert_outcomes() + result = testdir.runpytest_subprocess('--mypy', *xdist_args) + mypy_file_checks = 1 + mypy_status_check = 1 + mypy_checks = mypy_file_checks + mypy_status_check + result.assert_outcomes(passed=mypy_checks) + assert result.ret == 0 + + def test_mypy_error(testdir, xdist_args): """Verify that running on a module with type errors fails.""" testdir.makepyfile(''' From 063ce40b8bfd35d729c7a120bdba51d9ad7bcce0 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sun, 12 Apr 2020 14:38:47 +0200 Subject: [PATCH 5/5] Implemented https://github.com/dbader/pytest-mypy/pull/84#discussion_r407021212 --- src/pytest_mypy.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/pytest_mypy.py b/src/pytest_mypy.py index c6e4b9b..7c2ca8d 100644 --- a/src/pytest_mypy.py +++ b/src/pytest_mypy.py @@ -79,14 +79,10 @@ def pytest_collect_file(path, parent): parent.config.option.mypy, parent.config.option.mypy_ignore_missing_imports, ]): - if path.ext == '.pyi': - return MypyFile.from_parent(parent=parent, fspath=path) - # Do not create MypyFile instance for a .py file if a # .pyi file with the same name already exists; # pytest will complain about duplicate modules otherwise - path_pyi = str(path) + 'i' # i.e. a .pyi instead of a .py file - if not os.path.isfile(path_pyi): + if path.ext == '.pyi' or not path.new(ext='.pyi').isfile(): return MypyFile.from_parent(parent=parent, fspath=path) return None