mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #4834 from lamby/895553-sphinx-please-make-the-set-object-description-reproducible
Ensure set object descriptions are reproducible.
This commit is contained in:
1
AUTHORS
1
AUTHORS
@@ -40,6 +40,7 @@ Other contributors, listed alphabetically, are:
|
||||
* Timotheus Kampik - JS theme & search enhancements
|
||||
* Dave Kuhlman -- original LaTeX writer
|
||||
* Blaise Laflamme -- pyramid theme
|
||||
* Chris Lamb -- reproducibility fixes
|
||||
* Thomas Lamb -- linkcheck builder
|
||||
* Łukasz Langa -- partial support for autodoc
|
||||
* Ian Lee -- quickstart improvements
|
||||
|
||||
@@ -244,8 +244,19 @@ def object_description(object):
|
||||
except TypeError:
|
||||
pass # Cannot sort dict keys, fall back to generic repr
|
||||
else:
|
||||
items = ("%r: %r" % (key, object[key]) for key in sorted_keys)
|
||||
items = ("%s: %s" %
|
||||
(object_description(key), object_description(object[key]))
|
||||
for key in sorted_keys)
|
||||
return "{%s}" % ", ".join(items)
|
||||
if isinstance(object, set):
|
||||
try:
|
||||
sorted_values = sorted(object)
|
||||
except TypeError:
|
||||
pass # Cannot sort set values, fall back to generic repr
|
||||
else:
|
||||
template = "{%s}" if PY3 else "set([%s])"
|
||||
return template % ", ".join(object_description(x)
|
||||
for x in sorted_values)
|
||||
try:
|
||||
s = repr(object)
|
||||
except Exception:
|
||||
|
||||
@@ -346,6 +346,24 @@ def test_dictionary_sorting():
|
||||
assert description == "{'a': 1, 'b': 4, 'c': 3, 'd': 2}"
|
||||
|
||||
|
||||
def test_set_sorting():
|
||||
set_ = set("gfedcba")
|
||||
description = inspect.object_description(set_)
|
||||
if PY3:
|
||||
assert description == "{'a', 'b', 'c', 'd', 'e', 'f', 'g'}"
|
||||
else:
|
||||
assert description == "set(['a', 'b', 'c', 'd', 'e', 'f', 'g'])"
|
||||
|
||||
|
||||
def test_set_sorting_fallback():
|
||||
set_ = set((None, 1))
|
||||
description = inspect.object_description(set_)
|
||||
if PY3:
|
||||
assert description in ("{1, None}", "{None, 1}")
|
||||
else:
|
||||
assert description in ("set([1, None])", "set([None, 1])")
|
||||
|
||||
|
||||
def test_dict_customtype():
|
||||
class CustomType(object):
|
||||
def __init__(self, value):
|
||||
|
||||
Reference in New Issue
Block a user