Repository: pypa/pip
Pull request: Use rich.traceback with debug mode
<!---
Thank you for your soon to be pull request. Before you submit this, please
double check to make sure that you've added a news file fragment. In pip we
generate our NEWS.rst from multiple news fragment files, and all pull requests
require either a news file fragment or a marker to indicate they don't require
one.

To read more about adding a news file fragment for your PR, please check out
our documentation at: https://pip.pypa.io/en/latest/development/contributing/#news-entries
-->
Fixes #10791 

Problem statement:
Print the exception via rich.traceback, when running with `--debug`
This would mean that exporting `PIP_DEBUG=1` and running tests with this would print a detailed traceback with information about locals! It would make debugging easier, especially in tests when there's a weird failure.

Plus, I'm pretty sure anyone passing `--debug` would likely appreciate this anyway. :)

Hints from issue discussion:

Commit message: Fix lint

Patch to review:
diff --git a/news/10791.feature.rst b/news/10791.feature.rst
new file mode 100644
index 00000000000..be41c6b7e21
--- /dev/null
+++ b/news/10791.feature.rst
@@ -0,0 +1 @@
+Print the exception via ``rich.traceback``, when running with ``--debug``.
diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py
index 81c443adf49..22217ff3371 100644
--- a/src/pip/_internal/cli/base_command.py
+++ b/src/pip/_internal/cli/base_command.py
@@ -10,6 +10,8 @@
 from optparse import Values
 from typing import Any, Callable, List, Optional, Tuple
 
+from pip._vendor.rich.traceback import install
+
 from pip._internal.cli import cmdoptions
 from pip._internal.cli.command_context import CommandContextMixIn
 from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
@@ -215,6 +217,7 @@ def exc_logging_wrapper(*args: Any) -> int:
                 run = intercepts_unhandled_exc(self.run)
             else:
                 run = self.run
+                install(show_locals=True)
             return run(options, args)
         finally:
             self.handle_pip_version_check(options)


Code review comments:
diff_hunk: @@ -10,6 +10,8 @@
 from optparse import Values
 from typing import Any, Callable, List, Optional, Tuple
 
+from pip._vendor.rich.traceback import install
path: src/pip/_internal/cli/base_command.py
text: @author:
Should it be like this?
```python
from pip._vendor.rich import traceback as rich_traceback
```

@user1:
Yea, that would be nicer!