Repository: pypa/pip
File: tests/functional/test_build_env.py (Python)

Code before:

def run_with_build_env(
    script: PipTestEnvironment,
    setup_script_contents: str,
    test_script_contents: Optional[str] = None,
) -> TestPipResult:
    build_env_script = script.scratch_path / "build_env.py"
    build_env_script.write_text(
        dedent(
            f"""
            import subprocess
            import sys

            from pip._internal.build_env import BuildEnvironment
            from pip._internal.index.collector import LinkCollector
            from pip._internal.index.package_finder import PackageFinder
            from pip._internal.models.search_scope import SearchScope
            from pip._internal.models.selection_prefs import (
                SelectionPreferences
            )
            from pip._internal.network.session import PipSession
            from pip._internal.utils.temp_dir import global_tempdir_manager

            link_collector = LinkCollector(
                session=PipSession(),
                search_scope=SearchScope.create([{str(script.scratch_path)!r}], [], False),
            )
            selection_prefs = SelectionPreferences(
                allow_yanked=True,
            )
            finder = PackageFinder.create(
                link_collector=link_collector,
                selection_prefs=selection_prefs,
            )

            with global_tempdir_manager():
                build_env = BuildEnvironment()
            """  # noqa: E501
        )
        + indent(dedent(setup_script_contents), "    ")
        + indent(
            dedent(
                """
                if len(sys.argv) > 1:
                    with build_env:
                        subprocess.check_call((sys.executable, sys.argv[1]))
                """
            ),
            "    ",
        )
    )

Review comment (suggestion):
A local `scratch_path` variable would avoid the E501.

Code after:

def run_with_build_env(
    script: PipTestEnvironment,
    setup_script_contents: str,
    test_script_contents: Optional[str] = None,
) -> TestPipResult:
    build_env_script = script.scratch_path / "build_env.py"
    scratch_path = str(script.scratch_path)
    build_env_script.write_text(
        dedent(
            f"""
            import subprocess
            import sys

            from pip._internal.build_env import BuildEnvironment
            from pip._internal.index.collector import LinkCollector
            from pip._internal.index.package_finder import PackageFinder
            from pip._internal.models.search_scope import SearchScope
            from pip._internal.models.selection_prefs import (
                SelectionPreferences
            )
            from pip._internal.network.session import PipSession
            from pip._internal.utils.temp_dir import global_tempdir_manager

            link_collector = LinkCollector(
                session=PipSession(),
                search_scope=SearchScope.create([{scratch_path!r}], [], False),
            )
            selection_prefs = SelectionPreferences(
                allow_yanked=True,
            )
            finder = PackageFinder.create(
                link_collector=link_collector,
                selection_prefs=selection_prefs,
            )

            with global_tempdir_manager():
                build_env = BuildEnvironment()
            """
        )
        + indent(dedent(setup_script_contents), "    ")
        + indent(
            dedent(
                """
                if len(sys.argv) > 1:
                    with build_env:
                        subprocess.check_call((sys.executable, sys.argv[1]))
                """
            ),
            "    ",
        )