liblaf.pretty
¶
Public API for liblaf.pretty.
Import pformat when you want a width-aware Rich
renderable, pprint or pp when
you want to print immediately, and the registration helpers when you need to
teach the formatter about custom types.
Modules:
-
common–Shared sentinels and identity helpers used by the formatting pipeline.
-
custom–Customization helpers for teaching
liblaf.prettynew formatting rules. -
literals–Styled Rich text fragments reused throughout the formatter.
-
stages–Internal pipeline stages that turn wrapped objects into Rich renderables.
Classes:
-
PrettyConfig–Environment-backed defaults for the pretty printer.
-
PrettyContext–Helper object passed to custom formatters and pipeline stages.
-
PrettyHandler–Protocol implemented by pretty-printer callbacks.
-
PrettyOptions–Resolved options for a single formatting pass.
-
PrettyOverrides–Keyword overrides accepted by the public formatting functions.
Functions:
-
pformat–Build a width-aware Rich renderable for
obj. -
pprint–Format
objand print it through a Rich console.
Attributes:
-
config(PrettyConfig) – -
pp– -
register_func– -
register_lazy– -
register_type–
PrettyConfig
¶
Bases: BaseConfig
flowchart TD
liblaf.pretty.PrettyConfig[PrettyConfig]
liblaf.conf._config.BaseConfig[BaseConfig]
liblaf.conf._config.BaseConfig --> liblaf.pretty.PrettyConfig
click liblaf.pretty.PrettyConfig href "" "liblaf.pretty.PrettyConfig"
click liblaf.conf._config.BaseConfig href "" "liblaf.conf._config.BaseConfig"
Environment-backed defaults for the pretty printer.
Values are loaded from PRETTY_* variables and can be overridden per call with
pformat or pprint.
PRETTY_INDENT accepts the same markup and ANSI inputs as the per-call
indent override.
Methods:
-
dump–Materialize the current configuration as
PrettyOptions. -
load_env–Refresh every field from its configured environment variable.
-
override–Temporarily override one or more values within a context manager.
-
set–Update fields or groups from a mapping and keyword arguments.
-
to_dict–Serialize the current config tree to nested dictionaries.
-
to_namespace–Serialize the current config tree to nested namespaces.
Attributes:
-
env_prefix(str) – -
hide_defaults(Field[bool]) – -
indent(Field[Text]) – -
max_array(Field[int]) – -
max_dict(Field[int]) – -
max_level(Field[int]) – -
max_list(Field[int]) – -
max_long(Field[int]) – -
max_other(Field[int]) – -
max_string(Field[int]) – -
name(str) –
hide_defaults
class-attribute
instance-attribute
¶
hide_defaults: Field[bool] = field_bool(default=True)
dump
¶
dump() -> PrettyOptions
Materialize the current configuration as PrettyOptions.
The returned object is a snapshot of the current environment-backed
values, with indent normalized to [Text][rich.text.Text].
Source code in src/liblaf/pretty/_config.py
load_env
¶
Refresh every field from its configured environment variable.
Source code in .venv/lib/python3.14/site-packages/liblaf/conf/_config.py
override
¶
Temporarily override one or more values within a context manager.
Parameters:
-
changes(Mapping[str, Any] | None, default:None) –Optional mapping of names to temporary values.
-
**kwargs(Any, default:{}) –Additional name-to-value overrides.
Yields:
-
Generator[None]–Nonewhile the overrides are active.
Source code in .venv/lib/python3.14/site-packages/liblaf/conf/_config.py
set
¶
Update fields or groups from a mapping and keyword arguments.
Nested groups accept mapping values and forward them to the nested
config's own set() method.
Source code in .venv/lib/python3.14/site-packages/liblaf/conf/_config.py
to_dict
¶
Serialize the current config tree to nested dictionaries.
Source code in .venv/lib/python3.14/site-packages/liblaf/conf/_config.py
to_namespace
¶
to_namespace() -> SimpleNamespace
Serialize the current config tree to nested namespaces.
Source code in .venv/lib/python3.14/site-packages/liblaf/conf/_config.py
PrettyContext
¶
Bases: TraceContext
flowchart TD
liblaf.pretty.PrettyContext[PrettyContext]
liblaf.pretty.stages.wrapped._context.TraceContext[TraceContext]
liblaf.pretty.stages.wrapped._context.TraceContext --> liblaf.pretty.PrettyContext
click liblaf.pretty.PrettyContext href "" "liblaf.pretty.PrettyContext"
click liblaf.pretty.stages.wrapped._context.TraceContext href "" "liblaf.pretty.stages.wrapped._context.TraceContext"
Helper object passed to custom formatters and pipeline stages.
Custom handlers usually build output with container,
name_value, and
positional.
Attributes:
Parameters:
-
depth(int, default:0) – -
options(PrettyOptions, default:PrettyOptions(max_level=6, max_list=6, max_array=5, max_dict=4, max_string=30, max_long=40, max_other=30, indent=<text '| ' [] 'repr.indent'>, hide_defaults=True)) –Materialize the current configuration as
PrettyOptions.The returned object is a snapshot of the current environment-backed values, with
indentnormalized to [Text][rich.text.Text]. -
trace_cache(dict[int, TracedObject], default:<class 'dict'>) –dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
-
types(set[type], default:<dynamic>) –Build an unordered collection of unique elements.
-
registry(PrettyRegistry, default:PrettyRegistry(handlers=[<function _describe_fieldz at 0x7fa40748a350>, <function _describe_rich_repr at 0x7fa40748a4b0>], lazy_handlers={}, type_dispatcher=<function _default_type_dispatcher.<locals>.type_dispatcher at 0x7fa407473480>)) – -
wrap_cache(dict[int, WrappedNode], default:<class 'dict'>) –dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Methods:
-
add_separators–Add repr-style spacing and trailing commas to a sequence of items.
-
container–Build a repr-like container node.
-
ellipsis_item–Return the positional ellipsis item used for truncated containers.
-
finish–Build the lowering context with disambiguated type names.
-
identifier–Return the identity record for
obj. -
key_value–Build a
key: valueitem or an ellipsis placeholder when truncated. -
leaf–Build a scalar leaf node from Rich text.
-
name_value–Build a repr-style
name=valueitem. -
positional–Build a positional item or an ellipsis placeholder when truncated.
-
possibly_sorted–Sort items when possible, otherwise preserve their original order.
-
trace–Trace wrapped nodes and attach reference information.
-
truncate_dict–Yield mapping items up to
max_dict, then a truncation marker. -
truncate_list–Yield list-like items up to
max_list, then a truncation marker. -
wrap_eager–Wrap
objimmediately and memoize the result by object identity. -
wrap_lazy–Create a lazily wrapped placeholder for
obj.
arepr
cached
property
¶
arepr: Repr
Return the configured reprlib.Repr instance used for fallback formatting.
The limits mirror the active PrettyOptions.
trace_cache
class-attribute
instance-attribute
¶
trace_cache: dict[int, TracedObject] = field(factory=dict)
wrap_cache
class-attribute
instance-attribute
¶
wrap_cache: dict[int, WrappedNode] = field(factory=dict)
add_separators
staticmethod
¶
add_separators(
items: Iterable[WrappedItem],
) -> list[WrappedItem]
Add repr-style spacing and trailing commas to a sequence of items.
Source code in src/liblaf/pretty/custom/_context.py
container
¶
container(
obj: Any,
begin: Text,
children: Iterable[WrappedItem],
end: Text,
indent: Text | None = None,
*,
add_separators: bool = True,
empty: Text | None = None,
referencable: bool = True,
) -> WrappedContainer
Build a repr-like container node.
Parameters:
-
obj(Any) –Original object represented by the container.
-
begin(Text) –Opening punctuation, usually styled Rich text such as
(or[. -
children(Iterable[WrappedItem]) –Wrapped child items to render inside the container.
-
end(Text) –Closing punctuation.
-
indent(Text | None, default:None) –Indentation used for wrapped layouts. Defaults to the active formatter option.
-
add_separators(bool, default:True) –When
True, add repr-style spaces and commas between children before building the container. -
empty(Text | None, default:None) –Alternate rendering for empty containers such as
set(). -
referencable(bool, default:True) –Whether repeated appearances of
objshould be rendered as shared references later in the pipeline.
Returns:
-
WrappedContainer–A wrapped container ready for tracing and lowering.
Note
Containers are referencable by default. Repeated appearances of the same container can therefore render as shared-reference tags later in the pipeline.
Source code in src/liblaf/pretty/custom/_context.py
ellipsis_item
¶
ellipsis_item() -> WrappedItem
finish
¶
finish() -> LowerContext
identifier
¶
identifier(obj: Any) -> ObjectIdentifier
key_value
¶
key_value(key: Any, value: Any) -> WrappedItem
Build a key: value item or an ellipsis placeholder when truncated.
Source code in src/liblaf/pretty/custom/_context.py
leaf
¶
leaf(
obj: Any, text: Text, *, referencable: bool = True
) -> WrappedLeaf
Build a scalar leaf node from Rich text.
Set referencable=False for scalar summaries that should always render
inline instead of turning into shared-reference tags.
Source code in src/liblaf/pretty/custom/_context.py
name_value
¶
name_value(
name: str | Text | None, value: Any
) -> WrappedItem
Build a repr-style name=value item.
Falsey names fall back to positional.
Source code in src/liblaf/pretty/custom/_context.py
positional
¶
positional(value: Any) -> WrappedItem
Build a positional item or an ellipsis placeholder when truncated.
Source code in src/liblaf/pretty/custom/_context.py
possibly_sorted
staticmethod
¶
possibly_sorted[T](
items: Iterable[T],
*,
key: Callable[[T], Any] | None = None,
reverse: bool = False,
) -> Iterable[T]
Sort items when possible, otherwise preserve their original order.
This keeps set- and dict-like output stable when the items are orderable, while still handling unorderable objects gracefully.
Source code in src/liblaf/pretty/custom/_context.py
trace
¶
trace(root: WrappedNode) -> TracedNode
Trace wrapped nodes and attach reference information.
Source code in src/liblaf/pretty/custom/_context.py
truncate_dict
¶
Yield mapping items up to max_dict, then a truncation marker.
Source code in src/liblaf/pretty/custom/_context.py
truncate_list
¶
Yield list-like items up to max_list, then a truncation marker.
Source code in src/liblaf/pretty/custom/_context.py
wrap_eager
¶
wrap_eager(obj: Any) -> WrappedNode
Wrap obj immediately and memoize the result by object identity.
This preserves shared references for referencable objects and avoids rebuilding the same wrapped node repeatedly during one pass.
Source code in src/liblaf/pretty/custom/_context.py
wrap_lazy
¶
wrap_lazy(obj: Any) -> WrappedLazy
Create a lazily wrapped placeholder for obj.
Lazy children delay the actual wrapping work until the trace step asks for them.
Source code in src/liblaf/pretty/custom/_context.py
PrettyHandler
¶
Bases: Protocol
flowchart TD
liblaf.pretty.PrettyHandler[PrettyHandler]
click liblaf.pretty.PrettyHandler href "" "liblaf.pretty.PrettyHandler"
Protocol implemented by pretty-printer callbacks.
Handlers receive the object plus the active
PrettyContext and either return a
wrapped node or None to decline.
Methods:
-
__call__–
__call__
¶
__call__(obj: T, ctx: PrettyContext) -> WrappedNode | None
Source code in src/liblaf/pretty/custom/_registry.py
PrettyOptions
¶
Resolved options for a single formatting pass.
These values are usually created from config plus
per-call PrettyOverrides.
Attributes:
Parameters:
-
max_level(int) – -
max_list(int) – -
max_array(int) – -
max_dict(int) – -
max_string(int) – -
max_long(int) – -
max_other(int) – -
indent(Text) – -
hide_defaults(bool) –
PrettyOverrides
typed-dict
¶
PrettyOverrides(
*,
max_level: int = ...,
max_list: int = ...,
max_array: int = ...,
max_dict: int = ...,
max_string: int = ...,
max_long: int = ...,
max_other: int = ...,
indent: str | Text = ...,
hide_defaults: bool = ...,
)
Bases: TypedDict
flowchart TD
liblaf.pretty.PrettyOverrides[PrettyOverrides]
click liblaf.pretty.PrettyOverrides href "" "liblaf.pretty.PrettyOverrides"
Keyword overrides accepted by the public formatting functions.
Attributes:
Parameters:
-
max_level(int) – -
max_list(int) – -
max_array(int) – -
max_dict(int) – -
max_string(int) – -
max_long(int) – -
max_other(int) – -
indent(str | Text) – -
hide_defaults(bool) –
Parameters:
-
max_level(int, default:...) – -
max_list(int, default:...) – -
max_array(int, default:...) – -
max_dict(int, default:...) – -
max_string(int, default:...) – -
max_long(int, default:...) – -
max_other(int, default:...) – -
indent(str | Text, default:...) – -
hide_defaults(bool, default:...) –
pformat
¶
pformat(
obj: Any,
*,
max_level: int = ...,
max_list: int = ...,
max_array: int = ...,
max_dict: int = ...,
max_string: int = ...,
max_long: int = ...,
max_other: int = ...,
indent: str | Text = ...,
hide_defaults: bool = ...,
) -> LoweredNode
Build a width-aware Rich renderable for obj.
pformat() resolves the active configuration, wraps obj, traces shared
and cyclic references, and lowers the result into a Rich renderable. The
returned object is rendered later by Rich, so the final line breaks still
depend on the target console width.
Parameters:
-
obj(Any) –Object to format.
-
max_level(int, default:...) – -
max_list(int, default:...) – -
max_array(int, default:...) – -
max_dict(int, default:...) – -
max_string(int, default:...) – -
max_long(int, default:...) – -
max_other(int, default:...) – -
indent(str | Text, default:...) – -
hide_defaults(bool, default:...) –
Returns:
-
LoweredNode–A lowered renderable that can be passed to
console.print(...)or converted -
LoweredNode–to deterministic plain text with
to_plain(console=...).
Source code in src/liblaf/pretty/_api.py
pprint
¶
pprint(
obj: Any,
*,
console: Console | None = None,
max_level: int = ...,
max_list: int = ...,
max_array: int = ...,
max_dict: int = ...,
max_string: int = ...,
max_long: int = ...,
max_other: int = ...,
indent: str | Text = ...,
hide_defaults: bool = ...,
) -> None
Format obj and print it through a Rich console.
This is the side-effecting companion to pformat.
Parameters:
-
obj(Any) –Object to format.
-
console(Console | None, default:None) –Console to render into. When omitted, the active global Rich console is used.
-
max_level(int, default:...) – -
max_list(int, default:...) – -
max_array(int, default:...) – -
max_dict(int, default:...) – -
max_string(int, default:...) – -
max_long(int, default:...) – -
max_other(int, default:...) – -
indent(str | Text, default:...) – -
hide_defaults(bool, default:...) –