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.2.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.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prettyerr-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 fc29ecce7ad72e2bad735302ed9f13ad37048e4b4261dd3f23c780b77ae50025
MD5 eca31b680a9618f905ff69af23dfa7f2
BLAKE2b-256 d9d82af1f658c25295095863d0d8c46d8b620edfa960a9c893146e8e22d6284a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prettyerr-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b75f15bbe21a8587992814e3e2024621427a51f0b9cc2442afa4eb0db9f266f
MD5 fe23869d744a453f35ff262e7e7c4ec2
BLAKE2b-256 ee059c258d405ce1b133f94c5528e17c1d0ccef79ad2c0b5765eb5d0fdb9cede

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