Repository: pypa/pip

Issue:
pip show ignores environment markers
### Description

Perhaps the best explanation is to offer an example:

```
$ pip show opencv-python | grep Requires
Requires: numpy, numpy, numpy, numpy, numpy, numpy
```

Wow, it really wants numpy!  :) 

The reason is that environment markers are there:
https://github.com/opencv/opencv-python/blob/43cd7169cb92d96898d59753796678495b95c833/setup.py#L26-L36


### Expected behavior

I'm not sure whether they should be deduplicated by pip: 

```
Requires: numpy
```

_Evaluated_ and merged by pip (in my case for Python-3.11 on macOS):

```
Requires: numpy >=1.23.5
```

Or just rendered without evaluation. It's pretty crazy for this distribution (opencv-python==4.8.0.74) but I guess that would be something like:

```
Requires: numpy (>=1.13.3) ; python_version < "3.7", numpy (>=1.21.0) ; python_version <= "3.9" and platform_system == "Darwin" and platform_machine == "arm64", numpy (>=1.21.2) ; python_version >= "3.10", numpy (>=1.21.4) ; python_version >= "3.10" and platform_system == "Darwin", numpy (>=1.23.5) ; python_version >= "3.11", numpy (>=1.19.3) ; python_version >= "3.6" and platform_system == "Linux" and platform_machine == "aarch64", numpy (>=1.17.0) ; python_version >= "3.7", numpy (>=1.17.3) ; python_version >= "3.8", numpy (>=1.19.3) ; python_version >= "3.9"
```

The latter is probably my preference, even though this particular package makes it look pathological. The current behavior of stripping the markers entirely in pip show's output seems worse than any of these options.


### pip version

23.2

### Python version

3.11

### OS

macOS

### How to Reproduce

```
pip install opencv-python
pip show opencv-python
```

### Output

_No response_

### Code of Conduct

- [X] I agree to follow the [PSF Code of Conduct](https://www.python.org/psf/conduct/).


Discussion:
I have a similar issue when adding environment markers
Pull requests are always welcomed.
@uranusjr  Well, which of the outputs mentioned in "Expected behavior" would you like to see in a PR?
`pip show` is never strictly accurate and is bounded by the current platform so I’d say `numpy` is enough.
@wimglenn Do you want to tackle the issue? Otherwise I'd give it a try.
@chrysle  go ahead

Resolving patch:
diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py
--- a/src/pip/_internal/commands/show.py
+++ b/src/pip/_internal/commands/show.py
@@ -100,7 +100,11 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
         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:


Test patch:
diff --git a/tests/functional/test_show.py b/tests/functional/test_show.py
--- 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