Skip to content

Commit 7a1480c

Browse files
committed
Deduplicate pip show output in Requires field
1 parent 4b0e7e5 commit 7a1480c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

news/12165.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This change will deduplicate entries in the ``Requires`` field of ``pip show``.

src/pip/_internal/commands/show.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
100100
except KeyError:
101101
continue
102102

103-
requires = sorted((req.name for req in dist.iter_dependencies()), key=str.lower)
103+
requires = []
104+
# Avoid duplicates in requirements due to environment markers
105+
for req in dist.iter_dependencies():
106+
if req.name not in requires:
107+
requires.append(req.name)
108+
requires = sorted(requires, key=str.lower)
104109
required_by = sorted(_get_requiring_packages(dist), key=str.lower)
105110

106111
try:

tests/functional/test_show.py

+27
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,30 @@ def test_show_include_work_dir_pkg(script: PipTestEnvironment) -> None:
350350
result = script.pip("show", "simple", cwd=pkg_path)
351351
lines = result.stdout.splitlines()
352352
assert "Name: simple" in lines
353+
354+
355+
def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None:
356+
"""
357+
Test that show should deduplicate requirements
358+
for a package
359+
"""
360+
361+
# Create a test package and create .egg-info dir
362+
pkg_path = create_test_package_with_setup(
363+
script,
364+
name="simple",
365+
version="1.0",
366+
install_requires=[
367+
'pip >= 19.0.1',
368+
'pip >= 19.3.1; python_version < "3.8"',
369+
'pip >= 23.0.1; python_version < "3.9"',
370+
],
371+
)
372+
script.run("python", "setup.py", "egg_info", expect_stderr=True, cwd=pkg_path)
373+
374+
script.environ.update({"PYTHONPATH": pkg_path})
375+
376+
result = script.pip("show", "simple", cwd=pkg_path)
377+
print(result)
378+
lines = result.stdout.splitlines()
379+
assert "Requires: pip" in lines

0 commit comments

Comments
 (0)