Skip to content

Commit 419a70d

Browse files
committed
Document the special frozendict.fromkeys() behavior
Call the type constructor with a frozendict for all frozendict subclasses.
1 parent 5aad887 commit 419a70d

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,6 +5757,11 @@ Frozen dictionaries
57575757
Like dictionaries, frozendicts are :ref:`generic <generics>` over two types,
57585758
signifying (respectively) the types of the frozendict's keys and values.
57595759

5760+
.. classmethod:: fromkeys(iterable, value=None, /)
5761+
5762+
Similar to :meth:`dict.fromkeys`, but call the type constructor with a
5763+
:class:`frozendict` if the type is a :class:`frozendict` subclass.
5764+
57605765
.. versionadded:: 3.15
57615766

57625767

Lib/test/test_dict.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,8 @@ def __new__(cls, *args, **kwargs):
19551955
self.assertEqual(type(fd), FrozenDictSubclass)
19561956
self.assertEqual(created, frozendict(x=1))
19571957

1958-
# Dict subclass which overrides the constructor
1958+
# Dict subclass with a constructor which returns a frozendict
1959+
# by default
19591960
class DictSubclass(dict):
19601961
def __new__(cls, *args, **kwargs):
19611962
if args or kwargs:

Objects/dictobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,12 +3419,9 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
34193419
PyObject *d;
34203420
int need_copy = 0;
34213421

3422-
PyTypeObject *cls_type = _PyType_CAST(cls);
3423-
if (PyObject_IsSubclass(cls, (PyObject*)&PyFrozenDict_Type)
3424-
&& cls_type->tp_new == frozendict_new)
3425-
{
3422+
if (cls == (PyObject*)&PyFrozenDict_Type) {
34263423
// gh-151722: Create a frozendict which is not tracked by the GC.
3427-
d = frozendict_new_untracked(cls_type);
3424+
d = frozendict_new_untracked(&PyFrozenDict_Type);
34283425
}
34293426
else {
34303427
// Dict subclass, or frozendict subclass which overrides

0 commit comments

Comments
 (0)