You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Single threaded pytest spuriously fails for testcmdline and testpythoneval #4127
Sadly there's a problem with this. When I run the tests as follows:
pytest -n0 mypy/test/testcmdline.py
every test in that file fails. Apparently the pytest-xdist extension which runs tests in parallel does something that keeps the tests from failing. The error is essentially that every subprocess invoking scripts/mypy fails with the following traceback:
Traceback (most recent call last):
File "/Users/guido/src/mypy/scripts/mypy", line 4, in <module>
from mypy.main import main
ModuleNotFoundError: No module named 'mypy.main'
This makes sense as the current directory is set to the temporary directory where the test files are written, so the repo root is not on sys.path. I'm guessing that the xdist extension somehow retains the current directory?
The pythoneval tests fail in the same way.
(End quote)
On my machine most of the tests pass, except testCoberturaParser:
The following patch fixed the problem for me, but I can't reproduce the more serious issue, so it might not be the right solution (this diff is only about testcmdline; pythoneval should be similar):
diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py
index 26f1780..ede8f30 100644
--- a/mypy/test/testcmdline.py
+++ b/mypy/test/testcmdline.py
@@ -12,7 +12,7 @@ import sys
from typing import Tuple, List, Dict, Set
from mypy.myunit import Suite, SkipTestCaseException, AssertionFailure
-from mypy.test.config import test_data_prefix, test_temp_dir
+from mypy.test.config import test_data_prefix, test_temp_dir, PREFIX
from mypy.test.data import fix_cobertura_filename
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
from mypy.test.helpers import assert_string_arrays_equal, normalize_error_messages
@@ -58,10 +58,13 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
# Type check the program.
fixed = [python3_path,
os.path.join(testcase.old_cwd, 'scripts', 'mypy')]
+ env = os.environ.copy()
+ env['PYTHONPATH'] = PREFIX
process = subprocess.Popen(fixed + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
- cwd=test_temp_dir)
+ cwd=test_temp_dir,
+ env=env)
outb = process.stdout.read()
# Split output into lines.
out = [s.rstrip('\n\r') for s in str(outb, 'utf8').splitlines()]
Yeah, this fixes it for me. I tried pythoneval.py as well and it passes with -n0 -- so it doesn't need this fix. This is because it doesn't fork a subprocess to run mypy -- it uses mypy.api.run(). Perhaps testcmdline.py could be modified to do that too? I don't think we gain much by forking a process. (Though we don't seem to lose much either.)
When I moved pythoneval to mypy.api.run() I also considered moving testcmdline.py to it as well but did not, as I worried about giving up testing the actual OS<->mypy interaction. On the other hand, I feel that it hasn't really ever been an issue, and there is not much that can go wrong without Python itself being broken.
Maybe we can include 1-2 separate tests of the actual OS<->mypy
interaction; one for a passing program and one for a failing program.
(Maybe a few more to exercise the exceptional workflows that raise
CompileError prematurely, e.g. missing file or syntax errors.)
Activity
elazarg commentedon Oct 17, 2017
The following patch fixed the problem for me, but I can't reproduce the more serious issue, so it might not be the right solution (this diff is only about
testcmdline; pythoneval should be similar):gvanrossum commentedon Oct 17, 2017
Thanks! I'll try it when I have more time. It may miraculously work.
gvanrossum commentedon Oct 17, 2017
Yeah, this fixes it for me. I tried pythoneval.py as well and it passes with -
n0-- so it doesn't need this fix. This is because it doesn't fork a subprocess to run mypy -- it usesmypy.api.run(). Perhaps testcmdline.py could be modified to do that too? I don't think we gain much by forking a process. (Though we don't seem to lose much either.)emmatyping commentedon Oct 17, 2017
When I moved pythoneval to
mypy.api.run()I also considered moving testcmdline.py to it as well but did not, as I worried about giving up testing the actual OS<->mypy interaction. On the other hand, I feel that it hasn't really ever been an issue, and there is not much that can go wrong without Python itself being broken.gvanrossum commentedon Oct 17, 2017
Fix testcmdline when it is run with py.test -n0
Fix testcmdline when it is run with py.test -n0 (#5113)