Skip to content

Commit e96c2d0

Browse files
authored
Merge pull request #203 from scipy/dicts_numeric
ENH: make dicts with string keys use numeric comparisons
2 parents 1a0aa54 + 9fe97c9 commit e96c2d0

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

scipy_doctest/impl.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,13 @@ def check_output(self, want, got, optionflags):
422422
return False
423423

424424
if want_is_dict:
425-
# convert dicts into lists of key-value pairs and retry
426-
want_items = str(list(a_want.items()))
427-
got_items = str(list(a_got.items()))
428-
return self.check_output(want_items, got_items, optionflags)
425+
# split each dict into a pair of lists of keys and values, and retry
426+
want_keys, want_values = f"{list(a_want.keys())}", f"{list(a_want.values())}"
427+
got_keys, got_values = f"{list(a_got.keys())}", f"{list(a_got.values())}"
428+
429+
keys_match = self.check_output(want_keys, got_keys, optionflags)
430+
values_match = self.check_output(want_values, got_values, optionflags)
431+
return keys_match and values_match
429432

430433
# ... and defer to numpy
431434
strict = self.config.strict_check

scipy_doctest/tests/failure_cases.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,33 @@ def dict_wrong_values_np():
8686
"""
8787
>>> import numpy as np
8888
>>> dict(a=1, b=np.arange(3)/3)
89-
{'a': 1, 'b': array([0, 0.335, 0.669])}
89+
{'a': 1, 'b': array([0, 0.335, 0.69])}
9090
"""
9191

9292

9393
def dict_nested_wrong_values_np():
9494
"""
9595
>>> import numpy as np
9696
>>> dict(a=1, b=dict(blurb=np.arange(3)/3))
97-
{'a': 1, 'b': {'blurb': array([0, 0.335, 0.669])}}
97+
{'a': 1, 'b': {'blurb': array([0, 0.335, 0.69])}}
98+
"""
99+
100+
101+
# This is an XFAIL
102+
# Currently, checking nested dicts falls back to vanilla doctest
103+
# When this is fixed, move this case to module_cases.py
104+
def dict_nested_needs_numeric_comparison():
105+
"""
106+
>>> import numpy as np
107+
>>> dict(a=1, b=dict(blurb=np.arange(3)/3))
108+
{'a': 1, 'b': {'blurb': array([0, 0.333, 0.667])}}
109+
"""
110+
111+
112+
# This is an XFAIL
113+
# Nested sequences which contain strings fall back to vanilla doctest
114+
def list_of_tuples():
115+
"""
116+
>>> [('a', 1/3), ('b', 2/3)]
117+
[('a', 0.333), ('b', 0.667)]
98118
"""

scipy_doctest/tests/module_cases.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,26 @@ def two_dicts():
249249
>>> import numpy as np
250250
>>> dict(a=0, b=1)
251251
{'a': 0, 'b': 1}
252+
>>> {'a': 1/3, 'b': 2/3}
253+
{'a': 0.333, 'b': 0.667}
252254
>>> {'a': 0., 'b': np.arange(3) / 3 }
253-
{'a': 0.0, 'b': array([0, 0.33333333, 0.66666667])}
255+
{'a': 0.0, 'b': array([0, 0.333, 0.667])}
254256
"""
255257

256258
def nested_dicts():
259+
# Note that we need to give all digits: checking nested dictionaries
260+
# currently fall back to vanilla doctest.
261+
# cf failure_cases.py::dict_nested_needs_numeric_comparison
257262
"""
258263
>>> import numpy as np
259264
>>> {'a': 1.0, 'b': dict(blurb=np.arange(3)/3)}
260265
{'a': 1.0, 'b': {'blurb': array([0, 0.33333333, 0.66666667])}}
261266
"""
262267

268+
269+
def list_of_tuples_numeric():
270+
# cf failure_cases.py::list_of_tuples --- string entries preclude numeric comparisons
271+
"""
272+
>>> [(1, 1/3), (2, 2/3)]
273+
[(1, 0.333), (2, 0.667)]
274+
"""

scipy_doctest/tests/test_testmod.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def test_tuple_and_list():
117117
config=config)
118118
assert res.failed == 2
119119

120+
121+
def test_list_of_tuples():
122+
config = DTConfig()
123+
res, _ = _testmod(failure_cases,
124+
strategy=[failure_cases.list_of_tuples],
125+
config=config)
126+
assert res.failed == 1
127+
128+
120129
def test_dict():
121130
config = DTConfig()
122131
res, _ = _testmod(failure_cases,
@@ -125,9 +134,11 @@ def test_dict():
125134
failure_cases.dict_wrong_keys,
126135
failure_cases.dict_wrong_values,
127136
failure_cases.dict_wrong_values_np,
128-
failure_cases.dict_nested_wrong_values_np],
137+
failure_cases.dict_nested_wrong_values_np,
138+
failure_cases.dict_nested_needs_numeric_comparison,
139+
],
129140
config=config)
130-
assert res.failed == 6
141+
assert res.failed == 7
131142

132143

133144
@pytest.mark.parametrize('strict, num_fails', [(True, 1), (False, 0)])

0 commit comments

Comments
 (0)