Skip to content

Commit 5e83a2b

Browse files
committed
rename merge_extra_properties to merge_props
Add new replace_refs kwargs to load utility functions
1 parent 729be4e commit 5e83a2b

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

jsonref.py

+64-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from proxytypes import LazyProxy
1919

20-
__version__ = "1.0.0b2"
20+
__version__ = "1.0.0b3"
2121

2222

2323
class JsonRefError(Exception):
@@ -87,7 +87,7 @@ def __init__(
8787
loader=None,
8888
jsonschema=False,
8989
load_on_repr=True,
90-
merge_extra_properties=False,
90+
merge_props=False,
9191
_path=(),
9292
_store=None,
9393
):
@@ -98,7 +98,7 @@ def __init__(
9898
self.loader = loader or jsonloader
9999
self.jsonschema = jsonschema
100100
self.load_on_repr = load_on_repr
101-
self.merge_extra_properties = merge_extra_properties
101+
self.merge_props = merge_props
102102
self.path = _path
103103
self.store = _store # Use the same object to be shared with children
104104
if self.store is None:
@@ -111,7 +111,7 @@ def _ref_kwargs(self):
111111
loader=self.loader,
112112
jsonschema=self.jsonschema,
113113
load_on_repr=self.load_on_repr,
114-
merge_extra_properties=self.merge_extra_properties,
114+
merge_props=self.merge_props,
115115
path=self.path,
116116
store=self.store,
117117
)
@@ -145,7 +145,7 @@ def callback(self):
145145
if hasattr(result, "__subject__"):
146146
result = result.__subject__
147147
if (
148-
self.merge_extra_properties
148+
self.merge_props
149149
and isinstance(result, Mapping)
150150
and len(self.__reference__) > 1
151151
):
@@ -278,13 +278,13 @@ def walk_refs(obj, func, replace=False):
278278

