Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion parse_errors/_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def jsonpath_to_pointer(jsonpath: str) -> str:
raise ValueError(
f"Cannot parse JSONPath step at position {pos}: {tail[pos:]!r}"
)
name = m.group("name") or m.group("sq") or m.group("dq") or m.group("idx")
for group in ("name", "sq", "dq", "idx"):
if (name := m.group(group)) is not None:
break
parts.append(_escape(name))
pos = m.end()

Expand Down
21 changes: 15 additions & 6 deletions parse_errors/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,27 @@ def ParseContext(

try:
pointer = jsonpath_to_pointer(jsonpath)
except ValueError: # pragma: no cover
raise exc
except ValueError:
raise ParseError(
f"{filename}: {exc!r}", filename=filename, line=1
) from exc

fmt = format or detect_format(path)
assert fmt is not None
fmt = format.lower() if format else detect_format(path)
if fmt == "yml":
fmt = "yaml"
if fmt not in ("json", "toml", "yaml"):
raise ParseError(
f"{filename}: {exc!r}", filename=filename, line=1
) from exc

source = data if data is not None else path.read_bytes()
source_map = build_source_map(source, fmt)

entry = closest_entry(source_map, pointer)
if entry is None: # pragma: no cover
raise exc
if entry is None:
raise ParseError(
f"{filename}: {exc!r}", filename=filename, line=1
) from exc

loc = entry.value_start
# Lines are 0-based in source maps; convert to 1-based for humans.
Expand Down
Loading