Skip to content

Commit 8634c8f

Browse files
committed
pythongh-145099: Fix frozendict.__doc__ using dict's docstring
PyFrozenDict_Type.tp_doc was set to `dictionary_doc`, which describes `dict()` — saying "dict() -> new empty dictionary" throughout. Since frozendict is a separate type, it needs its own docstring. Add a `frozendict_doc` with correct "frozendict()" references and a note that frozendict is immutable and hashable. https://claude.ai/code/session_01EdeU6enhg5YoLvKmBPpiZf
1 parent f5c2531 commit 8634c8f

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/test/test_dict.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,11 @@ def test_hash(self):
18031803
with self.assertRaisesRegex(TypeError, "unhashable type: 'list'"):
18041804
hash(fd)
18051805

1806+
def test_doc(self):
1807+
# frozendict.__doc__ must mention "frozendict", not "dict"
1808+
self.assertIn("frozendict", frozendict.__doc__)
1809+
self.assertNotIn("dict() ->", frozendict.__doc__)
1810+
18061811
def test_fromkeys(self):
18071812
self.assertEqual(frozendict.fromkeys('abc'),
18081813
frozendict(a=None, b=None, c=None))

Objects/dictobject.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8123,6 +8123,19 @@ PyFrozenDict_New(PyObject *iterable)
81238123
}
81248124

81258125

8126+
PyDoc_STRVAR(frozendict_doc,
8127+
"frozendict() -> new empty frozendict\n"
8128+
"frozendict(mapping) -> new frozendict initialized from a mapping object's\n"
8129+
" (key, value) pairs\n"
8130+
"frozendict(iterable) -> new frozendict initialized as if via:\n"
8131+
" d = {}\n"
8132+
" for k, v in iterable:\n"
8133+
" d[k] = v\n"
8134+
"frozendict(**kwargs) -> new frozendict initialized with the name=value pairs\n"
8135+
" in the keyword argument list. For example: frozendict(one=1, two=2)\n"
8136+
"\n"
8137+
"Unlike dict, frozendict is immutable and hashable.");
8138+
81268139
PyTypeObject PyFrozenDict_Type = {
81278140
PyVarObject_HEAD_INIT(&PyType_Type, 0)
81288141
.tp_name = "frozendict",
@@ -8137,7 +8150,7 @@ PyTypeObject PyFrozenDict_Type = {
81378150
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
81388151
| Py_TPFLAGS_BASETYPE
81398152
| _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_MAPPING,
8140-
.tp_doc = dictionary_doc,
8153+
.tp_doc = frozendict_doc,
81418154
.tp_traverse = dict_traverse,
81428155
.tp_clear = dict_tp_clear,
81438156
.tp_richcompare = dict_richcompare,

0 commit comments

Comments
 (0)