Skip to content

Put filelock around PEP 561 tests (fixes #12615) #12857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ venv/
.mypy_cache/
.incremental_checker_cache.json
.cache
test-data/packages/.pip_lock
dmypy.json
.dmypy.json

Expand Down
9 changes: 9 additions & 0 deletions mypy/test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@
# This is *within* the tempfile.TemporaryDirectory that is chroot'ed per testcase.
# It is also hard-coded in numerous places, so don't change it.
test_temp_dir = 'tmp'

# The PEP 561 tests do a bunch of pip installs which, even though they operate
# on distinct temporary virtual environments, run into race conditions on shared
# file-system state. To make this work reliably in parallel mode, we'll use a
# FileLock courtesy of the tox-dev/py-filelock package.
# Ref. https://github.com/python/mypy/issues/12615
# Ref. mypy/test/testpep561.py
pip_lock = os.path.join(package_path, '.pip_lock')
pip_timeout = 60
17 changes: 11 additions & 6 deletions mypy/test/testpep561.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import contextmanager
import filelock
import os
import pytest
import re
Expand All @@ -9,7 +10,7 @@
from typing import Tuple, List, Generator

import mypy.api
from mypy.test.config import package_path
from mypy.test.config import package_path, pip_lock, pip_timeout
from mypy.util import try_find_python2_interpreter
from mypy.test.data import DataDrivenTestCase, DataSuite
from mypy.test.config import test_temp_dir
Expand Down Expand Up @@ -77,11 +78,15 @@ def install_package(pkg: str,
env = {'PIP_BUILD': dir}
# Inherit environment for Windows
env.update(os.environ)
proc = subprocess.run(install_cmd,
cwd=working_dir,
stdout=PIPE,
stderr=PIPE,
env=env)
try:
with filelock.FileLock(pip_lock, timeout=pip_timeout):
proc = subprocess.run(install_cmd,
cwd=working_dir,
stdout=PIPE,
stderr=PIPE,
env=env)
except filelock.Timeout as err:
raise Exception("Failed to acquire {}".format(pip_lock)) from err
if proc.returncode != 0:
raise Exception(proc.stdout.decode('utf-8') + proc.stderr.decode('utf-8'))

Expand Down
6 changes: 5 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@
'pytest-slow': ['pytest', '-q', '-k', ' or '.join(
[SAMPLES,
TYPESHED,
PEP561,
DAEMON,
MYPYC_EXTERNAL,
MYPYC_COMMAND_LINE,
ERROR_STREAM])],

# Test cases that might take minutes to run
'pytest-slower': ['pytest', '-q', '-k', ' or '.join(
[PEP561])],

# Test cases to run in typeshed CI
'typeshed-ci': ['pytest', '-q', '-k', ' or '.join([CMDLINE,
EVALUATION,
Expand Down
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-r mypy-requirements.txt
-r build-requirements.txt
attrs>=18.0
filelock>=3.0.0,<3.4.2; python_version<'3.7'
filelock>=3.0.0; python_version>='3.7'
flake8==3.9.2
flake8-bugbear==22.3.20
flake8-pyi>=20.5
Expand Down