Skip to content

[3.12] Revert "GH-96145: Add AttrDict to JSON module for use with object_hook (GH-96146)" (GH-105948) #106117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

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

.. testsetup:: *

import json
from json import AttrDict

--------------

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

.. versionadded:: 3.5

.. class:: AttrDict(**kwargs)
AttrDict(mapping, **kwargs)
AttrDict(iterable, **kwargs)

Subclass of :class:`dict` that also supports attribute style dotted access.

This class is intended for use with the :attr:`object_hook` in
:func:`json.load` and :func:`json.loads`:

.. doctest::

>>> json_string = '{"mercury": 88, "venus": 225, "earth": 365, "mars": 687}'
>>> orbital_period = json.loads(json_string, object_hook=AttrDict)
>>> orbital_period['earth'] # Dict style lookup
365
>>> orbital_period.earth # Attribute style lookup
365
>>> orbital_period.keys() # All dict methods are present
dict_keys(['mercury', 'venus', 'earth', 'mars'])

Attribute style access only works for keys that are valid attribute
names. In contrast, dictionary style access works for all keys. For
example, ``d.two words`` contains a space and is not syntactically
valid Python, so ``d["two words"]`` should be used instead.

If a key has the same name as a dictionary method, then a dictionary
lookup finds the key and an attribute lookup finds the method:

.. doctest::

>>> d = AttrDict(items=50)
>>> d['items'] # Lookup the key
50
>>> d.items() # Call the method
dict_items([('items', 50)])

.. versionadded:: 3.12


Standard Compliance and Interoperability
----------------------------------------
Expand Down
8 changes: 0 additions & 8 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,6 @@ itertools
tuples where the last batch may be shorter than the rest.
(Contributed by Raymond Hettinger in :gh:`98363`.)

json
----

* Added :class:`json.AttrDict` for use with ``object_hook`` in :func:`json.load`
or :func:`json.loads`. This is a subclass of :class:`dict` that also supports
attribute style dotted access.
(Contributed by Raymond Hettinger in :gh:`96145`.)

math
----

Expand Down
52 changes: 1 addition & 51 deletions Lib/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"""
__version__ = '2.0.9'
__all__ = [
'dump', 'dumps', 'load', 'loads', 'AttrDict',
'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
]

Expand Down Expand Up @@ -357,53 +357,3 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
if parse_constant is not None:
kw['parse_constant'] = parse_constant
return cls(**kw).decode(s)

class AttrDict(dict):
"""Dict like object that supports attribute style dotted access.

This class is intended for use with the *object_hook* in json.loads():

>>> from json import loads, AttrDict
>>> json_string = '{"mercury": 88, "venus": 225, "earth": 365, "mars": 687}'
>>> orbital_period = loads(json_string, object_hook=AttrDict)
>>> orbital_period['earth'] # Dict style lookup
365
>>> orbital_period.earth # Attribute style lookup
365
>>> orbital_period.keys() # All dict methods are present
dict_keys(['mercury', 'venus', 'earth', 'mars'])

Attribute style access only works for keys that are valid attribute names.
In contrast, dictionary style access works for all keys.
For example, ``d.two words`` contains a space and is not syntactically
valid Python, so ``d["two words"]`` should be used instead.

If a key has the same name as dictionary method, then a dictionary
lookup finds the key and an attribute lookup finds the method:

>>> d = AttrDict(items=50)
>>> d['items'] # Lookup the key
50
>>> d.items() # Call the method
dict_items([('items', 50)])

"""
__slots__ = ()

def __getattr__(self, attr):
try:
return self[attr]
except KeyError:
raise AttributeError(attr) from None

def __setattr__(self, attr, value):
self[attr] = value

def __delattr__(self, attr):
try:
del self[attr]
except KeyError:
raise AttributeError(attr) from None

def __dir__(self):
return list(self) + dir(type(self))
1 change: 0 additions & 1 deletion Lib/test/test_json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PyTest(unittest.TestCase):
json = pyjson
loads = staticmethod(pyjson.loads)
dumps = staticmethod(pyjson.dumps)
AttrDict = pyjson.AttrDict
JSONDecodeError = staticmethod(pyjson.JSONDecodeError)

@unittest.skipUnless(cjson, 'requires _json')
Expand Down
145 changes: 0 additions & 145 deletions Lib/test/test_json/test_attrdict.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reverted addition of ``json.AttrDict``.