Skip to content

Commit 6634971

Browse files
committed
Use requests.compat.json instead of plain json.
The `requests` package kindly manages 2/3 compat for json for us, might as well be consistent and use the same tool! As a side note, I’d like to move away from `six` and depend directly on `requests.compat`. I believe it has everything we need and then we can ditch the `six` dep and know that we’re always in sync with whatever requests is doing (which is really what we care about).
1 parent 4304f0e commit 6634971

File tree

14 files changed

+84
-74
lines changed

14 files changed

+84
-74
lines changed

Diff for: plotly/exceptions.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
A module that contains plotly's exception hierarchy.
66
77
"""
8-
import json
8+
from __future__ import absolute_import
9+
10+
from plotly.api.utils import to_native_utf8_string
911

1012

1113
# Base Plotly Error
@@ -21,7 +23,7 @@ class PlotlyRequestError(PlotlyError):
2123
"""General API error. Raised for *all* failed requests."""
2224

2325
def __init__(self, message, status_code, content):
24-
self.message = message
26+
self.message = to_native_utf8_string(message)
2527
self.status_code = status_code
2628
self.content = content
2729

Diff for: plotly/grid_objs/grid_objs.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"""
66
from __future__ import absolute_import
77

8-
import json
98
from collections import MutableSequence
109

10+
from requests.compat import json as _json
11+
1112
from plotly import exceptions, utils
1213

1314
__all__ = None
@@ -66,7 +67,7 @@ def __init__(self, data, name):
6667

6768
def __str__(self):
6869
max_chars = 10
69-
jdata = json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
70+
jdata = _json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
7071
if len(jdata) > max_chars:
7172
data_string = jdata[:max_chars] + "...]"
7273
else:

Diff for: plotly/offline/offline.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
"""
66
from __future__ import absolute_import
77

8-
import json
98
import os
109
import uuid
1110
import warnings
1211
from pkg_resources import resource_string
1312
import time
1413
import webbrowser
1514

15+
from requests.compat import json as _json
16+
1617
import plotly
1718
from plotly import tools, utils
1819
from plotly.exceptions import PlotlyError
@@ -183,10 +184,12 @@ def _plot_html(figure_or_data, config, validate, default_width,
183184
height = str(height) + 'px'
184185

185186
plotdivid = uuid.uuid4()
186-
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
187-
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
187+
jdata = _json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
188+
jlayout = _json.dumps(figure.get('layout', {}),
189+
cls=utils.PlotlyJSONEncoder)
188190
if 'frames' in figure_or_data:
189-
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder)
191+
jframes = _json.dumps(figure.get('frames', {}),
192+
cls=utils.PlotlyJSONEncoder)
190193

191194
configkeys = (
192195
'editable',
@@ -211,7 +214,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
211214
)
212215

213216
config_clean = dict((k, config[k]) for k in configkeys if k in config)
214-
jconfig = json.dumps(config_clean)
217+
jconfig = _json.dumps(config_clean)
215218

216219
# TODO: The get_config 'source of truth' should
217220
# really be somewhere other than plotly.plotly

Diff for: plotly/plotly/plotly.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
from __future__ import absolute_import
1818

1919
import copy
20-
import json
2120
import os
2221
import warnings
2322

2423
import six
2524
import six.moves
25+
from requests.compat import json as _json
2626

2727
from plotly import exceptions, tools, utils, files
2828
from plotly.api import v1, v2
@@ -642,7 +642,7 @@ def write(self, trace, layout=None, validate=True,
642642
stream_object.update(dict(layout=layout))
643643

644644
# TODO: allow string version of this?
645-
jdata = json.dumps(stream_object, cls=utils.PlotlyJSONEncoder)
645+
jdata = _json.dumps(stream_object, cls=utils.PlotlyJSONEncoder)
646646
jdata += "\n"
647647

648648
try:
@@ -1056,7 +1056,7 @@ def append_columns(cls, columns, grid=None, grid_url=None):
10561056

10571057
# This is sorta gross, we need to double-encode this.
10581058
body = {
1059-
'cols': json.dumps(columns, cls=utils.PlotlyJSONEncoder)
1059+
'cols': _json.dumps(columns, cls=utils.PlotlyJSONEncoder)
10601060
}
10611061
fid = grid_id
10621062
response = v2.grids.col_create(fid, body)

Diff for: plotly/tests/test_core/test_get_requests/test_get_requests.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
"""
88
import copy
9-
import json
10-
import requests
119

10+
import requests
1211
import six
1312
from nose.plugins.attrib import attr
13+
from requests.compat import json as _json
1414

1515

