Skip to content

Commit

Permalink
jsonschema -> fastjsonschema
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and radoering committed Sep 23, 2023
1 parent 288cb05 commit e9d34f1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 123 deletions.
3 changes: 1 addition & 2 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ test_task:
- POETRY_HOME=/opt/poetry
- $PYTHON -m venv $POETRY_HOME
- $POETRY_HOME/bin/pip install --upgrade pip setuptools wheel
# jsonschema 4.18 uses Rust-based libraries which causes issues when building from source
- $POETRY_HOME/bin/pip install poetry "jsonschema<4.18.0"
- $POETRY_HOME/bin/pip install poetry
- echo "PATH=$POETRY_HOME/bin:$PATH" >> $CIRRUS_ENV
install_and_test_script:
- poetry install
Expand Down
113 changes: 15 additions & 98 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ cachecontrol = { version = "^0.13.0", extras = ["filecache"] }
cleo = "^2.0.0"
crashtest = "^0.4.1"
dulwich = "^0.21.2"
fastjsonschema = "^2.18.0"
importlib-metadata = { version = ">=4.4", python = "<3.10" }
installer = "^0.7.0"
# jsonschema 4.18 uses Rust-based libraries which causes issues when building from source
jsonschema = ">=4.10.0,<4.18.0"
keyring = "^24.0.0"
# packaging uses calver, so version is unclamped
packaging = ">=20.4"
Expand Down Expand Up @@ -74,7 +73,6 @@ pytest-xdist = { version = "^3.1", extras = ["psutil"] }

[tool.poetry.group.typing.dependencies]
mypy = ">=1.0"
types-jsonschema = ">=4.9.0"
types-requests = ">=2.28.8"

# only used in github actions
Expand Down Expand Up @@ -181,6 +179,7 @@ warn_unused_ignores = false
[[tool.mypy.overrides]]
module = [
'deepdiff.*',
'fastjsonschema.*',
'httpretty.*',
'keyring.*',
'pexpect.*',
Expand Down
18 changes: 7 additions & 11 deletions src/poetry/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from pathlib import Path
from typing import Any

import jsonschema
import fastjsonschema

from fastjsonschema.exceptions import JsonSchemaException
from poetry.core.json import SCHEMA_DIR as CORE_SCHEMA_DIR


Expand All @@ -21,18 +22,13 @@ def validate_object(obj: dict[str, Any]) -> list[str]:
schema_file = Path(SCHEMA_DIR, "poetry.json")
schema = json.loads(schema_file.read_text(encoding="utf-8"))

validator = jsonschema.Draft7Validator(schema)
validation_errors = sorted(validator.iter_errors(obj), key=lambda e: e.path)
validate = fastjsonschema.compile(schema)

errors = []

for error in validation_errors:
message = error.message
if error.path:
path = ".".join(str(x) for x in error.absolute_path)
message = f"[{path}] {message}"

errors.append(message)
try:
validate(obj)
except JsonSchemaException as e:
errors = [e.message]

core_schema = json.loads(
(CORE_SCHEMA_DIR / "poetry-schema.json").read_text(encoding="utf-8")
Expand Down
12 changes: 3 additions & 9 deletions tests/json/test_schema_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_pyproject_toml_invalid_priority() -> None:
content = toml["tool"]["poetry"]
assert Factory.validate(content) == {
"errors": [
"[source.0.priority] 'arbitrary' is not one of ['primary', 'default',"
" 'secondary', 'supplemental', 'explicit']"
"data.source[0].priority must be one of ['primary', 'default', "
"'secondary', 'supplemental', 'explicit']"
],
"warnings": [],
}
Expand All @@ -42,12 +42,6 @@ def test_pyproject_toml_invalid_priority_legacy_and_new() -> None:
).read()
content = toml["tool"]["poetry"]
assert Factory.validate(content) == {
"errors": [
"[source.0] {'name': 'pypi-simple', 'url': "
"'https://pypi.org/simple/', 'default': False, 'priority': "
"'primary'} should not be valid under {'anyOf': [{'required': "
"['priority', 'default']}, {'required': ['priority', "
"'secondary']}]}"
],
"errors": ["data.source[0] must NOT match a disallowed definition"],
"warnings": [],
}

0 comments on commit e9d34f1

Please sign in to comment.