From ea897c9cb3056feb85561be749f05fd077217acb Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 21 Apr 2025 00:05:09 +0100 Subject: [PATCH] Expose dict view types in ``types`` --- Doc/library/types.rst | 21 ++++++++++++++++ Lib/test/test_types.py | 25 ++++++++++++++----- Lib/types.py | 4 +++ ...-04-21-00-01-05.gh-issue-132760.SPOms8.rst | 2 ++ Modules/_typesmodule.c | 3 +++ 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-21-00-01-05.gh-issue-132760.SPOms8.rst diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 2bedd7fdd3c8c8..0092018200fdb1 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -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 ` objects, + as returned by :meth:`dict.items`. + + .. versionadded:: next + +.. class:: DictKeysType + + The type of :ref:`keys view ` objects, + as returned by :meth:`dict.keys`. + + .. versionadded:: next + +.. class:: DictValuesType + + The type of :ref:`values view ` 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__``. diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index e5d80ee8eb7aca..e703a35b940e70 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -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__)) @@ -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. diff --git a/Lib/types.py b/Lib/types.py index 6efac3394345a5..60d41a043827bf 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -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. diff --git a/Misc/NEWS.d/next/Library/2025-04-21-00-01-05.gh-issue-132760.SPOms8.rst b/Misc/NEWS.d/next/Library/2025-04-21-00-01-05.gh-issue-132760.SPOms8.rst new file mode 100644 index 00000000000000..73b584fb029817 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-21-00-01-05.gh-issue-132760.SPOms8.rst @@ -0,0 +1,2 @@ +Expose the :ref:`dictionary view types ` in the :mod:`types` module. +Patch by Adam Turner. diff --git a/Modules/_typesmodule.c b/Modules/_typesmodule.c index a30a88196e7192..672dce1dae6a14 100644 --- a/Modules/_typesmodule.c +++ b/Modules/_typesmodule.c @@ -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);