{"id":2353,"library":"wadler-lindig","title":"Wadler-Lindig Pretty Printer","description":"Wadler-Lindig is a Python library that provides a pretty-printer based on the Wadler–Lindig algorithm. It serves as an alternative to the built-in `pprint.pprint`, aiming to produce output that consumes less horizontal space, which is particularly useful for complex custom types, error messages, or nested data structures. The library is characterized by its compact implementation, support for multi-line strings and ANSI escape codes, and zero runtime dependencies. The current version is 0.1.7, and it appears to be actively maintained.","status":"active","version":"0.1.7","language":"en","source_language":"en","source_url":"https://github.com/patrick-kidger/wadler_lindig","tags":["pretty-printing","formatting","debug","repr","wadler-lindig","devtools"],"install":[{"cmd":"pip install wadler-lindig","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used in quickstart example to demonstrate concise array formatting.","package":"numpy","optional":true}],"imports":[{"note":"It is recommended to import `wadler_lindig` with an alias (e.g., `wl`) to avoid naming conflicts with Python's standard `pprint` module and to clearly distinguish its functions.","wrong":"from wadler_lindig import pprint # Directly importing 'pprint' is discouraged as it may clash with the built-in module.","symbol":"pprint","correct":"import wadler_lindig as wl\nwl.pprint(obj)"},{"note":"Used to get a pretty-formatted string instead of printing directly to stdout.","symbol":"pformat","correct":"import wadler_lindig as wl\nformatted_string = wl.pformat(obj)"}],"quickstart":{"code":"import dataclasses\nimport numpy as np\nimport wadler_lindig as wl\n\n@dataclasses.dataclass\nclass MyDataclass:\n    x: list[str]\n    y: np.ndarray\n\nobj = MyDataclass(\n    x=[\"lorem\", \"ipsum\", \"dolor sit amet\", \"consectetur\", \"adipiscing elit\"],\n    y=np.zeros((2, 3))\n)\n\nprint(\"\\n--- Default pprint output ---\")\nprint(obj)\n\nprint(\"\\n--- wadler_lindig.pprint output (width=30, indent=4) ---\")\nwl.pprint(obj, width=30, indent=4)\n\nprint(\"\\n--- wadler_lindig.pformat output (width=20) ---\")\nformatted_str = wl.pformat(obj, width=20, indent=2)\nprint(formatted_str)\n\n# Example of custom pretty-printing for a non-dataclass type\nclass MyCustomClass:\n    def __init__(self, value):\n        self.value = value\n    \n    def __pdoc__(self, **kwargs) -> wl.AbstractDoc:\n        return wl.TextDoc(f\"<MyCustomClass: {self.value}> \")\n\n    def __repr__(self):\n        return wl.pformat(self, width=80)\n\ncustom_obj = MyCustomClass(123)\nprint(\"\\n--- Custom object with __pdoc__ ---\")\nwl.pprint(custom_obj)\n","lang":"python","description":"This quickstart demonstrates basic usage of `wadler_lindig.pprint` and `pformat` with a dataclass containing a list and a NumPy array. It also illustrates how to customize the pretty-printing for a custom class using the `__pdoc__` method. Note that `numpy` is used for demonstration purposes, but is not a core dependency of the `wadler-lindig` library itself."},"warnings":[{"fix":"Design custom `__pdoc__` methods or preprocess long strings if strict width adherence is critical for specific parts of the output.","message":"The `width` argument in `pprint` and `pformat` is a 'best-effort' maximum. It may be exceeded if there are unbroken pieces of text (e.g., long strings) that are inherently wider than the specified width.","severity":"gotcha","affected_versions":"All"},{"fix":"For your types, implement `def __pdoc__(self, **kwargs) -> wl.AbstractDoc: ...`. For external types, pass `custom=your_custom_function` to the pretty-printing calls.","message":"Customizing pretty-printing for your own types typically uses the `__pdoc__` method, which is specific to this library. For third-party types you don't own, use the `custom` argument in `wl.pprint` or `wl.pformat` instead of `__pdoc__`.","severity":"gotcha","affected_versions":"All"},{"fix":"Understand its purpose as an object introspection and display tool. For source code formatting, use dedicated tools like Black or Ruff.","message":"This library is designed for pretty-formatting Python *objects* at runtime, similar to the built-in `pprint` module. It is *not* a source code formatter like Black or Rustfmt, and should not be used for reformatting Python source files.","severity":"gotcha","affected_versions":"All"},{"fix":"If expecting exact `pprint` behavior, be aware of these differences. For NumPy array verbosity, set `short_arrays=False` in `pprint`/`pformat` calls. Adapt custom pretty-printing logic if migrating from `pprint`.","message":"Output format differs from Python's built-in `pprint.pprint`. `wadler-lindig` prioritizes reducing horizontal space and often indents more aggressively. It also provides a concise summary for NumPy arrays by default (e.g., `f64[2,3](numpy)`).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}