From 30aef341cc5ffea84e3484b2385056d8842648f2 Mon Sep 17 00:00:00 2001 From: Leonardo Rochael Almida Date: Mon, 24 Jun 2024 18:55:19 +0200 Subject: [PATCH] Implement `--no-deps` in requirements files --- news/9948.feature.rst | 3 +++ src/pip/_internal/req/req_file.py | 2 ++ tests/functional/test_new_resolver.py | 37 +++++++++++++++++++++++++++ tests/unit/test_req_file.py | 19 ++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/news/9948.feature.rst b/news/9948.feature.rst index 24e27dc09e8..c2f93dadb02 100644 --- a/news/9948.feature.rst +++ b/news/9948.feature.rst @@ -2,3 +2,6 @@ Implement the ``--no-deps-for`` switch for the ``install`` command and the requirements file. This switch will prevent the installation of dependencies of specific packages, as opposed to the ``--no-deps`` switch which prevents the installation dependencies of all requested packages. + +Also implement recognition of the global ``--no-deps`` switch in the requirements +file. diff --git a/src/pip/_internal/req/req_file.py b/src/pip/_internal/req/req_file.py index acfd6d2f360..366acc327e8 100644 --- a/src/pip/_internal/req/req_file.py +++ b/src/pip/_internal/req/req_file.py @@ -56,6 +56,7 @@ cmdoptions.find_links, cmdoptions.no_binary, cmdoptions.only_binary, + cmdoptions.no_deps, cmdoptions.no_deps_for, cmdoptions.prefer_binary, cmdoptions.require_hashes, @@ -226,6 +227,7 @@ def handle_option_line( options.features_enabled.extend( f for f in opts.features_enabled if f not in options.features_enabled ) + options.ignore_dependencies |= opts.ignore_dependencies options.ignore_dependencies_for.update(opts.ignore_dependencies_for) # set finder options diff --git a/tests/functional/test_new_resolver.py b/tests/functional/test_new_resolver.py index f15cbce2656..715c72a71bc 100644 --- a/tests/functional/test_new_resolver.py +++ b/tests/functional/test_new_resolver.py @@ -255,6 +255,43 @@ def test_new_resolver_ignore_dependencies_for(script: PipTestEnvironment) -> Non script.assert_not_installed("dep") +def test_new_resolver_no_deps_in_requirements( + tmpdir: pathlib.Path, + script: PipTestEnvironment, +) -> None: + create_basic_wheel_for_package( + script, + "base", + "0.1.0", + depends=["dep"], + ) + create_basic_wheel_for_package( + script, + "dep", + "0.1.0", + ) + + req_file = tmpdir / "requirements.txt" + req_file.write_text( + """ + base==0.1.0 + --no-deps + """ + ) + + script.pip( + "install", + "--no-cache-dir", + "--no-index", + "--find-links", + script.scratch_path, + "-r", + req_file, + ) + script.assert_installed(base="0.1.0") + script.assert_not_installed("dep") + + @pytest.mark.parametrize( "root_dep", [ diff --git a/tests/unit/test_req_file.py b/tests/unit/test_req_file.py index dd83bdf2378..564487eecf2 100644 --- a/tests/unit/test_req_file.py +++ b/tests/unit/test_req_file.py @@ -47,6 +47,7 @@ def options(session: PipSession) -> mock.Mock: index_url="default_url", format_control=FormatControl(set(), set()), features_enabled=[], + ignore_dependencies=False, ignore_dependencies_for=set(), ) @@ -764,6 +765,24 @@ def test_join_lines(self, tmpdir: Path, finder: PackageFinder) -> None: assert finder.index_urls == ["url1", "url2"] + def test_ignore_dependencies(self, tmpdir: Path, options: mock.Mock) -> None: + req = tmpdir / "req1.txt" + req.write_text( + """ + --no-deps + """ + ) + + list( + parse_reqfile( + req, + session=PipSession(), + options=options, + ) + ) + + assert options.ignore_dependencies + def test_ignore_dependencies_for(self, tmpdir: Path, options: mock.Mock) -> None: req = tmpdir / "req1.txt" req.write_text(