279279
def replace_refs(
280280
obj,
281-
proxies=True,
282-
lazy_load=True,
283281
base_uri="",
284282
loader=jsonloader,
285283
jsonschema=False,
286284
load_on_repr=True,
287-
merge_extra_properties=False,
285+
merge_props=False,
286+
proxies=True,
287+
lazy_load=True,
288288
):
289289
"""
290290
Returns a deep copy of `obj` with all contained JSON reference objects
@@ -294,12 +294,6 @@ def replace_refs(
294294
instance will be created. If `obj` is not a JSON reference object,
295295
a deep copy of it will be created with all contained JSON
296296
reference objects replaced by :class:`JsonRef` instances
297-
:param proxies: If `True`, references will be replaced with transparent
298-
proxy objects. Otherwise, they will be replaced directly with the
299-
referred data. (defaults to ``True``)
300-
:param lazy_load: When proxy objects are used, and this is `True`, the
301-
references will not be resolved until that section of the JSON
302-
document is accessed. (defaults to ``True``)
303297
:param base_uri: URI to resolve relative references against
304298
:param loader: Callable that takes a URI and returns the parsed JSON
305299
(defaults to global ``jsonloader``, a :class:`JsonLoader` instance)
@@ -310,11 +304,17 @@ def replace_refs(
310304
:param load_on_repr: If set to ``False``, :func:`repr` call on a
311305
:class:`JsonRef` object will not cause the reference to be loaded
312306
if it hasn't already. (defaults to ``True``)
313-
:param merge_extra_properties: When ``True``, JSON reference objects that
307+
:param merge_props: When ``True``, JSON reference objects that
314308
have extra keys other than '$ref' in them will be merged into the
315309
document resolved by the reference (if it is a dictionary.) NOTE: This
316310
is not part of the JSON Reference spec, and may not behave the same as
317311
other libraries.
312+
:param proxies: If `True`, references will be replaced with transparent
313+
proxy objects. Otherwise, they will be replaced directly with the
314+
referred data. (defaults to ``True``)
315+
:param lazy_load: When proxy objects are used, and this is `True`, the
316+
references will not be resolved until that section of the JSON
317+
document is accessed. (defaults to ``True``)
318318
319319
"""
320320
result = _replace_refs(
@@ -323,7 +323,7 @@ def replace_refs(
323323
loader=loader,
324324
jsonschema=jsonschema,
325325
load_on_repr=load_on_repr,
326-
merge_extra_properties=merge_extra_properties,
326+
merge_props=merge_props,
327327
store=URIDict(),
328328
path=(),
329329
recursing=False,
@@ -342,7 +342,7 @@ def _replace_refs(
342342
loader,
343343
jsonschema,
344344
load_on_repr,
345-
merge_extra_properties,
345+
merge_props,
346346
store,
347347
path,
348348
recursing
@@ -370,7 +370,7 @@ def _replace_refs(
370370
loader=loader,
371371
jsonschema=jsonschema,
372372
load_on_repr=load_on_repr,
373-
merge_extra_properties=merge_extra_properties,
373+
merge_props=merge_props,
374374
_path=path,
375375
_store=store,
376376
)
@@ -385,7 +385,7 @@ def _replace_refs(
385385
loader=loader,
386386
jsonschema=jsonschema,
387387
load_on_repr=load_on_repr,
388-
merge_extra_properties=merge_extra_properties,
388+
merge_props=merge_props,
389389
store=store,
390390
path=path + (k,),
391391
recursing=True,
@@ -400,7 +400,7 @@ def _replace_refs(
400400
loader=loader,
401401
jsonschema=jsonschema,
402402
load_on_repr=load_on_repr,
403-
merge_extra_properties=merge_extra_properties,
403+
merge_props=merge_props,
404404
store=store,
405405
path=path + (i,),
406406
recursing=True,
@@ -412,14 +412,24 @@ def _replace_refs(
412412
return obj
413413

414414

415-
def load(fp, base_uri="", loader=None, jsonschema=False, load_on_repr=True, **kwargs):
415+
def load(
416+
fp,
417+
base_uri="",
418+
loader=None,
419+
jsonschema=False,
420+
load_on_repr=True,
421+
merge_props=False,
422+
proxies=True,
423+
lazy_load=True,
424+
**kwargs
425+
):
416426
"""
417427
Drop in replacement for :func:`json.load`, where JSON references are
418428
proxied to their referent data.
419429
420430
:param fp: File-like object containing JSON document
421431
:param kwargs: This function takes any of the keyword arguments from
422-
:meth:`JsonRef.replace_refs`. Any other keyword arguments will be passed to
432+
:func:`replace_refs`. Any other keyword arguments will be passed to
423433
:func:`json.load`
424434
425435
"""
@@ -433,17 +443,30 @@ def load(fp, base_uri="", loader=None, jsonschema=False, load_on_repr=True, **kw
433443
loader=loader,
434444
jsonschema=jsonschema,
435445
load_on_repr=load_on_repr,
446+
merge_props=merge_props,
447+
proxies=proxies,
448+
lazy_load=lazy_load,
436449
)
437450

438451

439-
def loads(s, base_uri="", loader=None, jsonschema=False, load_on_repr=True, **kwargs):
452+
def loads(
453+
s,
454+
base_uri="",
455+
loader=None,
456+
jsonschema=False,
457+
load_on_repr=True,
458+
merge_props=False,
459+
proxies=True,
460+
lazy_load=True,
461+
**kwargs
462+
):
440463
"""
441464
Drop in replacement for :func:`json.loads`, where JSON references are
442465
proxied to their referent data.
443466
444467
:param s: String containing JSON document
445468
:param kwargs: This function takes any of the keyword arguments from
446-
:meth:`JsonRef.replace_refs`. Any other keyword arguments will be passed to
469+
:func:`replace_refs`. Any other keyword arguments will be passed to
447470
:func:`json.loads`
448471
449472
"""
@@ -457,17 +480,29 @@ def loads(s, base_uri="", loader=None, jsonschema=False, load_on_repr=True, **kw
457480
loader=loader,
458481
jsonschema=jsonschema,
459482
load_on_repr=load_on_repr,
483+
merge_props=merge_props,
484+
proxies=proxies,
485+
lazy_load=lazy_load,
460486
)
461487

462488

463-
def load_uri(uri, base_uri=None, loader=None, jsonschema=False, load_on_repr=True):
489+
def load_uri(
490+
uri,
491+
base_uri=None,
492+
loader=None,
493+
jsonschema=False,
494+
load_on_repr=True,
495+
merge_props=False,
496+
proxies=True,
497+
lazy_load=True,
498+
):
464499
"""
465500
Load JSON data from ``uri`` with JSON references proxied to their referent
466501
data.
467502
468503
:param uri: URI to fetch the JSON from
469504
:param kwargs: This function takes any of the keyword arguments from
470-
:meth:`JsonRef.replace_refs`
505+
:func:`replace_refs`
471506
472507
"""
473508

@@ -482,6 +517,9 @@ def load_uri(uri, base_uri=None, loader=None, jsonschema=False, load_on_repr=Tru
482517
loader=loader,
483518
jsonschema=jsonschema,
484519
load_on_repr=load_on_repr,
520+
merge_props=merge_props,
521+
proxies=proxies,
522+
lazy_load=lazy_load,
485523
)
486524

487525

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jsonref"
3-
version = "1.0.0b2"
3+
version = "1.0.0b3"
44
description = "jsonref is a library for automatic dereferencing of JSON Reference objects for Python."
55
authors = ["Chase Sterling <[email protected]>"]
66
license = "MIT"

tests.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def test_merge_extra_flag(self, parametrized_replace_refs):
8787
"a": {"main": 1},
8888
"b": {"$ref": "#/a", "extra": 2},
8989
}
90-
no_extra = parametrized_replace_refs(json, merge_extra_properties=False)
90+
no_extra = parametrized_replace_refs(json, merge_props=False)
9191
assert no_extra == {"a": {"main": 1}, "b": {"main": 1}}
92-
extra = parametrized_replace_refs(json, merge_extra_properties=True)
92+
extra = parametrized_replace_refs(json, merge_props=True)
9393
assert extra == {"a": {"main": 1}, "b": {"main": 1, "extra": 2}}
9494

9595
def test_extra_ref_attributes(self, parametrized_replace_refs):
@@ -98,9 +98,7 @@ def test_extra_ref_attributes(self, parametrized_replace_refs):
9898
"b": {"extra": "foobar", "$ref": "#/a"},
9999
"c": {"extra": {"more": "bar", "$ref": "#/a"}},
100100
}
101-
result = parametrized_replace_refs(
102-
json, load_on_repr=False, merge_extra_properties=True
103-
)
101+
result = parametrized_replace_refs(json, load_on_repr=False, merge_props=True)
104102
assert result["b"] == {
105103
"extra": "foobar",
106104
"type": "object",
@@ -116,7 +114,7 @@ def test_extra_ref_attributes(self, parametrized_replace_refs):
116114

117115
def test_recursive_extra(self, parametrized_replace_refs):
118116
json = {"a": {"$ref": "#", "extra": "foo"}}
119-
result = parametrized_replace_refs(json, merge_extra_properties=True)
117+
result = parametrized_replace_refs(json, merge_props=True)
120118
assert result["a"]["a"]["extra"] == "foo"
121119
assert result["a"]["a"] is result["a"]["a"]["a"]
122120

@@ -125,7 +123,7 @@ def test_extra_sibling_attributes_list_ref(self, parametrized_replace_refs):
125123
"a": ["target"],
126124
"b": {"extra": "foobar", "$ref": "#/a"},
127125
}
128-
result = parametrized_replace_refs(json, merge_extra_properties=True)
126+
result = parametrized_replace_refs(json, merge_props=True)
129127
assert result["b"] == result["a"]
130128

131129
def test_separate_extras(self, parametrized_replace_refs):
@@ -135,7 +133,7 @@ def test_separate_extras(self, parametrized_replace_refs):
135133
"y": {"$ref": "#/a", "extray": "y"},
136134
"z": {"$ref": "#/y", "extraz": "z"},
137135
}
138-
result = parametrized_replace_refs(json, merge_extra_properties=True)
136+
result = parametrized_replace_refs(json, merge_props=True)
139137
assert result == {
140138
"a": {"main": 1234},
141139
"x": {"main": 1234, "extrax": "x"},

0 commit comments

Comments
 (0)