diff --git a/news/12165.bugfix.rst b/news/12165.bugfix.rst new file mode 100644 index 000000000..dd09baf60 --- /dev/null +++ b/news/12165.bugfix.rst @@ -0,0 +1 @@ +This change will deduplicate entries in the ``Requires`` field of ``pip show``. diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index 3f10701f6..cfd67420d 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.py @@ -100,7 +100,11 @@ def search_packages_info(query: List[str]) -> Generator[_PackageInfo, None, None except KeyError: continue - requires = sorted((req.name for req in dist.iter_dependencies()), key=str.lower) + requires = sorted( + # Avoid duplicates in requirements (e.g. due to environment markers). + {req.name for req in dist.iter_dependencies()}, + key=str.lower, + ) required_by = sorted(_get_requiring_packages(dist), key=str.lower) try: diff --git a/tests/functional/test_show.py b/tests/functional/test_show.py index b8ec0510a..1f2e2f7ae 100644 --- a/tests/functional/test_show.py +++ b/tests/functional/test_show.py @@ -350,3 +350,29 @@ def test_show_include_work_dir_pkg(script: PipTestEnvironment) -> None: result = script.pip("show", "simple", cwd=pkg_path) lines = result.stdout.splitlines() assert "Name: simple" in lines + + +def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None: + """ + Test that show should deduplicate requirements + for a package + """ + + # Create a test package and create .egg-info dir + pkg_path = create_test_package_with_setup( + script, + name="simple", + version="1.0", + install_requires=[ + "pip >= 19.0.1", + 'pip >= 19.3.1; python_version < "3.8"', + 'pip >= 23.0.1; python_version < "3.9"', + ], + ) + script.run("python", "setup.py", "egg_info", expect_stderr=True, cwd=pkg_path) + + script.environ.update({"PYTHONPATH": pkg_path}) + + result = script.pip("show", "simple", cwd=pkg_path) + lines = result.stdout.splitlines() + assert "Requires: pip" in lines