Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aacd53f

Browse files
Issue #18726: All optional parameters of the dump(), dumps(),
load() and loads() functions and JSONEncoder and JSONDecoder class constructors in the json module are now keyword-only.
1 parent 4335437 commit aacd53f

6 files changed

Lines changed: 36 additions & 12 deletions

File tree

Doc/library/json.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ See :ref:`json-commandline` for detailed documentation.
126126
Basic Usage
127127
-----------
128128

129-
.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
129+
.. function:: dump(obj, fp, *, skipkeys=False, ensure_ascii=True, \
130130
check_circular=True, allow_nan=True, cls=None, \
131131
indent=None, separators=None, default=None, \
132132
sort_keys=False, **kw)
@@ -184,8 +184,11 @@ Basic Usage
184184
:meth:`default` method to serialize additional types), specify it with the
185185
*cls* kwarg; otherwise :class:`JSONEncoder` is used.
186186

187+
.. versionchanged:: 3.6
188+
All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
187189

188-
.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
190+
191+
.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
189192
check_circular=True, allow_nan=True, cls=None, \
190193
indent=None, separators=None, default=None, \
191194
sort_keys=False, **kw)
@@ -209,7 +212,7 @@ Basic Usage
209212
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
210213
keys.
211214

212-
.. function:: load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
215+
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
213216

214217
Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
215218
containing a JSON document) to a Python object using this :ref:`conversion
@@ -257,7 +260,10 @@ Basic Usage
257260
If the data being deserialized is not a valid JSON document, a
258261
:exc:`JSONDecodeError` will be raised.
259262

260-
.. function:: loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
263+
.. versionchanged:: 3.6
264+
All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
265+
266+
.. function:: loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
261267

262268
Deserialize *s* (a :class:`str` instance containing a JSON document) to a
263269
Python object using this :ref:`conversion table <json-to-py-table>`.
@@ -271,7 +277,7 @@ Basic Usage
271277
Encoders and Decoders
272278
---------------------
273279

274-
.. class:: JSONDecoder(object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
280+
.. class:: JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
275281

276282
Simple JSON decoder.
277283

@@ -341,6 +347,9 @@ Encoders and Decoders
341347
If the data being deserialized is not a valid JSON document, a
342348
:exc:`JSONDecodeError` will be raised.
343349

350+
.. versionchanged:: 3.6
351+
All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
352+
344353
.. method:: decode(s)
345354

346355
Return the Python representation of *s* (a :class:`str` instance
@@ -359,7 +368,7 @@ Encoders and Decoders
359368
extraneous data at the end.
360369

361370

362-
.. class:: JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
371+
.. class:: JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
363372

364373
Extensible JSON encoder for Python data structures.
365374

@@ -438,6 +447,9 @@ Encoders and Decoders
438447
otherwise be serialized. It should return a JSON encodable version of the
439448
object or raise a :exc:`TypeError`.
440449

450+
.. versionchanged:: 3.6
451+
All parameters are now :ref:`keyword-only <keyword-only_parameter>`.
452+
441453

442454
.. method:: default(o)
443455

Doc/whatsnew/3.6.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,14 @@ Changes in the Python API
685685
Code that has already been updated in accordance with the deprecation
686686
warning generated by 3.5 will not be affected.
687687

688+
* All optional parameters of the :func:`~json.dump`, :func:`~json.dumps`,
689+
:func:`~json.load` and :func:`~json.loads` functions and
690+
:class:`~json.JSONEncoder` and :class:`~json.JSONDecoder` class
691+
constructors in the :mod:`json` module are now :ref:`keyword-only
692+
<keyword-only_parameter>`.
693+
(Contributed by Serhiy Storchaka in :issue:`18726`.)
694+
695+
688696
Changes in the C API
689697
--------------------
690698

Lib/json/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
default=None,
117117
)
118118

119-
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
119+
def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
120120
allow_nan=True, cls=None, indent=None, separators=None,
121121
default=None, sort_keys=False, **kw):
122122
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
@@ -179,7 +179,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
179179
fp.write(chunk)
180180

181181

182-
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
182+
def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
183183
allow_nan=True, cls=None, indent=None, separators=None,
184184
default=None, sort_keys=False, **kw):
185185
"""Serialize ``obj`` to a JSON formatted ``str``.
@@ -240,7 +240,7 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
240240
_default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
241241

242242

243-
def load(fp, cls=None, object_hook=None, parse_float=None,
243+
def load(fp, *, cls=None, object_hook=None, parse_float=None,
244244
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
245245
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
246246
a JSON document) to a Python object.
@@ -268,7 +268,7 @@ def load(fp, cls=None, object_hook=None, parse_float=None,
268268
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
269269

270270

271-
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
271+
def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
272272
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
273273
"""Deserialize ``s`` (a ``str`` instance containing a JSON
274274
document) to a Python object.

Lib/json/decoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class JSONDecoder(object):
280280
281281
"""
282282

283-
def __init__(self, object_hook=None, parse_float=None,
283+
def __init__(self, *, object_hook=None, parse_float=None,
284284
parse_int=None, parse_constant=None, strict=True,
285285
object_pairs_hook=None):
286286
"""``object_hook``, if specified, will be called with the result

Lib/json/encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class JSONEncoder(object):
101101
"""
102102
item_separator = ', '
103103
key_separator = ': '
104-
def __init__(self, skipkeys=False, ensure_ascii=True,
104+
def __init__(self, *, skipkeys=False, ensure_ascii=True,
105105
check_circular=True, allow_nan=True, sort_keys=False,
106106
indent=None, separators=None, default=None):
107107
"""Constructor for JSONEncoder, with sensible defaults.

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 alpha 3
1010
Library
1111
-------
1212

13+
- Issue #18726: All optional parameters of the dump(), dumps(),
14+
load() and loads() functions and JSONEncoder and JSONDecoder class
15+
constructors in the json module are now keyword-only.
16+
1317
- Issue #27319: Methods selection_set(), selection_add(), selection_remove()
1418
and selection_toggle() of ttk.TreeView now allow passing multiple items as
1519
multiple arguments instead of passing them as a tuple. Deprecated

0 commit comments

Comments
 (0)