1616
default_headers = {'plotly-username': '',
@@ -37,9 +37,9 @@ def test_user_does_not_exist():
3737
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
3838
response = requests.get(server + resource, headers=hd)
3939
if six.PY3:
40-
content = json.loads(response.content.decode('unicode_escape'))
40+
content = _json.loads(response.content.decode('unicode_escape'))
4141
else:
42-
content = json.loads(response.content)
42+
content = _json.loads(response.content)
4343
print(response.status_code)
4444
print(content)
4545
assert response.status_code == 404
@@ -60,9 +60,9 @@ def test_file_does_not_exist():
6060
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
6161
response = requests.get(server + resource, headers=hd)
6262
if six.PY3:
63-
content = json.loads(response.content.decode('unicode_escape'))
63+
content = _json.loads(response.content.decode('unicode_escape'))
6464
else:
65-
content = json.loads(response.content)
65+
content = _json.loads(response.content)
6666
print(response.status_code)
6767
print(content)
6868
assert response.status_code == 404
@@ -100,9 +100,9 @@ def test_private_permission_defined():
100100
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
101101
response = requests.get(server + resource, headers=hd)
102102
if six.PY3:
103-
content = json.loads(response.content.decode('unicode_escape'))
103+
content = _json.loads(response.content.decode('unicode_escape'))
104104
else:
105-
content = json.loads(response.content)
105+
content = _json.loads(response.content)
106106
print(response.status_code)
107107
print(content)
108108
assert response.status_code == 403
@@ -122,9 +122,9 @@ def test_missing_headers():
122122
del hd[header]
123123
response = requests.get(server + resource, headers=hd)
124124
if six.PY3:
125-
content = json.loads(response.content.decode('unicode_escape'))
125+
content = _json.loads(response.content.decode('unicode_escape'))
126126
else:
127-
content = json.loads(response.content)
127+
content = _json.loads(response.content)
128128
print(response.status_code)
129129
print(content)
130130
assert response.status_code == 422
@@ -142,13 +142,13 @@ def test_valid_request():
142142
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
143143
response = requests.get(server + resource, headers=hd)
144144
if six.PY3:
145-
content = json.loads(response.content.decode('unicode_escape'))
145+
content = _json.loads(response.content.decode('unicode_escape'))
146146
else:
147-
content = json.loads(response.content)
147+
content = _json.loads(response.content)
148148
print(response.status_code)
149149
print(content)
150150
assert response.status_code == 200
151-
# content = json.loads(res.content)
151+
# content = _json.loads(res.content)
152152
# response_payload = content['payload']
153153
# figure = response_payload['figure']
154154
# if figure['data'][0]['x'] != [u'1', u'2', u'3']:

Diff for: plotly/tests/test_core/test_graph_reference/test_graph_reference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"""
55
from __future__ import absolute_import
66

7-
import json
87
import os
98
from pkg_resources import resource_string
109
from unittest import TestCase
1110

1211
from nose.plugins.attrib import attr
12+
from requests.compat import json as _json
1313

1414
from plotly import graph_reference as gr
1515
from plotly.api import v2
@@ -28,7 +28,7 @@ def test_default_schema_is_up_to_date(self):
2828

2929
path = os.path.join('package_data', 'default-schema.json')
3030
s = resource_string('plotly', path).decode('utf-8')
31-
default_schema = json.loads(s)
31+
default_schema = _json.loads(s)
3232

3333
msg = (
3434
'The default, hard-coded plot schema we ship with pip is out of '

Diff for: plotly/tests/test_core/test_offline/test_offline.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""
55
from __future__ import absolute_import
66

7-
from nose.tools import raises
87
from unittest import TestCase
9-
from plotly.tests.utils import PlotlyTestCase
10-
import json
8+
9+
from requests.compat import json as _json
1110

1211
import plotly
12+
from plotly.tests.utils import PlotlyTestCase
13+
1314

1415
fig = {
1516
'data': [
@@ -35,8 +36,9 @@ def _read_html(self, file_url):
3536
return f.read()
3637

3738
def test_default_plot_generates_expected_html(self):
38-
data_json = json.dumps(fig['data'], cls=plotly.utils.PlotlyJSONEncoder)
39-
layout_json = json.dumps(
39+
data_json = _json.dumps(fig['data'],
40+
cls=plotly.utils.PlotlyJSONEncoder)
41+
layout_json = _json.dumps(
4042
fig['layout'],
4143
cls=plotly.utils.PlotlyJSONEncoder)
4244

Diff for: plotly/tests/test_core/test_plotly/test_plot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"""
88
from __future__ import absolute_import
99

10-
import json
1110
import requests
1211
import six
12+
from requests.compat import json as _json
1313

1414
from unittest import TestCase
1515
from nose.plugins.attrib import attr
@@ -296,10 +296,10 @@ def generate_conflicting_plot_options_with_json_writes_of_config():
296296
"""
297297
def gen_test(plot_options):
298298
def test(self):
299-
config = json.load(open(CONFIG_FILE))
299+
config = _json.load(open(CONFIG_FILE))
300300
with open(CONFIG_FILE, 'w') as f:
301301
config.update(plot_options)
302-
f.write(json.dumps(config))
302+
f.write(_json.dumps(config))
303303
self.assertRaises(PlotlyError, py._plot_option_logic, {})
304304
return test
305305

Diff for: plotly/tests/test_core/test_utils/test_utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import absolute_import
22

3-
import json
43
from unittest import TestCase
54

5+
from requests.compat import json as _json
6+
67
from plotly.utils import PlotlyJSONEncoder, get_by_path, node_generator
78

89

910
class TestJSONEncoder(TestCase):
1011

1112
def test_nan_to_null(self):
1213
array = [1, float('NaN'), float('Inf'), float('-Inf'), 'platypus']
13-
result = json.dumps(array, cls=PlotlyJSONEncoder)
14+
result = _json.dumps(array, cls=PlotlyJSONEncoder)
1415
expected_result = '[1, null, null, null, "platypus"]'
1516
self.assertEqual(result, expected_result)
1617

Diff for: plotly/tests/test_optional/test_offline/test_offline.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from nose.tools import raises
88
from nose.plugins.attrib import attr
9+
from requests.compat import json as _json
910

1011
from unittest import TestCase
11-
import json
1212

1313
import plotly
1414

@@ -75,8 +75,8 @@ def test_default_mpl_plot_generates_expected_html(self):
7575
figure = plotly.tools.mpl_to_plotly(fig)
7676
data = figure['data']
7777
layout = figure['layout']
78-
data_json = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
79-
layout_json = json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
78+
data_json = _json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
79+
layout_json = _json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
8080
html = self._read_html(plotly.offline.plot_mpl(fig))
8181

8282
# just make sure a few of the parts are in here

0 commit comments

Comments
 (0)