Skip to content

Commit

Permalink
Update the 'Python Signatures' section
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 26, 2025
1 parent 4d4123b commit 44891a4
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions doc/usage/domains/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -563,43 +563,57 @@ Python Signatures

Signatures of functions, methods and class constructors can be given like they
would be written in Python.
This can include default values, positional-only or keyword-only parameters,
type annotations, and type parameters.
For example:

Default values for optional arguments can be given (but if they contain commas,
they will confuse the signature parser). Python 3-style argument annotations
can also be given as well as return type annotations::
.. code-block:: rst
.. py:function:: compile(source: str, filename: Path, symbol: str = 'file') -> ast.AST
.. py:function:: compile(source : string, filename, symbol='file') -> ast object
.. py:function:: compile(source: str, filename: Path, symbol: str = 'file') -> ast.AST
:no-index:

For functions with optional parameters that don't have default values
(typically functions implemented in C extension modules without keyword
argument support), you can use brackets to specify the optional parts:
argument support),
you can list multiple versions of the same signature in a single directive:

.. py:function:: compile(source[, filename[, symbol]])
:no-contents-entry:
:no-index-entry:
.. py:function:: compile(source)
compile(source, filename)
compile(source, filename, symbol)
:no-index:

It is customary to put the opening bracket before the comma.
Another approach is to use square brackets to specify the optional parts.
When using square brackets, it is customary to place
the opening bracket before the comma (``[,``).

.. py:function:: compile(source[, filename[, symbol]])
:no-index:

Python 3.12 introduced *type parameters*, which are type variables
declared directly within the class or function definition:
declared directly within the class or function definition:

.. code-block:: python
class AnimalList[AnimalT](list[AnimalT]):
...
...
def add[T](a: T, b: T) -> T:
return a + b
return a + b
The corresponding reStructuredText documentation would be:
The corresponding reStructuredText markup would be:

.. code-block:: rst
.. py:class:: AnimalList[AnimalT]
.. py:function:: add[T](a: T, b: T) -> T
See :pep:`695` and :pep:`696` for details and the full specification.
.. seealso::

:pep:`695` and :pep:`696`, for details and the full specification.


.. _info-field-lists:

Expand Down

0 comments on commit 44891a4

Please sign in to comment.