static PyMethodDef my_obj_methods[] = {
// Other methods.
...
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"}
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "my_obj is generic over its contained type"}
...
}
:exc:`TypeError` is raised. Array objects also implement the buffer interface,
and may be used wherever :term:`bytes-like objects <bytes-like object>` are supported.
+ Arrays are :ref:`generic <generics>` over the type of their contents.
+
.. audit-event:: array.__new__ typecode,initializer array.array
implementations can inject their own optimized implementations
of a Future object.
+ Futures are :ref:`generic <generics>` over the type of their results.
+
.. versionchanged:: 3.7
Added support for the :mod:`contextvars` module.
blocks. If the coroutine returns or raises without blocking, the task
will be finished eagerly and will skip scheduling to the event loop.
+ Tasks are :ref:`generic <generics>` over the return type of their wrapped
+ coroutines.
+
.. versionchanged:: 3.7
Added support for the :mod:`contextvars` module.
Unix. They are also useful for tracking transactions and other pools of data
where only the most recent activity is of interest.
+ Deques are :ref:`generic <generics>` over the type of their contents.
+
Deque objects support the following methods:
as if they were passed to the :class:`dict` constructor, including keyword
arguments.
+ :class:`!defaultdict`\s are :ref:`generic <generics>` over two types,
+ signifying (respectively) the types of the dictionary's keys and values.
+
:class:`defaultdict` objects support the following method in addition to the
standard :class:`dict` operations:
references to context variables which prevents context variables
from being properly garbage collected.
+ :class:`!ContextVar`\s are :ref:`generic <generics>` over the type of
+ their contained value.
+
.. attribute:: ContextVar.name
The name of the variable. This is a read-only property.
Tokens support the :ref:`context manager protocol <context-managers>`
to automatically reset context variables. See :meth:`ContextVar.set`.
+ Tokens are :ref:`generic <generics>` over the same type as the
+ :class:`ContextVar` which created them.
+
.. versionadded:: 3.14
Added support for usage as a context manager.
subscript and slice accesses; for slice reads, the resulting object is
*not* itself an :class:`Array`.
+ Arrays are :ref:`generic <generics>` over the type of their elements.
+
.. attribute:: _length_
raises a :exc:`TypeError` if any contained exception is not an
:exc:`Exception` subclass.
+ Exception groups are :ref:`generic <generics>` over the type of their
+ contained exceptions.
+
.. impl-detail::
The ``excs`` parameter may be any sequence, but lists and tuples are
To be directly usable as a :term:`path-like object`, ``os.DirEntry``
implements the :class:`PathLike` interface.
+ :class:`!DirEntry` objects are :ref:`generic <generics>` over the type of the
+ path (:class:`str` or :class:`bytes`).
+
Attributes and methods on a ``os.DirEntry`` instance are as follows:
.. attribute:: name
Constructor for an unbounded :abbr:`FIFO (first-in, first-out)` queue.
Simple queues lack advanced functionality such as task tracking.
+ Simple queues are :ref:`generic <generics>` over the type of their items.
+
.. versionadded:: 3.7
Compiled regular expression object returned by :func:`re.compile`.
+ Patterns are :ref:`generic <generics>` over the type of string they handle
+ (:class:`str` or :class:`bytes`).
+
.. versionchanged:: 3.9
:py:class:`re.Pattern` supports ``[]`` to indicate a Unicode (str) or bytes pattern.
See :ref:`types-genericalias`.
Match object returned by successful ``match``\ es and ``search``\ es.
+ Matches are :ref:`generic <generics>` over the type of string which was
+ matched (:class:`str` or :class:`bytes`).
+
.. versionchanged:: 3.9
:py:class:`re.Match` supports ``[]`` to indicate a Unicode (str) or bytes match.
See :ref:`types-genericalias`.
Many other operations also produce lists, including the :func:`sorted`
built-in.
+ Lists are :ref:`generic <generics>` over the types of their items.
+
Lists implement all of the :ref:`common <typesseq-common>` and
:ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the
following additional method:
Tuples implement all of the :ref:`common <typesseq-common>` sequence
operations.
+ Tuples are :ref:`generic <generics>` over the types of their contents.
+ For more information, refer to
+ :ref:`the typing documentation on annotating tuples <annotating-tuples>`.
+
For heterogeneous collections of data where access by name is clearer than
access by index, :func:`collections.namedtuple` may be a more appropriate
choice than a simple tuple object.
types such as :class:`bytes` and :class:`bytearray`, an element is a single
byte, but other types such as :class:`array.array` may have bigger elements.
+ :class:`!memoryview`\s are :ref:`generic <generics>` over the type of their
+ underlying data.
+
``len(view)`` is equal to the length of :meth:`~memoryview.tolist`, which
is the nested list representation of the view. If ``view.ndim = 1``,
this is equal to the number of elements in the view.
:meth:`~set.discard` methods may be a set. To support searching for an equivalent
frozenset, a temporary one is created from *elem*.
+Sets and frozensets are :ref:`generic <generics>` over the type of their elements.
+
.. seealso::
For detailed information on thread-safety guarantees for :class:`set`
Dictionary order is guaranteed to be insertion order. This behavior was
an implementation detail of CPython from 3.6.
+ Dictionaries are :ref:`generic <generics>` over two types, signifying
+ (respectively) the types of the dictionary's keys and values.
+
These are the operations that dictionaries support (and therefore, custom
mapping types should support too):
:class:`!frozendict` is not a :class:`!dict` subclass but inherits directly
from ``object``.
+ Like dictionaries, frozendicts are :ref:`generic <generics>` over two types,
+ signifying (respectively) the types of the frozendict's keys and values.
+
.. versionadded:: 3.15
...
3.0 | 1. + 2. | None | .2f
+ Interpolations are :ref:`generic <generics>` over the types of their values.
+
.. rubric:: Attributes
.. attribute:: value
entries, which means that when the mapping changes, the view reflects these
changes.
+ :class:`!MappingProxyType`\s are :ref:`generic <generics>` over two types,
+ signifying (respectively) the types of the underlying mapping's keys and
+ values.
+
.. versionadded:: 3.3
.. versionchanged:: 3.9
This is a subclassable type rather than a factory function.
+ Weak references are :ref:`generic <generics>` over the type of the object they
+ reference.
+
.. attribute:: __callback__
This read-only attribute returns the callback currently associated to the
those of generators (see :ref:`generator-methods`). However, unlike
generators, coroutines do not directly support iteration.
+Coroutines are :ref:`generic <generics>` over the types of their yield, send,
+and return values, respectively.
+
.. versionchanged:: 3.5.2
It is a :exc:`RuntimeError` to await on a coroutine more than once.
--- /dev/null
+Generic builtin and standard library types now document the meaning of their
+type parameters.
_ASYNCIO_FUTURE_DONE_METHODDEF
_ASYNCIO_FUTURE_GET_LOOP_METHODDEF
_ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("Futures are generic over the type of their results")},
{NULL, NULL} /* Sentinel */
};
_ASYNCIO_TASK_SET_NAME_METHODDEF
_ASYNCIO_TASK_GET_CORO_METHODDEF
_ASYNCIO_TASK_GET_CONTEXT_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("Tasks are generic over the return type of their wrapped coroutines")},
{NULL, NULL} /* Sentinel */
};
DEQUE_ROTATE_METHODDEF
DEQUE___SIZEOF___METHODDEF
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("deques are generic over the type of their contents")},
{NULL, NULL} /* sentinel */
};
return result;
}
+
+PyDoc_STRVAR(defdict_class_getitem_doc,
+"defaultdicts are generic over two types, signifying (respectively) the types \
+of the dictionary's keys and values");
+
+
static PyMethodDef defdict_methods[] = {
{"__missing__", defdict_missing, METH_O,
defdict_missing_doc},
{"__reduce__", defdict_reduce, METH_NOARGS,
reduce_doc},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
- PyDoc_STR("See PEP 585")},
+ defdict_class_getitem_doc},
{NULL}
};
static PyMethodDef Array_methods[] = {
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("Arrays are generic over the type of their elements")},
{ NULL, NULL }
};
{"__reduce__", partial_reduce, METH_NOARGS},
{"__setstate__", partial_setstate, METH_O},
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ PyDoc_STR("partial is generic over the wrapped function's return type")},
{NULL, NULL} /* sentinel */
};
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
_QUEUE_SIMPLEQUEUE___SIZEOF___METHODDEF
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("SimpleQueues are generic over the type of their contents")},
{NULL, NULL} /* sentinel */
};
_SRE_SRE_PATTERN___DEEPCOPY___METHODDEF
_SRE_SRE_PATTERN__FAIL_AFTER_METHODDEF
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
- PyDoc_STR("See PEP 585")},
+ PyDoc_STR("Patterns are generic over the type of string they handle (str or bytes)")},
{NULL, NULL}
};
_SRE_SRE_MATCH___COPY___METHODDEF
_SRE_SRE_MATCH___DEEPCOPY___METHODDEF
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
- PyDoc_STR("See PEP 585")},
+ PyDoc_STR("Matches are generic over the type of string which was matched (str or bytes)")},
{NULL, NULL}
};
ARRAY_ARRAY_TOBYTES_METHODDEF
ARRAY_ARRAY_TOUNICODE_METHODDEF
ARRAY_ARRAY___SIZEOF___METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("Arrays are generic over the type of their elements")},
{NULL, NULL} /* sentinel */
};
first iterable until it is exhausted, then elements from the next\n\
iterable, until all of the iterables are exhausted.");
+PyDoc_STRVAR(chain_class_getitem_doc,
+"chain is generic over the type of its contents.\n\
+This is the union of the types of the input iterable contents.");
+
static PyMethodDef chain_methods[] = {
ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, chain_class_getitem_doc},
{NULL, NULL} /* sentinel */
};
OS_DIRENTRY_INODE_METHODDEF
OS_DIRENTRY___FSPATH___METHODDEF
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("DirEntry is generic over the type of the path (str or bytes)")},
{NULL}
};
{"copy", mappingproxy_copy, METH_NOARGS,
PyDoc_STR("D.copy() -> a shallow copy of D")},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
- PyDoc_STR("See PEP 585")},
+ PyDoc_STR("mappingproxy objects are generic over two types, signifying (respectively) the types of their keys and values")},
{"__reversed__", mappingproxy_reversed, METH_NOARGS,
PyDoc_STR("D.__reversed__() -> reverse iterator")},
{0}
DICT_CLEAR_METHODDEF
DICT_COPY_METHODDEF
DICT___REVERSED___METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("dicts are generic over two types, signifying (respectively) the types of their keys and values")},
{NULL, NULL} /* sentinel */
};
DICT_FROMKEYS_METHODDEF
FROZENDICT_COPY_METHODDEF
DICT___REVERSED___METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("frozendicts are generic over two types, signifying (respectively) the types of the frozendict's keys and values")},
{"__getnewargs__", frozendict_getnewargs, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
static PyMethodDef enum_methods[] = {
{"__reduce__", enum_reduce, METH_NOARGS, reduce_doc},
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("'enumerate' objects are generic over the type of their values")},
{NULL, NULL} /* sentinel */
};
static PyMethodDef BaseExceptionGroup_methods[] = {
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ PyDoc_STR("Exception groups are generic over the type of their contained exceptions")},
BASEEXCEPTIONGROUP_DERIVE_METHODDEF
BASEEXCEPTIONGROUP_SPLIT_METHODDEF
BASEEXCEPTIONGROUP_SUBGROUP_METHODDEF
{"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
{"close", gen_close, METH_NOARGS, close_doc},
{"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("generators are generic over the types of their yield, send, and return values")},
{NULL, NULL} /* Sentinel */
};
{"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
{"close", gen_close, METH_NOARGS, coro_close_doc},
{"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("coroutines are generic over the types of their yield, send, and return values")},
{NULL, NULL} /* Sentinel */
};
{"aclose", async_gen_aclose, METH_NOARGS, async_aclose_doc},
{"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("async generators are generic over the types of their yield and send values")},
{NULL, NULL} /* Sentinel */
};
{"__reduce__", interpolation_reduce, METH_NOARGS,
PyDoc_STR("__reduce__() -> (cls, state)")},
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS, PyDoc_STR("Interpolations are generic over the types of their values")},
{NULL, NULL},
};
LIST_COUNT_METHODDEF
LIST_REVERSE_METHODDEF
LIST_SORT_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("lists are generic over the type of their contents")},
{NULL, NULL} /* sentinel */
};
MEMORYVIEW_INDEX_METHODDEF
{"__enter__", memory_enter, METH_NOARGS, NULL},
{"__exit__", memory_exit, METH_VARARGS, memory_exit_doc},
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("memoryviews are generic over the type of their underlying data")},
{NULL, NULL}
};
SET_SYMMETRIC_DIFFERENCE_UPDATE_METHODDEF
SET_UNION_METHODDEF
SET_UPDATE_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("sets are generic over the type of their elements")},
{NULL, NULL} /* sentinel */
};
SET___SIZEOF___METHODDEF
SET_SYMMETRIC_DIFFERENCE_METHODDEF
SET_UNION_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ PyDoc_STR("frozensets are generic over the type of their elements")},
{NULL, NULL} /* sentinel */
};
static PyMethodDef slice_methods[] = {
{"indices", slice_indices, METH_O, slice_indices_doc},
{"__reduce__", slice_reduce, METH_NOARGS, reduce_doc},
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
+ "slices are generic over the types of their start, end, and step values"},
{NULL, NULL}
};
static PyMethodDef template_methods[] = {
{"__reduce__", template_reduce, METH_NOARGS, NULL},
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ // note that this is not supported in typeshed, and it is not clear if the
+ // type for this is a simple TypeVar or a TypeVarTuple
+ // for details, see: https://github.com/python/typeshed/issues/14878
+ PyDoc_STR("Template supports [] for generic usage")},
{NULL, NULL},
};
return Py_BuildValue("(N)", tuple_slice(self, 0, Py_SIZE(self)));
}
+
+PyDoc_STRVAR(tuple_class_getitem_doc,
+"Tuples are generic over the types of their contents.\n\n\
+For example, use ``tuple[int, str]`` for a pair whose first element is an int and second element is a string.\n\n\
+Tuples also support the form ``tuple[T, ...]`` to indicate an arbitrary length tuple of elements of type T.");
+
static PyMethodDef tuple_methods[] = {
TUPLE___GETNEWARGS___METHODDEF
TUPLE_INDEX_METHODDEF
TUPLE_COUNT_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, tuple_class_getitem_doc},
{NULL, NULL} /* sentinel */
};
static PyMethodDef union_methods[] = {
{"__mro_entries__", union_mro_entries, METH_O},
- {"__class_getitem__", union_class_getitem, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ {"__class_getitem__", union_class_getitem, METH_O|METH_CLASS,
+ PyDoc_STR("Create a union containing the given types")},
{0}
};
static PyMethodDef weakref_methods[] = {
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ PyDoc_STR("Weakrefs are generic over the type of the referenced object.")},
{NULL} /* Sentinel */
};
_CONTEXTVARS_CONTEXTVAR_SET_METHODDEF
_CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ PyDoc_STR("ContextVars are generic over the type of their contained values")},
{NULL, NULL}
};
static PyMethodDef PyContextTokenType_methods[] = {
{"__class_getitem__", Py_GenericAlias,
- METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
+ METH_O|METH_CLASS,
+ PyDoc_STR("Tokens are generic over the same type as the ContextVar which created them.")},
TOKEN_ENTER_METHODDEF
TOKEN_EXIT_METHODDEF
{NULL}