From 3b9c79f7ebd39e2fdf95908b9c1f79f2b6bac718 Mon Sep 17 00:00:00 2001 From: Anandraj-prog Date: Sat, 11 Oct 2025 10:59:40 +0530 Subject: [PATCH] feat: Add custom pretty printing for pathlib.Path objects --- rich/pretty.py | 7 +++++++ test_my_fix.py | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test_my_fix.py diff --git a/rich/pretty.py b/rich/pretty.py index 5c725c0c5..926e02415 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -1,5 +1,6 @@ import builtins import collections +import pathlib import dataclasses import inspect import os @@ -816,6 +817,12 @@ def iter_attrs() -> ( child_node.key_separator = "=" append(child_node) pop_visited(obj_id) + + # START of new code block + elif isinstance(obj, pathlib.Path): + node = Node(value_repr=repr(str(obj))) + # END of new code block + elif _safe_isinstance(obj, _CONTAINERS): for container_type in _CONTAINERS: if _safe_isinstance(obj, container_type): diff --git a/test_my_fix.py b/test_my_fix.py new file mode 100644 index 000000000..76e5d3ddc --- /dev/null +++ b/test_my_fix.py @@ -0,0 +1,11 @@ +# test_my_fix.py +import pathlib +from rich.pretty import pprint + +# Create a Path object to test +my_file_path = pathlib.Path("./my_folder/my_script.py") + +# This will use the code you just modified! +print("--- Running test with the fix ---") +pprint(my_file_path) +print("-------------------------------") \ No newline at end of file