Skip to content

Commit

Permalink
Switch PEP 561 tests to a different build backend (#15451)
Browse files Browse the repository at this point in the history
Fixes #15446

setuptools has broken how editable installs work. It seems like most
other build backends continue to use static pth files, so let's just
switch to another one.
  • Loading branch information
hauntsaninja committed Jun 16, 2023
1 parent 65a715a commit db5b5af
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 97 deletions.
32 changes: 15 additions & 17 deletions mypy/test/testpep561.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,16 @@ def virtualenv(python_executable: str = sys.executable) -> Iterator[tuple[str, s


def install_package(
pkg: str, python_executable: str = sys.executable, use_pip: bool = True, editable: bool = False
pkg: str, python_executable: str = sys.executable, editable: bool = False
) -> None:
"""Install a package from test-data/packages/pkg/"""
working_dir = os.path.join(package_path, pkg)
with tempfile.TemporaryDirectory() as dir:
if use_pip:
install_cmd = [python_executable, "-m", "pip", "install"]
if editable:
install_cmd.append("-e")
install_cmd.append(".")
else:
install_cmd = [python_executable, "setup.py"]
if editable:
install_cmd.append("develop")
else:
install_cmd.append("install")
install_cmd = [python_executable, "-m", "pip", "install"]
if editable:
install_cmd.append("-e")
install_cmd.append(".")

# Note that newer versions of pip (21.3+) don't
# follow this env variable, but this is for compatibility
env = {"PIP_BUILD": dir}
Expand All @@ -82,21 +76,25 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
assert testcase.old_cwd is not None, "test was not properly set up"
python = sys.executable

if sys.version_info < (3, 8) and testcase.location[-1] == "testTypedPkgSimpleEditable":
# Python 3.7 doesn't ship with new enough pip to support PEP 660
# This is a quick hack to skip the test; we'll drop Python 3.7 support soon enough
return

assert python is not None, "Should be impossible"
pkgs, pip_args = parse_pkgs(testcase.input[0])
mypy_args = parse_mypy_args(testcase.input[1])
use_pip = True
editable = False
for arg in pip_args:
if arg == "no-pip":
use_pip = False
elif arg == "editable":
if arg == "editable":
editable = True
else:
raise ValueError(f"Unknown pip argument: {arg}")
assert pkgs, "No packages to install for PEP 561 test?"
with virtualenv(python) as venv:
venv_dir, python_executable = venv
for pkg in pkgs:
install_package(pkg, python_executable, use_pip, editable)
install_package(pkg, python_executable, editable)

cmd_line = list(mypy_args)
has_program = not ("-p" in cmd_line or "--package" in cmd_line)
Expand Down
11 changes: 11 additions & 0 deletions test-data/packages/typedpkg-stubs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = 'typedpkg-stubs'
version = '0.1'
description = 'test'

[tool.hatch.build]
include = ["**/*.pyi"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
13 changes: 0 additions & 13 deletions test-data/packages/typedpkg-stubs/setup.py

This file was deleted.

8 changes: 8 additions & 0 deletions test-data/packages/typedpkg/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = 'typedpkg'
version = '0.1'
description = 'test'

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
15 changes: 0 additions & 15 deletions test-data/packages/typedpkg/setup.py

This file was deleted.

11 changes: 11 additions & 0 deletions test-data/packages/typedpkg_ns_a/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = 'typedpkg_namespace.alpha'
version = '0.1'
description = 'test'

[tool.hatch.build]
include = ["**/*.py", "**/*.pyi", "**/py.typed"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
10 changes: 0 additions & 10 deletions test-data/packages/typedpkg_ns_a/setup.py

This file was deleted.

11 changes: 11 additions & 0 deletions test-data/packages/typedpkg_ns_b-stubs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = 'typedpkg_ns-stubs'
version = '0.1'
description = 'test'

[tool.hatch.build]
include = ["**/*.pyi"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
14 changes: 0 additions & 14 deletions test-data/packages/typedpkg_ns_b-stubs/setup.py

This file was deleted.

8 changes: 8 additions & 0 deletions test-data/packages/typedpkg_ns_b/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = 'typedpkg_namespace.beta'
version = '0.1'
description = 'test'

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
10 changes: 0 additions & 10 deletions test-data/packages/typedpkg_ns_b/setup.py

This file was deleted.

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

[case testTypedPkgSimpleEgg]
# pkgs: typedpkg; no-pip
from typedpkg.sample import ex
from typedpkg import dne
a = ex([''])
reveal_type(a)
[out]
testTypedPkgSimpleEgg.py:5: note: Revealed type is "builtins.tuple[builtins.str, ...]"

[case testTypedPkgSimpleEditable]
# pkgs: typedpkg; editable
from typedpkg.sample import ex
Expand All @@ -90,15 +81,6 @@ reveal_type(a)
[out]
testTypedPkgSimpleEditable.py:5: note: Revealed type is "builtins.tuple[builtins.str, ...]"

[case testTypedPkgSimpleEditableEgg]
# pkgs: typedpkg; editable; no-pip
from typedpkg.sample import ex
from typedpkg import dne
a = ex([''])
reveal_type(a)
[out]
testTypedPkgSimpleEditableEgg.py:5: note: Revealed type is "builtins.tuple[builtins.str, ...]"

[case testTypedPkgNamespaceImportFrom]
# pkgs: typedpkg, typedpkg_ns_a
from typedpkg.pkg.aaa import af
Expand Down

0 comments on commit db5b5af

Please sign in to comment.