Bug#1153243: sphinx: please make the output of object_description reproducible when passed a types.MappingProxyType instance

Chris Lamb lamby at debian.org
Fri Nov 6 17:27:41 GMT 2026


Source: sphinx
Version: 9.2.0-1
Severity: wishlist
Tags: patch
User: reproducible-builds at lists.alioth.debian.org
Usertags: randomness toolchain
X-Debbugs-Cc: reproducible-bugs at lists.alioth.debian.org

Hi,

Whilst working on the Reproducible Builds effort [0], we noticed that
sphinx was generating nondetermistic output.

Specifically, although Sphinx has code to generate determinstic string
representations of many Python objects (via the object_description method),
it does not (yet) know to traverse types.MappingProxyType instances.

The existing code will run repr(…) on these dict-like objects and this
will usually result in deterministic output. But the keys of these of
dictionaries are not sorted (AFAICT), and, more importantly, the values
of MappingProxyType's underlying dict are not recursively passed to
object_description.

That means that if a MappingProxyType dict contains, say, a frozenset
instance, then it will not be 'sorted' by object_description, and thus
Sphinx will generate output nondetermistically when facing this kind of
instance.

   >>> types.MappingProxyType({"key": frozenset(("a", "b"))})

You can see this in the python-keystoneauth1 package:

  https://tests.reproducible-builds.org/debian/rb-pkg/unstable/amd64/python-keystoneauth1.html

This diff is non-intuitive to read, but you have:

   __dict__ = mappingproxy(
      {
       '__abstractmethods__': frozenset({'from_token', 'is_interface_match'}),
       '__annotate_func__': None,

     [etc]

... where the frozenset assigned to __abstractmethods__ is the one not being sorted
as it is never seen by object_description.

Patch attached.

  [0] https://reproducible-builds.org/


Regards,

-- 
      ,''`.
     : :'  :     Chris Lamb
     `. `'`      lamby at debian.org / chris-lamb.co.uk
       `-
-------------- next part --------------
diff --git sphinx/util/inspect.py sphinx/util/inspect.py
index 140d959..3636341 100644
--- sphinx/util/inspect.py
+++ sphinx/util/inspect.py
@@ -540,6 +540,11 @@ def object_description(obj: Any, *, _seen: frozenset[int] = frozenset()) -> str:
             return 'list(...)'
         seen |= {id(obj)}
         return '[%s]' % ', '.join(object_description(x, _seen=seen) for x in obj)
+    elif isinstance(obj, types.MappingProxyType):
+        if id(obj) in seen:
+            return 'mappingproxy(...)'
+        seen |= {id(obj)}
+        return 'mappingproxy(%s)' % object_description(dict(obj))
 
     try:
         s = repr(obj)


More information about the Reproducible-bugs mailing list