From 9dcd453aaefeb6bbbac0b7333c8af8c6e783668d Mon Sep 17 00:00:00 2001 From: chrysle Date: Thu, 26 Oct 2023 20:52:15 +0200 Subject: [PATCH 1/2] Remove origin ireqs for extras when writing `pip-compile` annotations Co-authored-by: Sander Van Balen <7672159+sanderr@users.noreply.github.com> --- piptools/writer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/piptools/writer.py b/piptools/writer.py index 1ae341d60..d4dde7f5b 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -295,7 +295,8 @@ def _format_requirement( if src_ireq.comes_from } - if ireq.comes_from: + # Filter out the origin install requirements for extras. See https://github.com/jazzband/pip-tools/issues/2003 + if ireq.comes_from and (isinstance(ireq.comes_from, str) or ireq.comes_from.name != ireq.name): required_by.add(_comes_from_as_string(ireq.comes_from)) required_by |= set(getattr(ireq, "_required_by", set())) From f6ff30bfa705955ecaec1f0b3496e29a78ad4083 Mon Sep 17 00:00:00 2001 From: chrysle Date: Fri, 27 Oct 2023 18:39:44 +0200 Subject: [PATCH 2/2] Add functional test --- piptools/writer.py | 7 ++++-- tests/test_cli_compile.py | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/piptools/writer.py b/piptools/writer.py index d4dde7f5b..bcb70f02f 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -295,8 +295,11 @@ def _format_requirement( if src_ireq.comes_from } - # Filter out the origin install requirements for extras. See https://github.com/jazzband/pip-tools/issues/2003 - if ireq.comes_from and (isinstance(ireq.comes_from, str) or ireq.comes_from.name != ireq.name): + # Filter out the origin install requirements for extras. + # See https://github.com/jazzband/pip-tools/issues/2003 + if ireq.comes_from and ( + isinstance(ireq.comes_from, str) or ireq.comes_from.name != ireq.name + ): required_by.add(_comes_from_as_string(ireq.comes_from)) required_by |= set(getattr(ireq, "_required_by", set())) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 0275cc0d8..61ca317cc 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -3290,3 +3290,53 @@ def test_do_not_show_warning_on_explicit_strip_extras_option( assert out.exit_code == 0 assert strip_extras_warning not in out.stderr + + +def test_origin_of_extra_requirement_not_written_to_annotations( + pip_conf, runner, make_package, make_wheel, tmp_path, tmpdir +): + req_in = tmp_path / "requirements.in" + package_with_extras = make_package( + "package_with_extras", + version="0.1", + extras_require={ + "extra1": ["small-fake-a==0.1"], + "extra2": ["small-fake-b==0.1"], + }, + ) + + dists_dir = tmpdir / "dists" + make_wheel(package_with_extras, dists_dir) + + with open(req_in, "w") as req_out: + req_out.write("package-with-extras[extra1,extra2]") + + out = runner.invoke( + cli, + [ + "--output-file", + "-", + "--quiet", + "--no-header", + "--find-links", + str(dists_dir), + "--no-emit-options", + "--no-build-isolation", + req_in.as_posix(), + ], + ) + + assert out.exit_code == 0, out + assert ( + dedent( + f"""\ + package-with-extras[extra1,extra2]==0.1 + # via -r {req_in.as_posix()} + small-fake-a==0.1 + # via package-with-extras + small-fake-b==0.1 + # via package-with-extras + """ + ) + == out.stdout + )