diff --git a/CHANGELOG.md b/CHANGELOG.md index e53205666f..cdb89c6fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Expose locals_max_depth and locals_overflow in traceback.install + ## [14.2.0] - 2025-10-09 ### Changed diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4b04786b9c..0d022b7cfb 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -32,6 +32,7 @@ The following people have contributed to the development of Rich: - [Logan Hunt](https://github.com/dosisod) - [JP Hutchins](https://github.com/JPhutchins) - [Ionite](https://github.com/ionite34) +- [Daniel Jäkel](https://github.com/kigstn) - [Josh Karpel](https://github.com/JoshKarpel) - [Jan Katins](https://github.com/jankatins) - [Hugo van Kemenade](https://github.com/hugovk) diff --git a/rich/scope.py b/rich/scope.py index 36c06241f4..41d0299d54 100644 --- a/rich/scope.py +++ b/rich/scope.py @@ -8,7 +8,7 @@ from .text import Text, TextType if TYPE_CHECKING: - from .console import ConsoleRenderable + from .console import ConsoleRenderable, OverflowMethod def render_scope( @@ -19,6 +19,8 @@ def render_scope( indent_guides: bool = False, max_length: Optional[int] = None, max_string: Optional[int] = None, + max_depth: Optional[int] = None, + overflow: Optional["OverflowMethod"] = None, ) -> "ConsoleRenderable": """Render python variables in a given scope. @@ -30,6 +32,8 @@ def render_scope( max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None. max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None. + max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. + overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None. Returns: ConsoleRenderable: A renderable object. @@ -57,6 +61,8 @@ def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]: indent_guides=indent_guides, max_length=max_length, max_string=max_string, + max_depth=max_depth, + overflow=overflow, ), ) return Panel.fit( diff --git a/rich/traceback.py b/rich/traceback.py index c32baaca12..66eaecaae9 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -33,6 +33,7 @@ Console, ConsoleOptions, ConsoleRenderable, + OverflowMethod, Group, RenderResult, group, @@ -91,8 +92,10 @@ def install( show_locals: bool = False, locals_max_length: int = LOCALS_MAX_LENGTH, locals_max_string: int = LOCALS_MAX_STRING, + locals_max_depth: Optional[int] = None, locals_hide_dunder: bool = True, locals_hide_sunder: Optional[bool] = None, + locals_overflow: Optional[OverflowMethod] = None, indent_guides: bool = True, suppress: Iterable[Union[str, ModuleType]] = (), max_frames: int = 100, @@ -114,8 +117,10 @@ def install( locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. + locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True. locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False. + locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None. indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True. suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. @@ -148,8 +153,10 @@ def excepthook( show_locals=show_locals, locals_max_length=locals_max_length, locals_max_string=locals_max_string, + locals_max_depth=locals_max_depth, locals_hide_dunder=locals_hide_dunder, locals_hide_sunder=bool(locals_hide_sunder), + locals_overflow=locals_overflow, indent_guides=indent_guides, suppress=suppress, max_frames=max_frames, @@ -268,8 +275,10 @@ class Traceback: locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. + locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True. locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False. + locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None. suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100. @@ -295,8 +304,10 @@ def __init__( show_locals: bool = False, locals_max_length: int = LOCALS_MAX_LENGTH, locals_max_string: int = LOCALS_MAX_STRING, + locals_max_depth: Optional[int] = None, locals_hide_dunder: bool = True, locals_hide_sunder: bool = False, + locals_overlow: Optional[OverflowMethod] = None, indent_guides: bool = True, suppress: Iterable[Union[str, ModuleType]] = (), max_frames: int = 100, @@ -320,8 +331,10 @@ def __init__( self.indent_guides = indent_guides self.locals_max_length = locals_max_length self.locals_max_string = locals_max_string + self.locals_max_depth = locals_max_depth self.locals_hide_dunder = locals_hide_dunder self.locals_hide_sunder = locals_hide_sunder + self.locals_overflow = locals_overlow self.suppress: Sequence[str] = [] for suppress_entity in suppress: @@ -351,8 +364,10 @@ def from_exception( show_locals: bool = False, locals_max_length: int = LOCALS_MAX_LENGTH, locals_max_string: int = LOCALS_MAX_STRING, + locals_max_depth: Optional[int] = None, locals_hide_dunder: bool = True, locals_hide_sunder: bool = False, + locals_overflow: Optional[OverflowMethod] = None, indent_guides: bool = True, suppress: Iterable[Union[str, ModuleType]] = (), max_frames: int = 100, @@ -372,9 +387,11 @@ def from_exception( indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True. locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. + locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True. locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False. + locals_overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None. suppress (Iterable[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback. max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100. @@ -388,6 +405,7 @@ def from_exception( show_locals=show_locals, locals_max_length=locals_max_length, locals_max_string=locals_max_string, + locals_max_depth=locals_max_depth, locals_hide_dunder=locals_hide_dunder, locals_hide_sunder=locals_hide_sunder, ) @@ -403,8 +421,10 @@ def from_exception( indent_guides=indent_guides, locals_max_length=locals_max_length, locals_max_string=locals_max_string, + locals_max_depth=locals_max_depth, locals_hide_dunder=locals_hide_dunder, locals_hide_sunder=locals_hide_sunder, + locals_overlow=locals_overflow, suppress=suppress, max_frames=max_frames, ) @@ -419,6 +439,7 @@ def extract( show_locals: bool = False, locals_max_length: int = LOCALS_MAX_LENGTH, locals_max_string: int = LOCALS_MAX_STRING, + locals_max_depth: Optional[int] = None, locals_hide_dunder: bool = True, locals_hide_sunder: bool = False, _visited_exceptions: Optional[Set[BaseException]] = None, @@ -433,6 +454,7 @@ def extract( locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10. locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80. + locals_max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None. locals_hide_dunder (bool, optional): Hide locals prefixed with double underscore. Defaults to True. locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False. @@ -560,6 +582,7 @@ def get_locals( value, max_length=locals_max_length, max_string=locals_max_string, + max_depth=locals_max_depth, ) for key, value in get_locals(frame_summary.f_locals.items()) if not (inspect.isfunction(value) or inspect.isclass(value)) @@ -754,6 +777,8 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]: indent_guides=self.indent_guides, max_length=self.locals_max_length, max_string=self.locals_max_string, + max_depth=self.locals_max_depth, + overflow=self.locals_overflow, ) exclude_frames: Optional[range] = None