@@ -135,11 +135,15 @@ The following directives are provided for module and class contents:
135135
136136 This will be parsed as a Python expression for cross-referencing
137137 the type annotation.
138- As such, the argument to ``:type:`` should be a valid Python expression.
138+ As such, the argument to ``:type:`` should be a valid `annotation expression`_ .
139139
140- .. caution:: The valid syntax for the ``:type:`` directive option
141- differs from the syntax for the ``:type:`` `info field
142- <info-field-lists_>`__.
140+ .. caution::
141+ The valid syntax for the ``:type:`` directive option differs from
142+ the syntax for the ``:type:`` `info field <info-field-lists_>`__.
143+ The ``:type:`` directive option does not understand
144+ reStructuredText markup or the ``or`` or ``of`` keywords,
145+ meaning unions must use ``|`` and sequences must use square brackets,
146+ and roles such as ``:ref:`...``` cannot be used.
143147
144148 .. versionadded:: 2.4
145149
@@ -277,11 +281,15 @@ The following directives are provided for module and class contents:
277281
278282 This will be parsed as a Python expression for cross-referencing
279283 the type annotation.
280- As such, the argument to ``:type:`` should be a valid Python expression.
284+ As such, the argument to ``:type:`` should be a valid `annotation expression`_ .
281285
282- .. caution:: The valid syntax for the ``:type:`` directive option
283- differs from the syntax for the ``:type:`` `info field
284- <info-field-lists_>`__.
286+ .. caution::
287+ The valid syntax for the ``:type:`` directive option differs from
288+ the syntax for the ``:type:`` `info field <info-field-lists_>`__.
289+ The ``:type:`` directive option does not understand
290+ reStructuredText markup or the ``or`` or ``of`` keywords,
291+ meaning unions must use ``|`` and sequences must use square brackets,
292+ and roles such as ``:ref:`...``` cannot be used.
285293
286294 .. versionadded:: 2.4
287295
@@ -329,11 +337,15 @@ The following directives are provided for module and class contents:
329337
330338 This will be parsed as a Python expression for cross-referencing
331339 the type annotation.
332- As such, the argument to ``:type:`` should be a valid Python expression.
340+ As such, the argument to ``:type:`` should be a valid `annotation expression`_ .
333341
334- .. caution:: The valid syntax for the ``:type:`` directive option
335- differs from the syntax for the ``:type:`` `info field
336- <info-field-lists_>`__.
342+ .. caution::
343+ The valid syntax for the ``:type:`` directive option differs from
344+ the syntax for the ``:type:`` `info field <info-field-lists_>`__.
345+ The ``:type:`` directive option does not understand
346+ reStructuredText markup or the ``or`` or ``of`` keywords,
347+ meaning unions must use ``|`` and sequences must use square brackets,
348+ and roles such as ``:ref:`...``` cannot be used.
337349
338350 .. rst::directive:option:: module
339351 :type: text
@@ -542,50 +554,66 @@ The following directives are provided for module and class contents:
542554
543555 Refer to a decorator method using the :rst:role: `py:meth ` role.
544556
557+ .. _annotation expression : https://typing.readthedocs.io/en/latest/spec/annotations.html#type-and-annotation-expressions
558+
545559.. _signatures :
546560
547561Python Signatures
548562-----------------
549563
550564Signatures of functions, methods and class constructors can be given like they
551565would be written in Python.
566+ This can include default values, positional-only or keyword-only parameters,
567+ type annotations, and type parameters.
568+ For example:
569+
570+ .. code-block :: rst
552571
553- Default values for optional arguments can be given (but if they contain commas,
554- they will confuse the signature parser). Python 3-style argument annotations
555- can also be given as well as return type annotations::
572+ .. py:function:: compile(source: str, filename: Path, symbol: str = 'file') -> ast.AST
556573
557- .. py:function:: compile(source : string, filename, symbol='file') -> ast object
574+ .. py :function :: compile (source: str , filename: Path, symbol: str = ' file' ) -> ast.AST
575+ :no-index:
558576
559577For functions with optional parameters that don't have default values
560578(typically functions implemented in C extension modules without keyword
561- argument support), you can use brackets to specify the optional parts:
579+ argument support),
580+ you can list multiple versions of the same signature in a single directive:
562581
563- .. py :function :: compile (source[, filename[, symbol]])
564- :no-contents-entry:
565- :no-index-entry:
582+ .. py :function :: compile (source)
583+ compile (source, filename)
584+ compile (source, filename, symbol)
585+ :no-index:
586+
587+ Another approach is to use square brackets to specify the optional parts.
588+ When using square brackets, it is customary to place
589+ the opening bracket before the comma (``[, ``).
566590
567- It is customary to put the opening bracket before the comma.
591+ .. py :function :: compile (source[, filename[, symbol]])
592+ :no-index:
568593
569594Python 3.12 introduced *type parameters *, which are type variables
570- declared directly within the class or function definition:
595+ declared directly within the class or function definition:
571596
572597.. code-block :: python
573598
574599 class AnimalList[AnimalT](list[AnimalT]):
575- ...
600+ ...
576601
577602 def add[T](a: T, b: T) -> T:
578- return a + b
603+ return a + b
579604
580- The corresponding reStructuredText documentation would be:
605+ The corresponding reStructuredText markup would be:
581606
582607.. code-block :: rst
583608
584609 .. py:class:: AnimalList[AnimalT]
585610
586611 .. py:function:: add[T](a: T, b: T) -> T
587612
588- See :pep: `695 ` and :pep: `696 ` for details and the full specification.
613+ .. seealso ::
614+
615+ :pep: `695 ` and :pep: `696 `, for details and the full specification.
616+
589617
590618.. _info-field-lists :
591619
0 commit comments