Skip to content

Commit d3af83b

Browse files
authored
Revert "GH-96145: Add AttrDict to JSON module for use with object_hook (#96146)" (#105948)
This reverts commit 1f0eafa.
1 parent 512f299 commit d3af83b

File tree

6 files changed

+2
-248
lines changed

6 files changed

+2
-248
lines changed

Doc/library/json.rst

-43
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
**Source code:** :source:`Lib/json/__init__.py`
1111

12-
.. testsetup:: *
13-
14-
import json
15-
from json import AttrDict
16-
1712
--------------
1813

1914
`JSON (JavaScript Object Notation) <https://json.org>`_, specified by
@@ -548,44 +543,6 @@ Exceptions
548543

549544
.. versionadded:: 3.5
550545

551-
.. class:: AttrDict(**kwargs)
552-
AttrDict(mapping, **kwargs)
553-
AttrDict(iterable, **kwargs)
554-
555-
Subclass of :class:`dict` that also supports attribute style dotted access.
556-
557-
This class is intended for use with the :attr:`object_hook` in
558-
:func:`json.load` and :func:`json.loads`:
559-
560-
.. doctest::
561-
562-
>>> json_string = '{"mercury": 88, "venus": 225, "earth": 365, "mars": 687}'
563-
>>> orbital_period = json.loads(json_string, object_hook=AttrDict)
564-
>>> orbital_period['earth'] # Dict style lookup
565-
365
566-
>>> orbital_period.earth # Attribute style lookup
567-
365
568-
>>> orbital_period.keys() # All dict methods are present
569-
dict_keys(['mercury', 'venus', 'earth', 'mars'])
570-
571-
Attribute style access only works for keys that are valid attribute
572-
names. In contrast, dictionary style access works for all keys. For
573-
example, ``d.two words`` contains a space and is not syntactically
574-
valid Python, so ``d["two words"]`` should be used instead.
575-
576-
If a key has the same name as a dictionary method, then a dictionary
577-
lookup finds the key and an attribute lookup finds the method:
578-
579-
.. doctest::
580-
581-
>>> d = AttrDict(items=50)
582-
>>> d['items'] # Lookup the key
583-
50
584-
>>> d.items() # Call the method
585-
dict_items([('items', 50)])
586-
587-
.. versionadded:: 3.12
588-
589546

590547
Standard Compliance and Interoperability
591548
----------------------------------------

Doc/whatsnew/3.12.rst

-8
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,6 @@ itertools
593593
tuples where the last batch may be shorter than the rest.
594594
(Contributed by Raymond Hettinger in :gh:`98363`.)
595595

596-
json
597-
----
598-
599-
* Added :class:`json.AttrDict` for use with ``object_hook`` in :func:`json.load`
600-
or :func:`json.loads`. This is a subclass of :class:`dict` that also supports
601-
attribute style dotted access.
602-
(Contributed by Raymond Hettinger in :gh:`96145`.)
603-
604596
math
605597
----
606598

Lib/json/__init__.py

+1-51
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"""
9898
__version__ = '2.0.9'
9999
__all__ = [
100-
'dump', 'dumps', 'load', 'loads', 'AttrDict',
100+
'dump', 'dumps', 'load', 'loads',
101101
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
102102
]
103103

@@ -357,53 +357,3 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
357357
if parse_constant is not None:
358358
kw['parse_constant'] = parse_constant
359359
return cls(**kw).decode(s)
360-
361-
class AttrDict(dict):
362-
"""Dict like object that supports attribute style dotted access.
363-
364-
This class is intended for use with the *object_hook* in json.loads():
365-
366-
>>> from json import loads, AttrDict
367-
>>> json_string = '{"mercury": 88, "venus": 225, "earth": 365, "mars": 687}'
368-
>>> orbital_period = loads(json_string, object_hook=AttrDict)
369-
>>> orbital_period['earth'] # Dict style lookup
370-
365
371-
>>> orbital_period.earth # Attribute style lookup
372-
365
373-
>>> orbital_period.keys() # All dict methods are present
374-
dict_keys(['mercury', 'venus', 'earth', 'mars'])
375-
376-
Attribute style access only works for keys that are valid attribute names.
377-
In contrast, dictionary style access works for all keys.
378-
For example, ``d.two words`` contains a space and is not syntactically
379-
valid Python, so ``d["two words"]`` should be used instead.
380-
381-
If a key has the same name as dictionary method, then a dictionary
382-
lookup finds the key and an attribute lookup finds the method:
383-
384-
>>> d = AttrDict(items=50)
385-
>>> d['items'] # Lookup the key
386-
50
387-
>>> d.items() # Call the method
388-
dict_items([('items', 50)])
389-
390-
"""
391-
__slots__ = ()
392-
393-
def __getattr__(self, attr):
394-
try:
395-
return self[attr]
396-
except KeyError:
397-
raise AttributeError(attr) from None
398-
399-
def __setattr__(self, attr, value):
400-
self[attr] = value
401-
402-
def __delattr__(self, attr):
403-
try:
404-
del self[attr]
405-
except KeyError:
406-
raise AttributeError(attr) from None
407-
408-
def __dir__(self):
409-
return list(self) + dir(type(self))

Lib/test/test_json/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class PyTest(unittest.TestCase):
1818
json = pyjson
1919
loads = staticmethod(pyjson.loads)
2020
dumps = staticmethod(pyjson.dumps)
21-
AttrDict = pyjson.AttrDict
2221
JSONDecodeError = staticmethod(pyjson.JSONDecodeError)
2322

2423
@unittest.skipUnless(cjson, 'requires _json')

Lib/test/test_json/test_attrdict.py

-145
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reverted addition of ``json.AttrDict``.

0 commit comments

Comments
 (0)