Skip to content

Commit

Permalink
fix: Fix JSON schema for ending line numbers (and add test)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 3, 2023
1 parent ab7a3be commit 318c6b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
27 changes: 22 additions & 5 deletions docs/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Griffe object.",
"oneOf": [
{
Expand All @@ -26,9 +26,17 @@
"type": "string"
},
"lineno": {
"title": "For aliases, the line number in their own module.",
"title": "For aliases, the import starting line number in their own module.",
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Alias.lineno",
"type": "integer"
},
"endlineno": {
"title": "For aliases, the import ending line number in their own module.",
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Alias.endlineno",
"type": [
"integer",
"null"
]
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -101,7 +109,10 @@
"endlineno": {
"title": "The docstring ending line number.",
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Docstring.endlineno",
"type": "integer"
"type": [
"integer",
"null"
]
},
"parsed": {
"title": "The parsed docstring (list of docstring sections).",
Expand Down Expand Up @@ -162,7 +173,10 @@
"endlineno": {
"title": "The docstring ending line number.",
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Docstring.endlineno",
"type": "integer"
"type": [
"integer",
"null"
]
},
"bases": true,
"decorators": true,
Expand Down Expand Up @@ -221,7 +235,10 @@
"endlineno": {
"title": "The decorator ending line number.",
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Decorator.endlineno",
"type": "integer"
"type": [
"integer",
"null"
]
}
},
"additionalProperties": false,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ tests = [
"pytest-randomly>=3.10",
"pytest-xdist>=2.4",
"hypothesmith>=0.2.0",
"jsonschema>=4.17.3",
]
typing = [
"mypy>=0.910",
Expand Down
27 changes: 27 additions & 0 deletions tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from __future__ import annotations

import json

import pytest
from jsonschema import ValidationError, validate

from griffe.dataclasses import Function, Module, Object
from griffe.loader import GriffeLoader
Expand Down Expand Up @@ -30,3 +33,27 @@ def test_minimal_data_is_enough() -> None:
with pytest.raises(TypeError) as err:
Function.from_json(minimal)
assert "provided JSON object is not of type" in str(err.value)


# use this function in test_json_schema to ease schema debugging
def _validate(obj: dict, schema: dict):
if "members" in obj:
for member in obj["members"]:
_validate(member, schema)

try:
validate(obj, schema)
except ValidationError:
print(obj["path"])
raise


def test_json_schema() -> None:
"""Assert that our serialized data matches our JSON schema."""
loader = GriffeLoader()
module = loader.load_module("griffe")
loader.resolve_aliases()
data = json.loads(module.as_json(full=True))
with open("docs/schema.json") as f:
schema = json.load(f)
validate(data, schema)

0 comments on commit 318c6b4

Please sign in to comment.