Repository: pypa/pip
File: tests/unit/test_wheel_builder.py (Python)

Code before:


class ReqMock:
    def __init__(
        self,
        name: str = "pendulum",
        is_wheel: bool = False,
        editable: bool = False,
        link: Optional[Link] = None,
        constraint: bool = False,
        source_dir: Optional[str] = "/tmp/pip-install-123/pendulum",
        use_pep517: bool = True,
        supports_pyproject_editable: bool = False,
    ) -> None:
        self.name = name
        self.is_wheel = is_wheel
        self.editable = editable
        self.link = link
        self.constraint = constraint
        self.source_dir = source_dir
        self.use_pep517 = use_pep517
        self._supports_pyproject_editable = supports_pyproject_editable

    @property
    def supports_pyproject_editable(self) -> bool:
        return self._supports_pyproject_editable


@pytest.mark.parametrize(
    "req, expected",
    [
        # We build, whether pep 517 is enabled or not.
        (ReqMock(use_pep517=True), True),
        (ReqMock(use_pep517=False), True),
        # We don't build constraints.
        (ReqMock(constraint=True), False),
        # We don't build reqs that are already wheels.
        (ReqMock(is_wheel=True), False),
        # We build editables if the backend supports PEP 660.
        (ReqMock(editable=True, use_pep517=False), False),
        (
            ReqMock(editable=True, use_pep517=True, supports_pyproject_editable=True),
            True,
        ),
        (
            ReqMock(editable=True, use_pep517=True, supports_pyproject_editable=False),
            False,
        ),
        # We don't build if there is no source dir (whatever that means!).
        (ReqMock(source_dir=None), False),
        # By default (i.e. when binaries are allowed), VCS requirements

Review comment (suggestion):
We can simply rename the attribute instead. Or I think the entire class can be converted to dataclass!

Code after:
    assert result == expected


@dataclass
class ReqMock:
    name: str = "pendulum"
    is_wheel: bool = False
    editable: bool = False
    link: Optional[Link] = None
    constraint: bool = False
    source_dir: Optional[str] = "/tmp/pip-install-123/pendulum"
    use_pep517: bool = True
    supports_pyproject_editable: bool = False


@pytest.mark.parametrize(
    "req, expected",
    [
        # We build, whether pep 517 is enabled or not.
        (ReqMock(use_pep517=True), True),
        (ReqMock(use_pep517=False), True),
        # We don't build constraints.
        (ReqMock(constraint=True), False),
        # We don't build reqs that are already wheels.
        (ReqMock(is_wheel=True), False),
        # We build editables if the backend supports PEP 660.
        (ReqMock(editable=True, use_pep517=False), False),
        (
            ReqMock(editable=True, use_pep517=True, supports_pyproject_editable=True),
            True,
        ),
        (
            ReqMock(editable=True, use_pep517=True, supports_pyproject_editable=False),
            False,
        ),
        # We don't build if there is no source dir (whatever that means!).
        (ReqMock(source_dir=None), False),
        # By default (i.e. when binaries are allowed), VCS requirements
        # should be built in install mode.
        (
            ReqMock(link=Link("git+https://g.c/org/repo"), use_pep517=True),
            True,
        ),
        (
            ReqMock(link=Link("git+https://g.c/org/repo"), use_pep517=False),
            True,
        ),
    ],
)
def test_should_build_for_install_command(req: ReqMock, expected: bool) -> None:
    should_build = wheel_builder.should_build_for_install_command(