Skip to content

Commit 9af386a

Browse files
authored
Cleanup json schema validation (#10074)
1 parent 1f85e6f commit 9af386a

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/poetry/json/__init__.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
import json
44

5-
from pathlib import Path
5+
from importlib.resources import files
66
from typing import Any
77

88
import fastjsonschema
99

1010
from fastjsonschema.exceptions import JsonSchemaValueException
11-
from poetry.core.json import SCHEMA_DIR as CORE_SCHEMA_DIR
12-
13-
14-
SCHEMA_DIR = Path(__file__).parent / "schemas"
1511

1612

1713
def validate_object(obj: dict[str, Any]) -> list[str]:
18-
schema_file = Path(SCHEMA_DIR, "poetry.json")
19-
schema = json.loads(schema_file.read_text(encoding="utf-8"))
14+
schema = json.loads(
15+
(files(__package__) / "schemas" / "poetry.json").read_text(encoding="utf-8")
16+
)
2017

2118
validate = fastjsonschema.compile(schema)
2219

@@ -27,7 +24,9 @@ def validate_object(obj: dict[str, Any]) -> list[str]:
2724
errors = [e.message]
2825

2926
core_schema = json.loads(
30-
(CORE_SCHEMA_DIR / "poetry-schema.json").read_text(encoding="utf-8")
27+
(files("poetry.core") / "json" / "schemas" / "poetry-schema.json").read_text(
28+
encoding="utf-8"
29+
)
3130
)
3231

3332
properties = schema["properties"].keys() | core_schema["properties"].keys()

tests/json/test_schema.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import json
44

5+
from importlib.resources import files
56
from pathlib import Path
67
from typing import Any
78

8-
from poetry.core.json import SCHEMA_DIR as CORE_SCHEMA_DIR
9-
109
from poetry.factory import Factory
11-
from poetry.json import SCHEMA_DIR
1210
from poetry.toml import TOMLFile
1311

1412

13+
SCHEMA_FILE = files("poetry.json") / "schemas" / "poetry.json"
1514
FIXTURE_DIR = Path(__file__).parent / "fixtures"
1615
SOURCE_FIXTURE_DIR = FIXTURE_DIR / "source"
1716

@@ -59,12 +58,14 @@ def test_self_invalid_plugin() -> None:
5958

6059

6160
def test_dependencies_is_consistent_to_poetry_core_schema() -> None:
62-
with (SCHEMA_DIR / "poetry.json").open(encoding="utf-8") as f:
61+
with SCHEMA_FILE.open(encoding="utf-8") as f:
6362
schema = json.load(f)
6463
dependency_definitions = {
6564
key: value for key, value in schema["definitions"].items() if "depend" in key
6665
}
67-
with (CORE_SCHEMA_DIR / "poetry-schema.json").open(encoding="utf-8") as f:
66+
with (files("poetry.core") / "json" / "schemas" / "poetry-schema.json").open(
67+
encoding="utf-8"
68+
) as f:
6869
core_schema = json.load(f)
6970
core_dependency_definitions = {
7071
key: value

0 commit comments

Comments
 (0)