Pretty Err

Unknown

While working on Dewy, I wanted to include a pretty error reporting system, so I ended up building a pretty robust error reporting library reminiscent of Nushell's error reporting. It turned out quite well, so I broke it out into it's own package: prettyerr

Getting Started

Install the package from PyPI:

pip install prettyerr

Then you can point at the relevant bits of source and print a report:

from prettyerr import Error, Pointer, Span, SrcFile

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

repeat_start = src.index('repeat(')
bad_arg_start = src.index('"3"')

report = Error(
    SrcFile.from_text(src, "path/to/example.lang"),
    title="type mismatch for argument `times`",
    pointer_messages=[
        Pointer(span=Span(repeat_start, repeat_start + len('repeat')), message="`repeat` function's second argument `times` expects an 'int'"),
        Pointer(span=Span(bad_arg_start, bad_arg_start + len('"3"')), message="argument given is type 'string'"),
    ],
    hint='Consider changing string literal "3" to integer 3',
)
print(report)

Which prints something like:

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

Links