Skip to main content

parse-errors

parse-errors improves the errors you get when parsing config files (JSON, TOML, YAML). Instead of a bare exception with a vague message, you get a ParseError that includes the filename, line number, and column — so you can point users straight to the problem.

It understands msgspec validation errors and TOML syntax errors, mapping them back to the line of config they came from. Even unrelated exceptions get the filename attached.

Usage

Wrap your parse/validate call in ParseContext:

import msgspec
import tomllib
from parse_errors import ParseContext

class Config(msgspec.Struct):
    host: str
    port: int

filename = "config.toml"

with open(filename, "rb") as f:
    raw = f.read()

with ParseContext(filename, data=raw, format="toml"):
    data = tomllib.loads(raw.decode())
    config = msgspec.convert(data, Config)

ParseContext will intercept exceptions. If it can analyze them for precise locations, it will raise a ParseError exception with the original exception as the cause. If it can't find location information, the original exception is raised as-is. No exceptions are swallowed.

As a concrete example, if the msgspec.convert raises because port is a string instead of an integer, you get something like:

parse_errors.ParseError: config.toml:3:8: Expected `int`, got `str` - at `$.port`

rather than the bare msgspec.ValidationError with no location.

ParseContext also handles errors from TOML and other decoders that include positional information (at line N, column M) and re-raises them in the same filename:line:col: message format.

API

from parse_errors import ParseContext, ParseError
from parse_errors.source_map import SourceMap, build_source_map, locate_pointer

ParseContext(filename, *, data=None, format=None) — context manager.

  • filename: path to the file being parsed (used in error messages and to infer the format from the extension when format is omitted).
  • data: the file contents as str or bytes. If omitted, the file is read from disk automatically when location info is needed.
  • format: "json", "toml", or "yaml". Inferred from filename's extension when not supplied.

ParseError — the exception raised inside the context. Has attributes filename, line (1-based), and column (1-based).

locate_pointer(source, fmt, pointer) — returns the best source-map entry for one JSON Pointer without building a full map. fmt is "json", "toml", or "yaml"; source is str or UTF-8 bytes; pointer uses RFC 6901 escaping. If the exact pointer is not present, the result matches closest_entry(build_source_map(source, fmt), pointer): the nearest enclosing value when one exists, otherwise None.

SourceMap(source, fmt) — caches the parsed document for repeated targeted lookups. Use SourceMap(...).locate(pointer) when several errors in the same document need locations. It still avoids constructing a full pointer-to-entry map.

build_source_map(source, fmt) — builds the full pointer-to-location map. This is useful when callers need many arbitrary entries or need to inspect all locations.

Warning: source-map helpers locate nodes in a document; they are not validating parsers. JSON and TOML location support uses tree-sitter so it can return a location from a syntax tree even when a real decoder would reject the source. Parse or validate the document with your normal parser first, then use these helpers only to map known error paths back to source locations.

Version Compat

This library is compatible with Python 3.10+, but should be linted under the newest stable version.

Versioning

This library follows meanver which basically means semver along with a promise to rename when the major version changes.

License

parse-errors is copyright Tim Hatch, and licensed under the MIT license. See the LICENSE file for details.

Metadata

Release files for parse-errors 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parse-errors 0.7.0
File Size Uploaded
parse_errors-0.7.0.tar.gz 26.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parse-errors 0.7.0
File Interpreter ABI Platform
parse_errors-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 46.5 kB

Release files / parse_errors-0.7.0.tar.gz

Download URL parse_errors-0.7.0.tar.gz
Size 26.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3f663dfce7d1e0e2e5c58d95c0e70aee447ef03b7b32c19f85345517a406aa67
BLAKE2b-256 checksum
How to use checksums
884164736ec42022812a6f030c4a50bdbb435eaa51628e654f68a0f1caec4353
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / parse_errors-0.7.0-py3-none-any.whl

Download URL parse_errors-0.7.0-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d984c96cbe893ef0304318511d48e2c4c914d7f5113fc8bf02b275b01428dfc
BLAKE2b-256 checksum
How to use checksums
4c39ca12a91e6f2341dc53850a8e33097e4271687ad6e9101640e78adc984c72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page