Skip to content

Commit ea897c9

Browse files
committed
Expose dict view types in types
1 parent dc4a707 commit ea897c9

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

Doc/library/types.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ Standard names are defined for the following types:
318318

319319
This is now an alias for :class:`typing.Union`.
320320

321+
.. class:: DictItemsType
322+
323+
The type of :ref:`items view <dict-views>` objects,
324+
as returned by :meth:`dict.items`.
325+
326+
.. versionadded:: next
327+
328+
.. class:: DictKeysType
329+
330+
The type of :ref:`keys view <dict-views>` objects,
331+
as returned by :meth:`dict.keys`.
332+
333+
.. versionadded:: next
334+
335+
.. class:: DictValuesType
336+
337+
The type of :ref:`values view <dict-views>` objects,
338+
as returned by :meth:`dict.values`.
339+
340+
.. versionadded:: next
341+
321342
.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)
322343

323344
The type of traceback objects such as found in ``sys.exception().__traceback__``.

Lib/test/test_types.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ def test_names(self):
5151
all_names = ignored | {
5252
'AsyncGeneratorType', 'BuiltinFunctionType', 'BuiltinMethodType',
5353
'CapsuleType', 'CellType', 'ClassMethodDescriptorType', 'CodeType',
54-
'CoroutineType', 'EllipsisType', 'FrameType', 'FunctionType',
55-
'GeneratorType', 'GenericAlias', 'GetSetDescriptorType',
56-
'LambdaType', 'MappingProxyType', 'MemberDescriptorType',
57-
'MethodDescriptorType', 'MethodType', 'MethodWrapperType',
58-
'ModuleType', 'NoneType', 'NotImplementedType', 'SimpleNamespace',
59-
'TracebackType', 'UnionType', 'WrapperDescriptorType',
54+
'CoroutineType', 'DictItemsType', 'DictKeysType', 'DictValuesType',
55+
'EllipsisType', 'FrameType', 'FunctionType', 'GeneratorType',
56+
'GenericAlias', 'GetSetDescriptorType', 'LambdaType',
57+
'MappingProxyType', 'MemberDescriptorType', 'MethodDescriptorType',
58+
'MethodType', 'MethodWrapperType', 'ModuleType', 'NoneType',
59+
'NotImplementedType', 'SimpleNamespace', 'TracebackType',
60+
'UnionType', 'WrapperDescriptorType',
6061
}
6162
self.assertEqual(all_names, set(c_types.__all__))
6263
self.assertEqual(all_names - c_only_names, set(py_types.__all__))
@@ -673,6 +674,18 @@ def test_traceback_and_frame_types(self):
673674
def test_capsule_type(self):
674675
self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType)
675676

677+
def test_dict_items_type(self):
678+
self.assertIsInstance({}.items(), types.DictItemsType)
679+
self.assertIs(type({}.items()), types.DictItemsType)
680+
681+
def test_dict_keys_type(self):
682+
self.assertIsInstance({}.keys(), types.DictKeysType)
683+
self.assertIs(type({}.keys()), types.DictKeysType)
684+
685+
def test_dict_values_type(self):
686+
self.assertIsInstance({}.values(), types.DictValuesType)
687+
self.assertIs(type({}.values()), types.DictValuesType)
688+
676689
def test_call_unbound_crash(self):
677690
# GH-131998: The specialized instruction would get tricked into dereferencing
678691
# a bound "self" that didn't exist if subsequently called unbound.

Lib/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def _m(self): pass
7070
NoneType = type(None)
7171
NotImplementedType = type(NotImplemented)
7272

73+
DictItemsType = type({}.items())
74+
DictKeysType = type({}.keys())
75+
DictValuesType = type({}.values())
76+
7377
# CapsuleType cannot be accessed from pure Python,
7478
# so there is no fallback definition.
7579

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expose the :ref:`dictionary view types <dict-views>` in the :mod:`types` module.
2+
Patch by Adam Turner.

Modules/_typesmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ _types_exec(PyObject *m)
2626
EXPORT_STATIC_TYPE("ClassMethodDescriptorType", PyClassMethodDescr_Type);
2727
EXPORT_STATIC_TYPE("CodeType", PyCode_Type);
2828
EXPORT_STATIC_TYPE("CoroutineType", PyCoro_Type);
29+
EXPORT_STATIC_TYPE("DictItemsType", PyDictItems_Type);
30+
EXPORT_STATIC_TYPE("DictKeysType", PyDictKeys_Type);
31+
EXPORT_STATIC_TYPE("DictValuesType", PyDictValues_Type);
2932
EXPORT_STATIC_TYPE("EllipsisType", PyEllipsis_Type);
3033
EXPORT_STATIC_TYPE("FrameType", PyFrame_Type);
3134
EXPORT_STATIC_TYPE("FunctionType", PyFunction_Type);

0 commit comments

Comments
 (0)