andvari 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {andvari-0.2.0 → andvari-0.3.0}/PKG-INFO +12 -2
- {andvari-0.2.0 → andvari-0.3.0}/README.md +11 -1
- {andvari-0.2.0 → andvari-0.3.0}/pyproject.toml +1 -1
- {andvari-0.2.0 → andvari-0.3.0}/pyproject.toml.orig +1 -1
- andvari-0.3.0/src/andvari/__init__.py +3 -0
- {andvari-0.2.0 → andvari-0.3.0}/src/andvari/diff.py +53 -20
- andvari-0.2.0/src/andvari/__init__.py +0 -3
- {andvari-0.2.0 → andvari-0.3.0}/LICENSE +0 -0
- {andvari-0.2.0 → andvari-0.3.0}/src/andvari/cli.py +0 -0
- {andvari-0.2.0 → andvari-0.3.0}/src/andvari/models.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: andvari
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Deterministic diff of two versions of a JSON Schema, and Pydantic models from one
|
|
5
5
|
Author: unpunnyfuns
|
|
6
6
|
Author-email: unpunnyfuns <unpunnyfuns@gmail.com>
|
|
@@ -144,9 +144,19 @@ new = {"type": "object", "properties": {"id": {"type": "string"}}}
|
|
|
144
144
|
|
|
145
145
|
print(fingerprint(old) == fingerprint(new))
|
|
146
146
|
for change in diff(old, new):
|
|
147
|
-
print(change.severity, change.path, change.keyword, change.reason)
|
|
147
|
+
print(change.severity, change.path, change.keyword, change.reason, change.old, change.new)
|
|
148
148
|
```
|
|
149
149
|
|
|
150
|
+
Each change carries what was there before and after:
|
|
151
|
+
|
|
152
|
+
| Change to | `old` and `new` |
|
|
153
|
+
| ------------------- | ----------------------------------------------------------- |
|
|
154
|
+
| A keyword | Its two values |
|
|
155
|
+
| A whole path | The path's row from `flatten`: its keywords and a `required` flag |
|
|
156
|
+
| A whole combinator | Its branches, as `path -> row` |
|
|
157
|
+
|
|
158
|
+
The side with nothing is `andvari.MISSING`, because `None` is a value a schema can hold. The command line output does not show these values.
|
|
159
|
+
|
|
150
160
|
## Setup and tests
|
|
151
161
|
|
|
152
162
|
```sh
|
|
@@ -131,9 +131,19 @@ new = {"type": "object", "properties": {"id": {"type": "string"}}}
|
|
|
131
131
|
|
|
132
132
|
print(fingerprint(old) == fingerprint(new))
|
|
133
133
|
for change in diff(old, new):
|
|
134
|
-
print(change.severity, change.path, change.keyword, change.reason)
|
|
134
|
+
print(change.severity, change.path, change.keyword, change.reason, change.old, change.new)
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
+
Each change carries what was there before and after:
|
|
138
|
+
|
|
139
|
+
| Change to | `old` and `new` |
|
|
140
|
+
| ------------------- | ----------------------------------------------------------- |
|
|
141
|
+
| A keyword | Its two values |
|
|
142
|
+
| A whole path | The path's row from `flatten`: its keywords and a `required` flag |
|
|
143
|
+
| A whole combinator | Its branches, as `path -> row` |
|
|
144
|
+
|
|
145
|
+
The side with nothing is `andvari.MISSING`, because `None` is a value a schema can hold. The command line output does not show these values.
|
|
146
|
+
|
|
137
147
|
## Setup and tests
|
|
138
148
|
|
|
139
149
|
```sh
|
|
@@ -52,12 +52,20 @@ class Change(NamedTuple):
|
|
|
52
52
|
|
|
53
53
|
`keyword` is None when a whole field was added or removed, and the name of
|
|
54
54
|
the keyword when a field exists in both and one of its keywords differs.
|
|
55
|
+
|
|
56
|
+
`old` and `new` hold what was there before and after, so that whoever acts
|
|
57
|
+
on a change does not have to look it up again. For a keyword they are its
|
|
58
|
+
two values. For a whole field they are its row from `flatten`. For a
|
|
59
|
+
combinator they are its branches, as a table of `path -> row`. The side
|
|
60
|
+
that has nothing is `MISSING`.
|
|
55
61
|
"""
|
|
56
62
|
|
|
57
63
|
path: str
|
|
58
64
|
keyword: str | None
|
|
59
65
|
severity: str
|
|
60
66
|
reason: str
|
|
67
|
+
old: object = MISSING
|
|
68
|
+
new: object = MISSING
|
|
61
69
|
|
|
62
70
|
|
|
63
71
|
def canonical(value: object) -> str:
|
|
@@ -306,11 +314,26 @@ def branched(table: dict) -> set:
|
|
|
306
314
|
return {(parent(path), row["branch of"]) for path, row in table.items() if "branch of" in row}
|
|
307
315
|
|
|
308
316
|
|
|
309
|
-
def
|
|
317
|
+
def branches(table: dict, node: str, keyword: str) -> dict:
|
|
318
|
+
"""Return the rows of the branches that `keyword` has on `node`, by path."""
|
|
319
|
+
return {
|
|
320
|
+
path: row
|
|
321
|
+
for path, row in table.items()
|
|
322
|
+
if row.get("branch of") == keyword and parent(path) == node
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def sides(value: object, added: bool) -> tuple[object, object]:
|
|
327
|
+
"""Return `(old, new)` for something that exists in one version only."""
|
|
328
|
+
return (MISSING, value) if added else (value, MISSING)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def branch_change(path: str, keyword: str, own: dict, other: dict, added: bool) -> Change | None:
|
|
310
332
|
"""Label a branch of the combinator `keyword` that exists in one version only.
|
|
311
333
|
|
|
312
|
-
`
|
|
313
|
-
the
|
|
334
|
+
`own` is the table of the version that has the branch and `other` the
|
|
335
|
+
table of the version without it. Returns None when the keyword comparison
|
|
336
|
+
on the row reports the difference instead.
|
|
314
337
|
"""
|
|
315
338
|
node = parent(path)
|
|
316
339
|
verb = "added" if added else "removed"
|
|
@@ -321,7 +344,7 @@ def branch_change(path: str, keyword: str, other: dict, added: bool) -> Change |
|
|
|
321
344
|
# alternative is a warning for the same reason a new `enum` value is.
|
|
322
345
|
narrows = added == (keyword == "allOf")
|
|
323
346
|
severity = "breaking" if narrows else "warning" if added else "compatible"
|
|
324
|
-
return Change(path, None, severity, f"branch {verb}")
|
|
347
|
+
return Change(path, None, severity, f"branch {verb}", *sides(own[path], added))
|
|
325
348
|
if keyword in other[node]:
|
|
326
349
|
# The other version has the keyword as a whole value, such as an empty
|
|
327
350
|
# list, and the keyword comparison on the row reports the difference.
|
|
@@ -329,7 +352,9 @@ def branch_change(path: str, keyword: str, other: dict, added: bool) -> Change |
|
|
|
329
352
|
# The whole combinator appeared or went. Its branches would all say the
|
|
330
353
|
# same thing, so it is one change on the node. New conditions are
|
|
331
354
|
# breaking whatever the combinator, and dropping them accepts more.
|
|
332
|
-
|
|
355
|
+
severity = "breaking" if added else "compatible"
|
|
356
|
+
present = branches(own, node, keyword)
|
|
357
|
+
return Change(node, keyword, severity, f"{keyword} {verb}", *sides(present, added))
|
|
333
358
|
|
|
334
359
|
|
|
335
360
|
def diff(old: dict, new: dict) -> list[Change]:
|
|
@@ -338,9 +363,13 @@ def diff(old: dict, new: dict) -> list[Change]:
|
|
|
338
363
|
after = flatten(inline_refs(new))
|
|
339
364
|
gone = before.keys() - after.keys()
|
|
340
365
|
fresh = after.keys() - before.keys()
|
|
341
|
-
#
|
|
342
|
-
# the same single change.
|
|
343
|
-
changes =
|
|
366
|
+
# Keyed by path and keyword, because every branch of an added or removed
|
|
367
|
+
# combinator yields the same single change.
|
|
368
|
+
changes = {}
|
|
369
|
+
|
|
370
|
+
def record(change: Change | None) -> None:
|
|
371
|
+
if change:
|
|
372
|
+
changes[change.path, change.keyword] = change
|
|
344
373
|
|
|
345
374
|
# Removed fields. When a whole object goes, only the object is reported,
|
|
346
375
|
# not every field that was inside it.
|
|
@@ -348,25 +377,23 @@ def diff(old: dict, new: dict) -> list[Change]:
|
|
|
348
377
|
if parent(path) in gone:
|
|
349
378
|
continue
|
|
350
379
|
if keyword := before[path].get("branch of"):
|
|
351
|
-
|
|
352
|
-
changes.add(change)
|
|
380
|
+
record(branch_change(path, keyword, before, after, added=False))
|
|
353
381
|
else:
|
|
354
|
-
|
|
382
|
+
record(Change(path, None, "breaking", "path removed", old=before[path]))
|
|
355
383
|
|
|
356
384
|
# Added fields, with the same rule for whole objects.
|
|
357
385
|
for path in fresh:
|
|
358
386
|
if parent(path) in fresh:
|
|
359
387
|
continue
|
|
360
388
|
if after[path]["required"]:
|
|
361
|
-
|
|
389
|
+
record(Change(path, None, "breaking", "required property added", new=after[path]))
|
|
362
390
|
elif keyword := after[path].get("branch of"):
|
|
363
|
-
|
|
364
|
-
changes.add(change)
|
|
391
|
+
record(branch_change(path, keyword, after, before, added=True))
|
|
365
392
|
elif path.endswith("/*"):
|
|
366
393
|
# An array that had no item schema accepted anything in it.
|
|
367
|
-
|
|
394
|
+
record(Change(path, None, "breaking", "items added", new=after[path]))
|
|
368
395
|
else:
|
|
369
|
-
|
|
396
|
+
record(Change(path, None, "warning", "optional property added", new=after[path]))
|
|
370
397
|
|
|
371
398
|
# Fields present in both: compare every keyword either side has.
|
|
372
399
|
followed = branched(before) | branched(after)
|
|
@@ -384,11 +411,17 @@ def diff(old: dict, new: dict) -> list[Change]:
|
|
|
384
411
|
if (path, key) in followed:
|
|
385
412
|
# The keyword is missing from one row only because its
|
|
386
413
|
# branches were followed there. It went from a whole value to
|
|
387
|
-
# branches or back, which is a change, not an addition.
|
|
388
|
-
|
|
414
|
+
# branches or back, which is a change, not an addition. The
|
|
415
|
+
# side with branches reports those in place of a value.
|
|
416
|
+
if old_value is MISSING:
|
|
417
|
+
old_value = branches(before, path, key)
|
|
418
|
+
else:
|
|
419
|
+
new_value = branches(after, path, key)
|
|
420
|
+
record(Change(path, key, "breaking", f"{key} changed", old_value, new_value))
|
|
389
421
|
else:
|
|
390
|
-
|
|
422
|
+
severity, reason = classify(key, old_value, new_value)
|
|
423
|
+
record(Change(path, key, severity, reason, old_value, new_value))
|
|
391
424
|
|
|
392
425
|
# The loops above run over sets, whose order is not fixed. Sorting makes
|
|
393
426
|
# the output the same on every run.
|
|
394
|
-
return sorted(changes, key=lambda change: (change.path, change.keyword or ""))
|
|
427
|
+
return sorted(changes.values(), key=lambda change: (change.path, change.keyword or ""))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|