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
14 changes: 14 additions & 0 deletions src/check_datapackage/check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import sys
from dataclasses import dataclass, field
from functools import reduce
from typing import Any, Callable, Iterator, Optional
Expand Down Expand Up @@ -47,6 +48,19 @@ class for more details, especially about the default values.
issues += apply_extensions(properties, config.extensions)
issues = exclude(issues, config.exclusions, properties)

if error and issues:
# TODO: Switch to using `explain()` once implemented
errors: list[str] = _map(
issues,
lambda issue: f"- Property `{issue.jsonpath}`: {issue.message}\n",
)
# Should hide the traceback for the user
sys.tracebacklimit = 0
raise Exception(
"There were some issues found in your `datapackage.json`:\n\n"
+ "\n".join(errors)
)

return sorted(set(issues))


Expand Down
10 changes: 5 additions & 5 deletions src/check_datapackage/read_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def read_json(path: Path) -> dict[str, Any]:
dictionary.
"""
try:
descriptor: Any = loads(path.read_text())
properties: dict[str, Any] = loads(path.read_text())
except JSONDecodeError as error:
raise JSONDecodeError(
f"The path {path} couldn't be parsed as JSON. Is there a typo or other "
Expand All @@ -28,11 +28,11 @@ def read_json(path: Path) -> dict[str, Any]:
pos=error.pos,
) from None # To hide the original traceback

if not isinstance(descriptor, dict):
if not isinstance(properties, dict): # pyright: ignore
raise TypeError(
f"The file {path} should parse into a Python dictionary (`dict`) "
f"but it converts to the type `{type(descriptor)}`. Is the file "
f"but it converts to the type `{type(properties)}`. Is the file "
"missing a curly bracket `{` at the beginning or `}` at the end?"
)
) from None # To hide the original traceback

return descriptor
return properties
11 changes: 10 additions & 1 deletion tests/test_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from pytest import mark
from pytest import mark, raises

from check_datapackage.check import check
from check_datapackage.config import Config
Expand Down Expand Up @@ -272,3 +272,12 @@ def test_fail_with_bad_resource_path(path, location, type):
assert len(issues) == 1
assert issues[0].type == type
assert issues[0].jsonpath == location


def test_error_as_true():
properties = {
"name": 123,
}

with raises(Exception):
check(properties, error=True)
3 changes: 2 additions & 1 deletion tools/vulture-allowlist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa
# mypy: ignore-errors
version # unused variable (src/check_datapackage/config.py:40)
tracebacklimit # unused attribute (src/check_datapackage/check.py:58)
version # unused variable (src/check_datapackage/config.py:44)