parse-errors 0.0.0a1__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- parse_errors-0.6.0/.github/workflows/build.yml +73 -0
- parse_errors-0.6.0/.gitignore +113 -0
- parse_errors-0.6.0/.vars.ini +10 -0
- parse_errors-0.6.0/LICENSE +21 -0
- parse_errors-0.6.0/MANIFEST.in +1 -0
- parse_errors-0.6.0/Makefile +22 -0
- parse_errors-0.6.0/PKG-INFO +143 -0
- parse_errors-0.6.0/README.md +108 -0
- parse_errors-0.6.0/parse_errors/__init__.py +13 -0
- parse_errors-0.6.0/parse_errors/_jsonpath.py +71 -0
- parse_errors-0.6.0/parse_errors/_version.py +24 -0
- parse_errors-0.6.0/parse_errors/context.py +114 -0
- parse_errors-0.6.0/parse_errors/json_source_map/__init__.py +222 -0
- parse_errors-0.6.0/parse_errors/json_source_map/__main__.py +8 -0
- parse_errors-0.6.0/parse_errors/py.typed +0 -0
- parse_errors-0.6.0/parse_errors/source_map.py +155 -0
- parse_errors-0.6.0/parse_errors/toml_source_map/__init__.py +294 -0
- parse_errors-0.6.0/parse_errors/toml_source_map/__main__.py +8 -0
- parse_errors-0.6.0/parse_errors/yaml_source_map/__init__.py +160 -0
- parse_errors-0.6.0/parse_errors/yaml_source_map/__main__.py +8 -0
- parse_errors-0.6.0/parse_errors.egg-info/PKG-INFO +143 -0
- parse_errors-0.6.0/parse_errors.egg-info/SOURCES.txt +34 -0
- parse_errors-0.6.0/parse_errors.egg-info/dependency_links.txt +1 -0
- parse_errors-0.6.0/parse_errors.egg-info/requires.txt +19 -0
- parse_errors-0.6.0/parse_errors.egg-info/scm_file_list.json +30 -0
- parse_errors-0.6.0/parse_errors.egg-info/scm_version.json +8 -0
- parse_errors-0.6.0/parse_errors.egg-info/top_level.txt +1 -0
- parse_errors-0.6.0/pyproject.toml +105 -0
- parse_errors-0.6.0/setup.cfg +4 -0
- parse_errors-0.6.0/tests/__init__.py +1 -0
- parse_errors-0.6.0/tests/_types.py +10 -0
- parse_errors-0.6.0/tests/conftest.py +0 -0
- parse_errors-0.6.0/tests/test_parse_context_json.py +107 -0
- parse_errors-0.6.0/tests/test_parse_context_toml.py +96 -0
- parse_errors-0.6.0/tests/test_parse_context_yaml.py +48 -0
- parse_errors-0.6.0/tests/test_source_map.py +327 -0
- parse_errors-0.0.0a1/PKG-INFO +0 -5
- parse_errors-0.0.0a1/parse_errors/__init__.py +0 -1
- parse_errors-0.0.0a1/pyproject.toml +0 -11
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
name: Build
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- master
|
|
6
|
+
- main
|
|
7
|
+
- tmp-*
|
|
8
|
+
tags:
|
|
9
|
+
- v*
|
|
10
|
+
pull_request:
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
test:
|
|
14
|
+
runs-on: ${{ matrix.os }}
|
|
15
|
+
strategy:
|
|
16
|
+
fail-fast: false
|
|
17
|
+
matrix:
|
|
18
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
19
|
+
os: [macOS-latest, ubuntu-latest, windows-latest]
|
|
20
|
+
|
|
21
|
+
steps:
|
|
22
|
+
- name: Checkout
|
|
23
|
+
uses: actions/checkout@v4
|
|
24
|
+
- name: Set Up Python ${{ matrix.python-version }}
|
|
25
|
+
uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: ${{ matrix.python-version }}
|
|
28
|
+
allow-prereleases: true
|
|
29
|
+
- uses: astral-sh/setup-uv@v3
|
|
30
|
+
- name: Install
|
|
31
|
+
run: uv sync --extra test --extra dev
|
|
32
|
+
- name: Test
|
|
33
|
+
run: |
|
|
34
|
+
git config --global user.name "Unit Test"
|
|
35
|
+
git config --global user.email "example@example.com"
|
|
36
|
+
make test
|
|
37
|
+
- name: Lint
|
|
38
|
+
run: |
|
|
39
|
+
make lint
|
|
40
|
+
|
|
41
|
+
build:
|
|
42
|
+
needs: test
|
|
43
|
+
runs-on: ubuntu-latest
|
|
44
|
+
steps:
|
|
45
|
+
- uses: actions/checkout@v4
|
|
46
|
+
- uses: actions/setup-python@v5
|
|
47
|
+
with:
|
|
48
|
+
python-version: "3.14"
|
|
49
|
+
- uses: astral-sh/setup-uv@v3
|
|
50
|
+
- name: Install
|
|
51
|
+
run: uv sync --extra test --extra dev
|
|
52
|
+
- name: Build
|
|
53
|
+
run: uv run python -m build
|
|
54
|
+
- name: Upload
|
|
55
|
+
uses: actions/upload-artifact@v4
|
|
56
|
+
with:
|
|
57
|
+
name: sdist
|
|
58
|
+
path: dist
|
|
59
|
+
|
|
60
|
+
publish:
|
|
61
|
+
needs: build
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
64
|
+
environment: pypi
|
|
65
|
+
permissions:
|
|
66
|
+
contents: read
|
|
67
|
+
id-token: write
|
|
68
|
+
steps:
|
|
69
|
+
- uses: actions/download-artifact@v4
|
|
70
|
+
with:
|
|
71
|
+
name: sdist
|
|
72
|
+
path: dist
|
|
73
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
.installed.cfg
|
|
25
|
+
*.egg
|
|
26
|
+
MANIFEST
|
|
27
|
+
|
|
28
|
+
# PyInstaller
|
|
29
|
+
# Usually these files are written by a python script from a template
|
|
30
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
31
|
+
*.manifest
|
|
32
|
+
*.spec
|
|
33
|
+
|
|
34
|
+
# Installer logs
|
|
35
|
+
pip-log.txt
|
|
36
|
+
pip-delete-this-directory.txt
|
|
37
|
+
|
|
38
|
+
# Unit test / coverage reports
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.coverage
|
|
42
|
+
.coverage.*
|
|
43
|
+
.cache
|
|
44
|
+
nosetests.xml
|
|
45
|
+
coverage.xml
|
|
46
|
+
*.cover
|
|
47
|
+
.hypothesis/
|
|
48
|
+
.pytest_cache/
|
|
49
|
+
|
|
50
|
+
# Translations
|
|
51
|
+
*.mo
|
|
52
|
+
*.pot
|
|
53
|
+
|
|
54
|
+
# Django stuff:
|
|
55
|
+
*.log
|
|
56
|
+
local_settings.py
|
|
57
|
+
db.sqlite3
|
|
58
|
+
|
|
59
|
+
# Flask stuff:
|
|
60
|
+
instance/
|
|
61
|
+
.webassets-cache
|
|
62
|
+
|
|
63
|
+
# Scrapy stuff:
|
|
64
|
+
.scrapy
|
|
65
|
+
|
|
66
|
+
# Sphinx documentation
|
|
67
|
+
docs/_build/
|
|
68
|
+
|
|
69
|
+
# PyBuilder
|
|
70
|
+
target/
|
|
71
|
+
|
|
72
|
+
# Jupyter Notebook
|
|
73
|
+
.ipynb_checkpoints
|
|
74
|
+
|
|
75
|
+
# pyenv
|
|
76
|
+
.python-version
|
|
77
|
+
|
|
78
|
+
# celery beat schedule file
|
|
79
|
+
celerybeat-schedule
|
|
80
|
+
|
|
81
|
+
# SageMath parsed files
|
|
82
|
+
*.sage.py
|
|
83
|
+
|
|
84
|
+
# Environments
|
|
85
|
+
.env
|
|
86
|
+
.venv*
|
|
87
|
+
env/
|
|
88
|
+
venv/
|
|
89
|
+
ENV/
|
|
90
|
+
env.bak/
|
|
91
|
+
venv.bak/
|
|
92
|
+
|
|
93
|
+
# Spyder project settings
|
|
94
|
+
.spyderproject
|
|
95
|
+
.spyproject
|
|
96
|
+
|
|
97
|
+
# Rope project settings
|
|
98
|
+
.ropeproject
|
|
99
|
+
|
|
100
|
+
# mkdocs documentation
|
|
101
|
+
/site
|
|
102
|
+
|
|
103
|
+
# mypy
|
|
104
|
+
.mypy_cache/
|
|
105
|
+
|
|
106
|
+
# Visual Studio Code
|
|
107
|
+
.vscode/
|
|
108
|
+
|
|
109
|
+
# Vim swapfiles
|
|
110
|
+
*.sw[op]
|
|
111
|
+
|
|
112
|
+
# Setuptools-scm
|
|
113
|
+
_version.py
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[vars]
|
|
2
|
+
pypi_name = parse-errors
|
|
3
|
+
short_desc = re-raise parse errors with filename and line number
|
|
4
|
+
url = https://github.com/advice-animal/parse-errors/
|
|
5
|
+
author = Tim Hatch
|
|
6
|
+
author_email = tim@timhatch.com
|
|
7
|
+
package = parse_errors
|
|
8
|
+
year = 2026
|
|
9
|
+
author_website = https://timhatch.com/
|
|
10
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tim Hatch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include *.md LICENSE
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
.venv:
|
|
2
|
+
uv sync --extra dev --extra test
|
|
3
|
+
|
|
4
|
+
.PHONY: setup
|
|
5
|
+
setup:
|
|
6
|
+
uv sync --extra dev --extra test
|
|
7
|
+
|
|
8
|
+
.PHONY: test
|
|
9
|
+
test:
|
|
10
|
+
uv run coverage run -m pytest $(TESTOPTS)
|
|
11
|
+
uv run coverage report
|
|
12
|
+
|
|
13
|
+
.PHONY: format
|
|
14
|
+
format:
|
|
15
|
+
uv run ruff format
|
|
16
|
+
uv run ruff check --fix
|
|
17
|
+
|
|
18
|
+
.PHONY: lint
|
|
19
|
+
lint:
|
|
20
|
+
uv run ruff check
|
|
21
|
+
uv run python -m checkdeps --allow-names parse_errors parse_errors
|
|
22
|
+
uv run mypy --strict --install-types --non-interactive parse_errors
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: parse-errors
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: re-raise parse errors with filename and line number
|
|
5
|
+
Author-email: Tim Hatch <tim@timhatch.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/advice-animal/parse-errors/
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: pyyaml
|
|
18
|
+
Requires-Dist: tree-sitter!=0.26.0
|
|
19
|
+
Requires-Dist: tree-sitter-json
|
|
20
|
+
Requires-Dist: tree-sitter-toml
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: build>=1; extra == "dev"
|
|
23
|
+
Requires-Dist: checkdeps==0.9.0; extra == "dev"
|
|
24
|
+
Requires-Dist: mypy==1.19.1; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff==0.15.6; extra == "dev"
|
|
26
|
+
Requires-Dist: setuptools>=65; extra == "dev"
|
|
27
|
+
Requires-Dist: ty==0.0.81; extra == "dev"
|
|
28
|
+
Requires-Dist: types-pyyaml; extra == "dev"
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: coverage>=7.14; extra == "test"
|
|
31
|
+
Requires-Dist: msgspec; extra == "test"
|
|
32
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
33
|
+
Requires-Dist: tomli; extra == "test"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# parse-errors
|
|
37
|
+
|
|
38
|
+
`parse-errors` improves the errors you get when parsing config files (JSON,
|
|
39
|
+
TOML, YAML). Instead of a bare exception with a vague message, you get a
|
|
40
|
+
`ParseError` that includes the filename, line number, and column — so you can
|
|
41
|
+
point users straight to the problem.
|
|
42
|
+
|
|
43
|
+
It understands msgspec validation errors and TOML syntax errors, mapping them
|
|
44
|
+
back to the line of config they came from. Even unrelated exceptions get the
|
|
45
|
+
filename attached.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Wrap your parse/validate call in `ParseContext`:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import msgspec
|
|
53
|
+
import tomllib
|
|
54
|
+
from parse_errors import ParseContext
|
|
55
|
+
|
|
56
|
+
class Config(msgspec.Struct):
|
|
57
|
+
host: str
|
|
58
|
+
port: int
|
|
59
|
+
|
|
60
|
+
filename = "config.toml"
|
|
61
|
+
|
|
62
|
+
with open(filename, "rb") as f:
|
|
63
|
+
raw = f.read()
|
|
64
|
+
|
|
65
|
+
with ParseContext(filename, data=raw, format="toml"):
|
|
66
|
+
data = tomllib.loads(raw.decode())
|
|
67
|
+
config = msgspec.convert(data, Config)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`ParseContext` will intercept exceptions. If it can analyze them for precise
|
|
71
|
+
locations, it will raise a `ParseError` exception with the original exception as
|
|
72
|
+
the cause. If it can't find location information, the original exception is
|
|
73
|
+
raised as-is. No exceptions are swallowed.
|
|
74
|
+
|
|
75
|
+
As a concrete example, if the `msgspec.convert` raises because `port` is a
|
|
76
|
+
string instead of an integer, you get something like:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
parse_errors.ParseError: config.toml:3:8: Expected `int`, got `str` - at `$.port`
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
rather than the bare `msgspec.ValidationError` with no location.
|
|
83
|
+
|
|
84
|
+
`ParseContext` also handles errors from TOML and other decoders that include
|
|
85
|
+
positional information (`at line N, column M`) and re-raises them in the same
|
|
86
|
+
`filename:line:col: message` format.
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from parse_errors import ParseContext, ParseError
|
|
92
|
+
from parse_errors.source_map import SourceMap, build_source_map, locate_pointer
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**`ParseContext(filename, *, data=None, format=None)`** — context manager.
|
|
96
|
+
|
|
97
|
+
- `filename`: path to the file being parsed (used in error messages and to
|
|
98
|
+
infer the format from the extension when `format` is omitted).
|
|
99
|
+
- `data`: the file contents as `str` or `bytes`. If omitted, the file is read
|
|
100
|
+
from disk automatically when location info is needed.
|
|
101
|
+
- `format`: `"json"`, `"toml"`, or `"yaml"`. Inferred from `filename`'s
|
|
102
|
+
extension when not supplied.
|
|
103
|
+
|
|
104
|
+
**`ParseError`** — the exception raised inside the context. Has attributes
|
|
105
|
+
`filename`, `line` (1-based), and `column` (1-based).
|
|
106
|
+
|
|
107
|
+
**`locate_pointer(source, fmt, pointer)`** — returns the best source-map entry
|
|
108
|
+
for one JSON Pointer without building a full map. `fmt` is `"json"`,
|
|
109
|
+
`"toml"`, or `"yaml"`; `source` is `str` or UTF-8 `bytes`; `pointer` uses RFC
|
|
110
|
+
6901 escaping. If the exact pointer is not present, the result matches
|
|
111
|
+
`closest_entry(build_source_map(source, fmt), pointer)`: the nearest enclosing
|
|
112
|
+
value when one exists, otherwise `None`.
|
|
113
|
+
|
|
114
|
+
**`SourceMap(source, fmt)`** — caches the parsed document for repeated targeted
|
|
115
|
+
lookups. Use `SourceMap(...).locate(pointer)` when several errors in the same
|
|
116
|
+
document need locations. It still avoids constructing a full pointer-to-entry
|
|
117
|
+
map.
|
|
118
|
+
|
|
119
|
+
**`build_source_map(source, fmt)`** — builds the full pointer-to-location map.
|
|
120
|
+
This is useful when callers need many arbitrary entries or need to inspect all
|
|
121
|
+
locations.
|
|
122
|
+
|
|
123
|
+
**Warning:** source-map helpers locate nodes in a document; they are not
|
|
124
|
+
validating parsers. JSON and TOML location support uses tree-sitter so it can
|
|
125
|
+
return a location from a syntax tree even when a real decoder would reject the
|
|
126
|
+
source. Parse or validate the document with your normal parser first, then use
|
|
127
|
+
these helpers only to map known error paths back to source locations.
|
|
128
|
+
|
|
129
|
+
# Version Compat
|
|
130
|
+
|
|
131
|
+
This library is compatible with Python 3.10+, but should be linted under the
|
|
132
|
+
newest stable version.
|
|
133
|
+
|
|
134
|
+
# Versioning
|
|
135
|
+
|
|
136
|
+
This library follows [meanver](https://meanver.org/) which basically means
|
|
137
|
+
[semver](https://semver.org/) along with a promise to rename when the major
|
|
138
|
+
version changes.
|
|
139
|
+
|
|
140
|
+
# License
|
|
141
|
+
|
|
142
|
+
parse-errors is copyright [Tim Hatch](https://timhatch.com/), and licensed under
|
|
143
|
+
the MIT license. See the `LICENSE` file for details.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# parse-errors
|
|
2
|
+
|
|
3
|
+
`parse-errors` improves the errors you get when parsing config files (JSON,
|
|
4
|
+
TOML, YAML). Instead of a bare exception with a vague message, you get a
|
|
5
|
+
`ParseError` that includes the filename, line number, and column — so you can
|
|
6
|
+
point users straight to the problem.
|
|
7
|
+
|
|
8
|
+
It understands msgspec validation errors and TOML syntax errors, mapping them
|
|
9
|
+
back to the line of config they came from. Even unrelated exceptions get the
|
|
10
|
+
filename attached.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Wrap your parse/validate call in `ParseContext`:
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
import msgspec
|
|
18
|
+
import tomllib
|
|
19
|
+
from parse_errors import ParseContext
|
|
20
|
+
|
|
21
|
+
class Config(msgspec.Struct):
|
|
22
|
+
host: str
|
|
23
|
+
port: int
|
|
24
|
+
|
|
25
|
+
filename = "config.toml"
|
|
26
|
+
|
|
27
|
+
with open(filename, "rb") as f:
|
|
28
|
+
raw = f.read()
|
|
29
|
+
|
|
30
|
+
with ParseContext(filename, data=raw, format="toml"):
|
|
31
|
+
data = tomllib.loads(raw.decode())
|
|
32
|
+
config = msgspec.convert(data, Config)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`ParseContext` will intercept exceptions. If it can analyze them for precise
|
|
36
|
+
locations, it will raise a `ParseError` exception with the original exception as
|
|
37
|
+
the cause. If it can't find location information, the original exception is
|
|
38
|
+
raised as-is. No exceptions are swallowed.
|
|
39
|
+
|
|
40
|
+
As a concrete example, if the `msgspec.convert` raises because `port` is a
|
|
41
|
+
string instead of an integer, you get something like:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
parse_errors.ParseError: config.toml:3:8: Expected `int`, got `str` - at `$.port`
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
rather than the bare `msgspec.ValidationError` with no location.
|
|
48
|
+
|
|
49
|
+
`ParseContext` also handles errors from TOML and other decoders that include
|
|
50
|
+
positional information (`at line N, column M`) and re-raises them in the same
|
|
51
|
+
`filename:line:col: message` format.
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from parse_errors import ParseContext, ParseError
|
|
57
|
+
from parse_errors.source_map import SourceMap, build_source_map, locate_pointer
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**`ParseContext(filename, *, data=None, format=None)`** — context manager.
|
|
61
|
+
|
|
62
|
+
- `filename`: path to the file being parsed (used in error messages and to
|
|
63
|
+
infer the format from the extension when `format` is omitted).
|
|
64
|
+
- `data`: the file contents as `str` or `bytes`. If omitted, the file is read
|
|
65
|
+
from disk automatically when location info is needed.
|
|
66
|
+
- `format`: `"json"`, `"toml"`, or `"yaml"`. Inferred from `filename`'s
|
|
67
|
+
extension when not supplied.
|
|
68
|
+
|
|
69
|
+
**`ParseError`** — the exception raised inside the context. Has attributes
|
|
70
|
+
`filename`, `line` (1-based), and `column` (1-based).
|
|
71
|
+
|
|
72
|
+
**`locate_pointer(source, fmt, pointer)`** — returns the best source-map entry
|
|
73
|
+
for one JSON Pointer without building a full map. `fmt` is `"json"`,
|
|
74
|
+
`"toml"`, or `"yaml"`; `source` is `str` or UTF-8 `bytes`; `pointer` uses RFC
|
|
75
|
+
6901 escaping. If the exact pointer is not present, the result matches
|
|
76
|
+
`closest_entry(build_source_map(source, fmt), pointer)`: the nearest enclosing
|
|
77
|
+
value when one exists, otherwise `None`.
|
|
78
|
+
|
|
79
|
+
**`SourceMap(source, fmt)`** — caches the parsed document for repeated targeted
|
|
80
|
+
lookups. Use `SourceMap(...).locate(pointer)` when several errors in the same
|
|
81
|
+
document need locations. It still avoids constructing a full pointer-to-entry
|
|
82
|
+
map.
|
|
83
|
+
|
|
84
|
+
**`build_source_map(source, fmt)`** — builds the full pointer-to-location map.
|
|
85
|
+
This is useful when callers need many arbitrary entries or need to inspect all
|
|
86
|
+
locations.
|
|
87
|
+
|
|
88
|
+
**Warning:** source-map helpers locate nodes in a document; they are not
|
|
89
|
+
validating parsers. JSON and TOML location support uses tree-sitter so it can
|
|
90
|
+
return a location from a syntax tree even when a real decoder would reject the
|
|
91
|
+
source. Parse or validate the document with your normal parser first, then use
|
|
92
|
+
these helpers only to map known error paths back to source locations.
|
|
93
|
+
|
|
94
|
+
# Version Compat
|
|
95
|
+
|
|
96
|
+
This library is compatible with Python 3.10+, but should be linted under the
|
|
97
|
+
newest stable version.
|
|
98
|
+
|
|
99
|
+
# Versioning
|
|
100
|
+
|
|
101
|
+
This library follows [meanver](https://meanver.org/) which basically means
|
|
102
|
+
[semver](https://semver.org/) along with a promise to rename when the major
|
|
103
|
+
version changes.
|
|
104
|
+
|
|
105
|
+
# License
|
|
106
|
+
|
|
107
|
+
parse-errors is copyright [Tim Hatch](https://timhatch.com/), and licensed under
|
|
108
|
+
the MIT license. See the `LICENSE` file for details.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""re-raise parse errors with filename and line number."""
|
|
2
|
+
|
|
3
|
+
from .context import ParseContext, ParseError
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
from ._version import __version__
|
|
7
|
+
except ImportError: # pragma: no cover
|
|
8
|
+
__version__ = "dev"
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"ParseContext",
|
|
12
|
+
"ParseError",
|
|
13
|
+
]
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Convert JSONPath expressions to JSON Pointer (RFC 6901)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
# Matches a single step in a JSONPath: .key or [index] or ['key'] or ["key"]
|
|
8
|
+
_STEP = re.compile(
|
|
9
|
+
r"\.(?P<name>[^.\[]+)" # .key
|
|
10
|
+
r"|\[(?P<idx>\d+)\]" # [0]
|
|
11
|
+
r"|\[\'(?P<sq>[^\']*)\'\]" # ['key']
|
|
12
|
+
r'|\["(?P<dq>[^"]*)"\]' # ["key"]
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
# Pattern to extract JSONPath from msgspec-style error messages: "... - at `$.foo.bar`"
|
|
16
|
+
_AT_PATH = re.compile(r" - at `(\$[^`]*)`")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def jsonpath_to_pointer(jsonpath: str) -> str:
|
|
20
|
+
"""Convert a JSONPath string like ``$.foo[0].bar`` to a JSON Pointer like ``/foo/0/bar``.
|
|
21
|
+
|
|
22
|
+
Only supports simple dot-notation and bracket-index forms as produced by
|
|
23
|
+
msgspec. Does not support filter expressions or wildcards.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
jsonpath: A JSONPath string starting with ``$``.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
A JSON Pointer string (RFC 6901), e.g. ``/foo/0/bar``.
|
|
30
|
+
"""
|
|
31
|
+
if jsonpath == "$":
|
|
32
|
+
return ""
|
|
33
|
+
if not jsonpath.startswith("$"):
|
|
34
|
+
raise ValueError(f"JSONPath must start with '$', got: {jsonpath!r}")
|
|
35
|
+
|
|
36
|
+
tail = jsonpath[1:] # strip leading $
|
|
37
|
+
parts: list[str] = []
|
|
38
|
+
|
|
39
|
+
pos = 0
|
|
40
|
+
while pos < len(tail):
|
|
41
|
+
m = _STEP.match(tail, pos)
|
|
42
|
+
if m is None:
|
|
43
|
+
raise ValueError(
|
|
44
|
+
f"Cannot parse JSONPath step at position {pos}: {tail[pos:]!r}"
|
|
45
|
+
)
|
|
46
|
+
for group in ("name", "sq", "dq", "idx"):
|
|
47
|
+
if (name := m.group(group)) is not None:
|
|
48
|
+
break
|
|
49
|
+
parts.append(_escape(name))
|
|
50
|
+
pos = m.end()
|
|
51
|
+
|
|
52
|
+
return "/" + "/".join(parts) if parts else ""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def extract_jsonpath(message: str) -> str | None:
|
|
56
|
+
"""Extract a JSONPath expression from an error message.
|
|
57
|
+
|
|
58
|
+
Looks for the pattern ``- at `$.path``` as used by msgspec.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
message: The exception message string.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
The JSONPath string if found, otherwise ``None``.
|
|
65
|
+
"""
|
|
66
|
+
m = _AT_PATH.search(message)
|
|
67
|
+
return m.group(1) if m else None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _escape(segment: str) -> str:
|
|
71
|
+
return segment.replace("~", "~0").replace("/", "~1")
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '0.6.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 6, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = 'g81d17405e'
|