Skip to content
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

Add missing type hints - Part 2 #4801

Merged
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,5 @@ def do_lock(command_tester_factory: "CommandTesterFactory", poetry: "Poetry") ->


@pytest.fixture
def project_root():
def project_root() -> Path:
return Path(__file__).parent.parent
2 changes: 1 addition & 1 deletion tests/console/commands/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@pytest.fixture
def source_dir(tmp_path: Path) -> Path:
yield Path(tmp_path.as_posix())
return Path(tmp_path.as_posix())


@pytest.fixture
Expand Down
42 changes: 25 additions & 17 deletions tests/inspection/test_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
from typing import Set

import pytest
Expand All @@ -11,6 +12,9 @@
from poetry.utils.env import VirtualEnv


if TYPE_CHECKING:
from pytest_mock import MockerFixture

FIXTURE_DIR_BASE = Path(__file__).parent.parent / "fixtures"
FIXTURE_DIR_INSPECTIONS = FIXTURE_DIR_BASE / "inspection"

Expand All @@ -32,7 +36,7 @@ def demo_wheel() -> Path:

@pytest.fixture
def source_dir(tmp_path: Path) -> Path:
yield Path(tmp_path.as_posix())
return Path(tmp_path.as_posix())


@pytest.fixture
Expand All @@ -46,7 +50,7 @@ def demo_setup(source_dir: Path) -> Path:
'install_requires=["package"])'
)
)
yield source_dir
return source_dir


@pytest.fixture
Expand All @@ -65,7 +69,7 @@ def demo_setup_cfg(source_dir: Path) -> Path:
)
)
)
yield source_dir
return source_dir


@pytest.fixture
Expand All @@ -79,7 +83,7 @@ def demo_setup_complex(source_dir: Path) -> Path:
'install_requires=[i for i in ["package"]])'
)
)
yield source_dir
return source_dir


@pytest.fixture
Expand All @@ -88,7 +92,7 @@ def demo_setup_complex_pep517_legacy(demo_setup_complex: Path) -> Path:
pyproject_toml.write_text(
decode("[build-system]\n" 'requires = ["setuptools", "wheel"]')
)
yield demo_setup_complex
return demo_setup_complex


def demo_check_info(info: PackageInfo, requires_dist: Set[str] = None) -> None:
Expand All @@ -104,17 +108,17 @@ def demo_check_info(info: PackageInfo, requires_dist: Set[str] = None) -> None:
assert set(info.requires_dist) == requires_dist


def test_info_from_sdist(demo_sdist):
def test_info_from_sdist(demo_sdist: Path):
info = PackageInfo.from_sdist(demo_sdist)
demo_check_info(info)


def test_info_from_wheel(demo_wheel):
def test_info_from_wheel(demo_wheel: Path):
info = PackageInfo.from_wheel(demo_wheel)
demo_check_info(info)


def test_info_from_bdist(demo_wheel):
def test_info_from_bdist(demo_wheel: Path):
info = PackageInfo.from_bdist(demo_wheel)
demo_check_info(info)

Expand All @@ -133,12 +137,12 @@ def test_info_from_requires_txt():
demo_check_info(info)


def test_info_from_setup_py(demo_setup):
def test_info_from_setup_py(demo_setup: Path):
info = PackageInfo.from_setup_files(demo_setup)
demo_check_info(info, requires_dist={"package"})


def test_info_from_setup_cfg(demo_setup_cfg):
def test_info_from_setup_cfg(demo_setup_cfg: Path):
info = PackageInfo.from_setup_files(demo_setup_cfg)
demo_check_info(info, requires_dist={"package"})

Expand All @@ -153,26 +157,28 @@ def test_info_no_setup_pkg_info_no_deps():
assert info.requires_dist is None


def test_info_setup_simple(mocker, demo_setup):
def test_info_setup_simple(mocker: "MockerFixture", demo_setup: Path):
spy = mocker.spy(VirtualEnv, "run")
info = PackageInfo.from_directory(demo_setup)
assert spy.call_count == 0
demo_check_info(info, requires_dist={"package"})


