https://github.com/python/cpython/commit/d1505b543a26d3288725ca83e4a70dfa378eb866
commit: d1505b543a26d3288725ca83e4a70dfa378eb866
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-02-17T18:04:17Z
summary:

gh-141510: Change repr(frozendict) for empty dict (#144921)

repr(frozendict()) returns "frozendict()" instead of
"frozendict({})".

files:
M Lib/test/test_dict.py
M Objects/dictobject.c

diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 2a106a8a4e8739..21f8bb11071c90 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -1767,6 +1767,9 @@ def test_update(self):
         self.assertEqual(copy, frozendict({'x': 1}))
 
     def test_repr(self):
+        d = frozendict()
+        self.assertEqual(repr(d), "frozendict()")
+
         d = frozendict(x=1, y=2)
         self.assertEqual(repr(d), "frozendict({'x': 1, 'y': 2})")
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index f7a359e4a1a40d..7db2e547b54dba 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -7868,6 +7868,11 @@ static PyMethodDef frozendict_methods[] = {
 static PyObject *
 frozendict_repr(PyObject *self)
 {
+    PyDictObject *mp = _PyAnyDict_CAST(self);
+    if (mp->ma_used == 0) {
+        return PyUnicode_FromFormat("%s()", Py_TYPE(self)->tp_name);
+    }
+
     PyObject *repr = anydict_repr_impl(self);
     if (repr == NULL) {
         return NULL;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to