Repository: pypa/pip
File: src/pip/_internal/cli/req_command.py (Python)

Code before:
                )
                requirements.append(req_to_add)

        if options.scripts:
            if len(options.scripts) > 1:
                raise CommandError("--script can only be given once")

            script = options.scripts[0]
            script_metadata = pep723_metadata(script)

            script_requires_python = script_metadata.get("requires-python", "")

            if script_requires_python and not options.ignore_requires_python:
                target_python = make_target_python(options)

                if not check_requires_python(
                    requires_python=script_requires_python,
                    version_info=target_python.py_version_info,
                ):
                    raise UnsupportedPythonVersion(
                        f"Script {script!r} requires a different Python: "
                        f"{target_python.py_version} not in {script_requires_python!r}"
                    )

            for req in script_metadata.get("dependencies", []):
                requirements.append(
                    InstallRequirement(Requirement(req), comes_from=None)
                )

        # If any requirement has hash options, enable hash checking.
        if any(req.has_hash_options for req in requirements):
            options.require_hashes = True

        if not (args or options.editables or options.requirements or options.scripts):
            opts = {"name": self.name}
            if options.find_links:
                raise CommandError(
                    "You must give at least one requirement to {name} "
                    '(maybe you meant "pip {name} {links}"?)'.format(
                        **dict(opts, links=" ".join(options.find_links))
                    )
                )
            else:
                raise CommandError(
                    "You must give at least one requirement to {name} "
                    '(see "pip help {name}")'.format(**opts)
                )

        return requirements

    @staticmethod

Review comment (suggestion):
`ruff` is upset that I wrote a loop with `.append()` here (`PERF401`) but I don't know if a comprehension would really be justified here? I can maybe see the case for `requirements.extend(...)` but it still seems like it'd be harder to read to me. Looking for feedback.

Edit: here's a diff of the `extend()` version that passes the new tests:

```diff
diff --git a/src/pip/_internal/cli/req_command.py b/src/pip/_internal/cli/req_command.py
index 294dc9f40..1f972eba4 100644
--- a/src/pip/_internal/cli/req_command.py
+++ b/src/pip/_internal/cli/req_command.py
@@ -298,10 +298,10 @@ class RequirementCommand(IndexGroupCommand):
                         f"{target_python.py_version} not in {script_requires_python!r}"
                     )

-            for req in script_metadata.get("dependencies", []):
-                requirements.append(
-                    InstallRequirement(Requirement(req), comes_from=None)
-                )
+            requirements.extend(
+                InstallRequirement(Requirement(req), comes_from=None)
+                for req in script_metadata.get("dependencies", [])
+            )

         # If any requirement has hash options, enable hash checking.
         if any(req.has_hash_options for req in requirements):

```

Code after:
            )
            requirements.append(req_to_add)

        if options.dependency_groups:
            for req in parse_dependency_groups(options.dependency_groups):
                req_to_add = install_req_from_req_string(
                    req,
                    isolated=options.isolated_mode,
                    user_supplied=True,
                )
                requirements.append(req_to_add)

        for req in options.editables:
            req_to_add = install_req_from_editable(
                req,
                user_supplied=True,
                isolated=options.isolated_mode,
                config_settings=getattr(options, "config_settings", None),
            )
            requirements.append(req_to_add)

        # NOTE: options.require_hashes may be set if --require-hashes is True
        for filename in options.requirements:
            for parsed_req in parse_requirements(
                filename, finder=finder, options=options, session=session
            ):
                req_to_add = install_req_from_parsed_requirement(
                    parsed_req,
                    isolated=options.isolated_mode,
                    user_supplied=True,
                    config_settings=(
                        parsed_req.options.get("config_settings")
                        if parsed_req.options
                        else None
                    ),
                )
                requirements.append(req_to_add)

        if options.requirements_from_scripts:
            if len(options.requirements_from_scripts) > 1:
                raise CommandError("--requirements-from-script can only be given once")

            script = options.requirements_from_scripts[0]
            try:
                script_metadata = pep723_metadata(script)
            except PEP723Exception as exc:
                raise CommandError(exc.msg)

            script_requires_python = script_metadata.get("requires-python", "")

            if script_requires_python and not options.ignore_requires_python: