blob: 8ba681f11dabccfc9f56e996538316ba04c3b41a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<span id="typehintobjects"></span><h1>Objects for Type Hinting</h1> <p>Various built-in types for type hinting are provided. Currently, two types exist – <a class="reference internal" href="../library/stdtypes#types-genericalias"><span class="std std-ref">GenericAlias</span></a> and <a class="reference internal" href="../library/stdtypes#types-union"><span class="std std-ref">Union</span></a>. Only <code>GenericAlias</code> is exposed to C.</p> <dl class="c function"> <dt class="sig sig-object c" id="c.Py_GenericAlias">
<code>PyObject *Py_GenericAlias(PyObject *origin, PyObject *args)</code> </dt> <dd>
<em class="stableabi"> Part of the <a class="reference internal" href="stable#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.9.</em><p>Create a <a class="reference internal" href="../library/stdtypes#types-genericalias"><span class="std std-ref">GenericAlias</span></a> object. Equivalent to calling the Python class <a class="reference internal" href="../library/types#types.GenericAlias" title="types.GenericAlias"><code>types.GenericAlias</code></a>. The <em>origin</em> and <em>args</em> arguments set the <code>GenericAlias</code>‘s <code>__origin__</code> and <code>__args__</code> attributes respectively. <em>origin</em> should be a <span class="c-expr sig sig-inline c"><a class="reference internal" href="type#c.PyTypeObject" title="PyTypeObject"><span class="n">PyTypeObject</span></a><span class="p">*</span></span>, and <em>args</em> can be a <span class="c-expr sig sig-inline c"><a class="reference internal" href="tuple#c.PyTupleObject" title="PyTupleObject"><span class="n">PyTupleObject</span></a><span class="p">*</span></span> or any <code>PyObject*</code>. If <em>args</em> passed is not a tuple, a 1-tuple is automatically constructed and <code>__args__</code> is set to <code>(args,)</code>. Minimal checking is done for the arguments, so the function will succeed even if <em>origin</em> is not a type. The <code>GenericAlias</code>‘s <code>__parameters__</code> attribute is constructed lazily from <code>__args__</code>. On failure, an exception is raised and <code>NULL</code> is returned.</p> <p>Here’s an example of how to make an extension type generic:</p> <pre data-language="c">...
static PyMethodDef my_obj_methods[] = {
// Other methods.
...
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"}
...
}
</pre> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The data model method <a class="reference internal" href="../reference/datamodel#object.__class_getitem__" title="object.__class_getitem__"><code>__class_getitem__()</code></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd>
</dl> <dl class="c var"> <dt class="sig sig-object c" id="c.Py_GenericAliasType">
<code>PyTypeObject Py_GenericAliasType</code> </dt> <dd>
<em class="stableabi"> Part of the <a class="reference internal" href="stable#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.9.</em><p>The C type of the object returned by <a class="reference internal" href="#c.Py_GenericAlias" title="Py_GenericAlias"><code>Py_GenericAlias()</code></a>. Equivalent to <a class="reference internal" href="../library/types#types.GenericAlias" title="types.GenericAlias"><code>types.GenericAlias</code></a> in Python.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd>
</dl> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/c-api/typehints.html" class="_attribution-link">https://docs.python.org/3.12/c-api/typehints.html</a>
</p>
</div>
|