Skip to content

Commit

Permalink
Drop need for virtualenv (#13136)
Browse files Browse the repository at this point in the history
Linking #12237
  • Loading branch information
hauntsaninja committed Jul 18, 2022
1 parent 37080f5 commit ed5a2a0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
python-version: '3.7'
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
run: pip install --upgrade 'setuptools!=50' tox==3.24.5
- name: Setup tox environment
run: tox -e ${{ env.TOXENV }} --notest
- name: Test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
source $VENV/bin/activate
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
run: pip install --upgrade 'setuptools!=50' tox==3.24.5
- name: Compiled with mypyc
if: ${{ matrix.test_mypyc }}
run: |
Expand All @@ -128,7 +128,7 @@ jobs:
with:
python-version: '3.11-dev'
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
run: pip install --upgrade 'setuptools!=50' tox==3.24.5
- name: Setup tox environment
run: tox -e py --notest
- name: Test
Expand Down
31 changes: 10 additions & 21 deletions mypy/test/testpep561.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from contextlib import contextmanager
import filelock
import os
import pytest
import re
import subprocess
from subprocess import PIPE
import sys
import tempfile
from typing import Tuple, List, Generator
from typing import Tuple, List, Iterator

import mypy.api
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
from mypy.test.helpers import assert_string_arrays_equal, perform_file_operations
Expand All @@ -32,23 +30,19 @@ def run_case(self, test_case: DataDrivenTestCase) -> None:


@contextmanager
def virtualenv(
python_executable: str = sys.executable
) -> Generator[Tuple[str, str], None, None]:
def virtualenv(python_executable: str = sys.executable) -> Iterator[Tuple[str, str]]:
"""Context manager that creates a virtualenv in a temporary directory
returns the path to the created Python executable"""
# Sadly, we need virtualenv, as the Python 3 venv module does not support creating a venv
# for Python 2, and Python 2 does not have its own venv.
Returns the path to the created Python executable
"""
with tempfile.TemporaryDirectory() as venv_dir:
proc = subprocess.run([sys.executable,
'-m',
'virtualenv',
f'-p{python_executable}',
venv_dir], cwd=os.getcwd(), stdout=PIPE, stderr=PIPE)
proc = subprocess.run(
[python_executable, '-m', 'venv', venv_dir],
cwd=os.getcwd(), stdout=PIPE, stderr=PIPE
)
if proc.returncode != 0:
err = proc.stdout.decode('utf-8') + proc.stderr.decode('utf-8')
raise Exception("Failed to create venv. Do you have virtualenv installed?\n" + err)
raise Exception("Failed to create venv.\n" + err)
if sys.platform == 'win32':
yield venv_dir, os.path.abspath(os.path.join(venv_dir, 'Scripts', 'python'))
else:
Expand Down Expand Up @@ -94,12 +88,7 @@ def install_package(pkg: str,
def test_pep561(testcase: DataDrivenTestCase) -> None:
"""Test running mypy on files that depend on PEP 561 packages."""
assert testcase.old_cwd is not None, "test was not properly set up"
if 'python2' in testcase.name.lower():
python = try_find_python2_interpreter()
if python is None:
pytest.skip()
else:
python = sys.executable
python = sys.executable

assert python is not None, "Should be impossible"
pkgs, pip_args = parse_pkgs(testcase.input[0])
Expand Down
2 changes: 1 addition & 1 deletion mypyc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ First clone the mypy git repository:

Optionally create a virtualenv (recommended):

$ virtualenv -p python3 <directory>
$ python3 -m venv <directory>
$ source <directory>/bin/activate

Then install the dependencies:
Expand Down
18 changes: 9 additions & 9 deletions mypyc/test-data/run-imports.test
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ def test_import_as_submodule_within_function() -> None:
# assert 'nob' not in globals()

def test_import_module_without_stub_in_function() -> None:
# 'virtualenv' must not have a stub in typeshed for this test case
import virtualenv # type: ignore
# 'psutil' must not have a stub in typeshed for this test case
import psutil # type: ignore
# TODO: We shouldn't add local imports to globals()
# assert 'virtualenv' not in globals()
assert isinstance(virtualenv.__name__, str)
# assert 'psutil' not in globals()
assert isinstance(psutil.__name__, str)

def test_import_as_module_without_stub_in_function() -> None:
# 'virtualenv' must not have a stub in typeshed for this test case
import virtualenv as vv # type: ignore
assert 'virtualenv' not in globals()
# 'psutil' must not have a stub in typeshed for this test case
import psutil as pp # type: ignore
assert 'psutil' not in globals()
# TODO: We shouldn't add local imports to globals()
# assert 'vv' not in globals()
assert isinstance(vv.__name__, str)
# assert 'pp' not in globals()
assert isinstance(pp.__name__, str)

[file testmodule.py]
def factorial(x: int) -> int:
Expand Down
10 changes: 0 additions & 10 deletions test-data/unit/pep561.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ reveal_type(a)
[out]
testStubPrecedence.py:5: note: Revealed type is "builtins.list[builtins.str]"

[case testTypedPkgStubs_python2]
# pkgs: typedpkg-stubs
from typedpkg.sample import ex
from typedpkg import dne
a = ex([''])
reveal_type(a)
[out]
testTypedPkgStubs_python2.py:3: error: Module "typedpkg" has no attribute "dne"
testTypedPkgStubs_python2.py:5: note: Revealed type is "builtins.list[builtins.str]"

[case testTypedPkgSimpleEgg]
# pkgs: typedpkg; no-pip
from typedpkg.sample import ex
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pytest-forked>=1.3.0,<2.0.0
pytest-cov>=2.10.0,<3.0.0
py>=1.5.2
typed_ast>=1.5.4,<2; python_version>='3.8'
virtualenv>=20.6.0
setuptools!=50
six
importlib-metadata>=4.6.1,<5.0.0

0 comments on commit ed5a2a0

Please sign in to comment.