Skip to content

GH-132760: Expose dict view types in types #132761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ Standard names are defined for the following types:

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

.. class:: DictItemsType

The type of :ref:`items view <dict-views>` objects,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to also note that these types are available in the dict-views section.

as returned by :meth:`dict.items`.

.. versionadded:: next

.. class:: DictKeysType

The type of :ref:`keys view <dict-views>` objects,
as returned by :meth:`dict.keys`.

.. versionadded:: next

.. class:: DictValuesType

The type of :ref:`values view <dict-views>` objects,
as returned by :meth:`dict.values`.

.. versionadded:: next

.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)

The type of traceback objects such as found in ``sys.exception().__traceback__``.
Expand Down
25 changes: 19 additions & 6 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def test_names(self):
all_names = ignored | {
'AsyncGeneratorType', 'BuiltinFunctionType', 'BuiltinMethodType',
'CapsuleType', 'CellType', 'ClassMethodDescriptorType', 'CodeType',
'CoroutineType', 'EllipsisType', 'FrameType', 'FunctionType',
'GeneratorType', 'GenericAlias', 'GetSetDescriptorType',
'LambdaType', 'MappingProxyType', 'MemberDescriptorType',
'MethodDescriptorType', 'MethodType', 'MethodWrapperType',
'ModuleType', 'NoneType', 'NotImplementedType', 'SimpleNamespace',
'TracebackType', 'UnionType', 'WrapperDescriptorType',
'CoroutineType', 'DictItemsType', 'DictKeysType', 'DictValuesType',
'EllipsisType', 'FrameType', 'FunctionType', 'GeneratorType',
'GenericAlias', 'GetSetDescriptorType', 'LambdaType',
'MappingProxyType', 'MemberDescriptorType', 'MethodDescriptorType',
'MethodType', 'MethodWrapperType', 'ModuleType', 'NoneType',
'NotImplementedType', 'SimpleNamespace', 'TracebackType',
'UnionType', 'WrapperDescriptorType',
}
self.assertEqual(all_names, set(c_types.__all__))
self.assertEqual(all_names - c_only_names, set(py_types.__all__))
Expand Down Expand Up @@ -673,6 +674,18 @@ def test_traceback_and_frame_types(self):
def test_capsule_type(self):
self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType)

def test_dict_items_type(self):
self.assertIsInstance({}.items(), types.DictItemsType)
self.assertIs(type({}.items()), types.DictItemsType)

def test_dict_keys_type(self):
self.assertIsInstance({}.keys(), types.DictKeysType)
self.assertIs(type({}.keys()), types.DictKeysType)

def test_dict_values_type(self):
self.assertIsInstance({}.values(), types.DictValuesType)
self.assertIs(type({}.values()), types.DictValuesType)

def test_call_unbound_crash(self):
# GH-131998: The specialized instruction would get tricked into dereferencing
# a bound "self" that didn't exist if subsequently called unbound.
Expand Down
4 changes: 4 additions & 0 deletions Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def _m(self): pass
NoneType = type(None)
NotImplementedType = type(NotImplemented)

DictItemsType = type({}.items())
DictKeysType = type({}.keys())
DictValuesType = type({}.values())

# CapsuleType cannot be accessed from pure Python,
# so there is no fallback definition.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Expose the :ref:`dictionary view types <dict-views>` in the :mod:`types` module.
Patch by Adam Turner.
3 changes: 3 additions & 0 deletions Modules/_typesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ _types_exec(PyObject *m)
EXPORT_STATIC_TYPE("ClassMethodDescriptorType", PyClassMethodDescr_Type);
EXPORT_STATIC_TYPE("CodeType", PyCode_Type);
EXPORT_STATIC_TYPE("CoroutineType", PyCoro_Type);
EXPORT_STATIC_TYPE("DictItemsType", PyDictItems_Type);
EXPORT_STATIC_TYPE("DictKeysType", PyDictKeys_Type);
EXPORT_STATIC_TYPE("DictValuesType", PyDictValues_Type);
EXPORT_STATIC_TYPE("EllipsisType", PyEllipsis_Type);
EXPORT_STATIC_TYPE("FrameType", PyFrame_Type);
EXPORT_STATIC_TYPE("FunctionType", PyFunction_Type);
Expand Down
Loading