Skip to main content

Pretty error messages

Project description

Pretty Err

PyPI version Python versions CI

A simple library for printing modern, source-annotated error messages.

Features

  • source span annotation
  • color output
  • multiple non-overlapping spans
  • zero-width spans

Install

pip install prettyerr

Quickstart

Print out an error report for some example source code

from prettyerr import Error, Pointer, Span, SrcFile

# example source code
src = """\
const repeat = (message:string, times:int) :> string => {
    return message * times
}
result = repeat("hello", "3")
printl(result)
"""

# Locate spans to highlight (.index() here is just for simplicity)
repeat_start = src.index('repeat(')
bad_arg_start = src.index('"3"')
times_span = Span(repeat_start, repeat_start + len('repeat'))
bad_arg_span = Span(bad_arg_start, bad_arg_start + len('"3"'))

# Build and print the error report
report = Error(
    SrcFile.from_text(src, "path/to/example.lang"),
    title="type mismatch for argument `times`",
    pointer_messages=[
        Pointer(span=times_span, message="`repeat` function's second argument `times` expects an 'int'"),
        Pointer(span=bad_arg_span, message="argument given is type 'string'"),
    ],
    hint='Consider changing string literal "3" to integer 3',
)
print(report)

This generates the following error report

Error: type mismatch for argument `times`

    ╭─[path/to/example.lang:4:10]
  4 | result = repeat("hello", "3")
    ·          ──┬───          ─┬─
    ·            │              ╰─ argument given is type 'string'
    ·            ╰─ `repeat` function's second argument `times` expects an 'int'
    ╰───
  help: Consider changing string literal "3" to integer 3

API at a glance

  • SrcFile.from_text(body, path=None)
  • Span(start, stop) (stop-exclusive)
  • Pointer(span=Span(...) | list[Span], message=..., placement=None, color=None)
  • Report types: Error, Warning, Info, Hint
  • Common fields: title, message, pointer_messages, hint, use_color

Tips

  • Disable ANSI colors with use_color=False.
  • Raise an exception with Error(...).throw() (raises ReportException).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prettyerr-0.0.2rc1.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prettyerr-0.0.2rc1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file prettyerr-0.0.2rc1.tar.gz.

File metadata

  • Download URL: prettyerr-0.0.2rc1.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for prettyerr-0.0.2rc1.tar.gz
Algorithm Hash digest
SHA256 0dc91a3b9af7af4c1be4fb21f298af18f1efcff090cc725709b6e6de04e9dc95
MD5 0b88b29b99d206eb0b99f3392301d605
BLAKE2b-256 47aaac9a19b95eea270f3c68774d1db0f4907b22dd8840ac445b59819286f730

See more details on using hashes here.

File details

Details for the file prettyerr-0.0.2rc1-py3-none-any.whl.

File metadata

  • Download URL: prettyerr-0.0.2rc1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for prettyerr-0.0.2rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 0aa8946eb680d7caceb0d1493afd08480ea02fa1d3e3915b73536e0293792078
MD5 a1ca53ef5ff20fa9e33dd4b8bb8b0a44
BLAKE2b-256 6d1dafde94f7a75d3931f2f9a210d8bc77c0b69a9a25608a492410635f7a5c6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page