def test_info_setup_cfg(mocker, demo_setup_cfg):
def test_info_setup_cfg(mocker: "MockerFixture", demo_setup_cfg: Path):
spy = mocker.spy(VirtualEnv, "run")
info = PackageInfo.from_directory(demo_setup_cfg)
assert spy.call_count == 0
demo_check_info(info, requires_dist={"package"})


def test_info_setup_complex(demo_setup_complex):
def test_info_setup_complex(demo_setup_complex: Path):
info = PackageInfo.from_directory(demo_setup_complex)
demo_check_info(info, requires_dist={"package"})


def test_info_setup_complex_pep517_error(mocker, demo_setup_complex):
def test_info_setup_complex_pep517_error(
mocker: "MockerFixture", demo_setup_complex: Path
):
mocker.patch(
"poetry.utils.env.VirtualEnv.run",
autospec=True,
Expand All @@ -183,12 +189,14 @@ def test_info_setup_complex_pep517_error(mocker, demo_setup_complex):
PackageInfo.from_directory(demo_setup_complex)


def test_info_setup_complex_pep517_legacy(demo_setup_complex_pep517_legacy):
def test_info_setup_complex_pep517_legacy(demo_setup_complex_pep517_legacy: Path):
info = PackageInfo.from_directory(demo_setup_complex_pep517_legacy)
demo_check_info(info, requires_dist={"package"})


def test_info_setup_complex_disable_build(mocker, demo_setup_complex):
def test_info_setup_complex_disable_build(
mocker: "MockerFixture", demo_setup_complex: Path
):
spy = mocker.spy(VirtualEnv, "run")
info = PackageInfo.from_directory(demo_setup_complex, disable_build=True)
assert spy.call_count == 0
Expand All @@ -199,7 +207,7 @@ def test_info_setup_complex_disable_build(mocker, demo_setup_complex):

@pytest.mark.parametrize("missing", ["version", "name", "install_requires"])
def test_info_setup_missing_mandatory_should_trigger_pep517(
mocker, source_dir, missing
mocker: "MockerFixture", source_dir: Path, missing: str
):
setup = "from setuptools import setup; "
setup += "setup("
Expand Down
13 changes: 10 additions & 3 deletions tests/installation/test_chef.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import TYPE_CHECKING

from packaging.tags import Tag

Expand All @@ -7,7 +8,13 @@
from poetry.utils.env import MockEnv


def test_get_cached_archive_for_link(config, mocker):
if TYPE_CHECKING:
from pytest_mock import MockerFixture

from tests.conftest import Config


def test_get_cached_archive_for_link(config: "Config", mocker: "MockerFixture"):
chef = Chef(
config,
MockEnv(
Expand Down Expand Up @@ -38,7 +45,7 @@ def test_get_cached_archive_for_link(config, mocker):
assert Link("file:///foo/demo-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl") == archive


def test_get_cached_archives_for_link(config, mocker):
def test_get_cached_archives_for_link(config: "Config", mocker: "MockerFixture"):
chef = Chef(
config,
MockEnv(
Expand All @@ -63,7 +70,7 @@ def test_get_cached_archives_for_link(config, mocker):
}


def test_get_cache_directory_for_link(config, config_cache_dir):
def test_get_cache_directory_for_link(config: "Config", config_cache_dir: Path):
chef = Chef(
config,
MockEnv(
Expand Down
65 changes: 41 additions & 24 deletions tests/installation/test_chooser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import re

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Type
from typing import Union

import pytest

Expand All @@ -14,6 +21,12 @@
from poetry.utils.env import MockEnv


if TYPE_CHECKING:
import httpretty

from httpretty.core import HTTPrettyRequest


JSON_FIXTURES = (
Path(__file__).parent.parent / "repositories" / "fixtures" / "pypi.org" / "json"
)
Expand All @@ -22,7 +35,7 @@


@pytest.fixture()
def env():
def env() -> MockEnv:
return MockEnv(
supported_tags=[
Tag("cp37", "cp37", "macosx_10_15_x86_64"),
Expand All @@ -32,8 +45,10 @@ def env():


@pytest.fixture()
def mock_pypi(http):
def callback(request, uri, headers):
def mock_pypi(http: Type["httpretty.httpretty"]) -> None:
def callback(
request: "HTTPrettyRequest", uri: str, headers: Dict[str, Any]
) -> Optional[List[Union[int, Dict[str, Any], str]]]:
parts = uri.rsplit("/")

name = parts[-3]
Expand All @@ -57,8 +72,10 @@ def callback(request, uri, headers):


@pytest.fixture()
def mock_legacy(http):
def callback(request, uri, headers):
def mock_legacy(http: Type["httpretty.httpretty"]) -> None:
def callback(
request: "HTTPrettyRequest", uri: str, headers: Dict[str, Any]
) -> List[Union[int, Dict[str, Any], str]]:
parts = uri.rsplit("/")
name = parts[-2]

Expand All @@ -75,7 +92,7 @@ def callback(request, uri, headers):


@pytest.fixture()
def pool():
def pool() -> Pool:
pool = Pool()

pool.add_repository(PyPiRepository(disable_cache=True))
Expand All @@ -88,7 +105,7 @@ def pool():

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_chooses_universal_wheel_link_if_available(
env, mock_pypi, mock_legacy, source_type, pool
env: MockEnv, mock_pypi: None, mock_legacy: None, source_type: str, pool: Pool
):
chooser = Chooser(pool, env)

Expand All @@ -109,7 +126,7 @@ def test_chooser_chooses_universal_wheel_link_if_available(

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_chooses_specific_python_universal_wheel_link_if_available(
env, mock_pypi, mock_legacy, source_type, pool
env: MockEnv, mock_pypi: None, mock_legacy: None, source_type: str, pool: Pool
):
chooser = Chooser(pool, env)

Expand All @@ -130,7 +147,7 @@ def test_chooser_chooses_specific_python_universal_wheel_link_if_available(

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_chooses_system_specific_wheel_link_if_available(
mock_pypi, mock_legacy, source_type, pool
mock_pypi: None, mock_legacy: None, source_type: str, pool: Pool
):
env = MockEnv(
supported_tags=[Tag("cp37", "cp37m", "win32"), Tag("py3", "none", "any")]
Expand All @@ -154,11 +171,11 @@ def test_chooser_chooses_system_specific_wheel_link_if_available(

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_chooses_sdist_if_no_compatible_wheel_link_is_available(
env,
mock_pypi,
mock_legacy,
source_type,
pool,
env: MockEnv,
mock_pypi: None,
mock_legacy: None,
source_type: str,
pool: Pool,
):
chooser = Chooser(pool, env)

Expand All @@ -179,11 +196,11 @@ def test_chooser_chooses_sdist_if_no_compatible_wheel_link_is_available(

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_chooses_distributions_that_match_the_package_hashes(
env,
mock_pypi,
mock_legacy,
source_type,
pool,
env: MockEnv,
mock_pypi: None,
mock_legacy: None,
source_type: str,
pool: Pool,
):
chooser = Chooser(pool, env)

Expand Down Expand Up @@ -212,11 +229,11 @@ def test_chooser_chooses_distributions_that_match_the_package_hashes(

@pytest.mark.parametrize("source_type", ["", "legacy"])
def test_chooser_throws_an_error_if_package_hashes_do_not_match(
env,
mock_pypi,
mock_legacy,
source_type,
pool,
env: MockEnv,
mock_pypi: None,
mock_legacy: None,
source_type: None,
pool: Pool,
):
chooser = Chooser(pool, env)

Expand Down
Loading