| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
================= |
| 4 |
The Python Domain |
| 5 |
================= |
| 6 |
|
| 7 |
.. versionadded:: 1.0 |
| 8 |
|
| 9 |
The Python domain (name **py**) provides the following directives for module |
| 10 |
declarations: |
| 11 |
|
| 12 |
.. rst:directive:: .. py:module:: name |
| 13 |
|
| 14 |
This directive marks the beginning of the description of a module (or package |
| 15 |
submodule, in which case the name should be fully qualified, including the |
| 16 |
package name). A description of the module such as the docstring can be |
| 17 |
placed in the body of the directive. |
| 18 |
|
| 19 |
This directive will also cause an entry in the global module index. |
| 20 |
|
| 21 |
.. versionchanged:: 5.2 |
| 22 |
|
| 23 |
Module directives support body content. |
| 24 |
|
| 25 |
.. rubric:: options |
| 26 |
|
| 27 |
.. rst:directive:option:: platform: platforms |
| 28 |
:type: comma separated list |
| 29 |
|
| 30 |
Indicate platforms which the module is available (if it is available on |
| 31 |
all platforms, the option should be omitted). The keys are short |
| 32 |
identifiers; examples that are in use include "IRIX", "Mac", "Windows" |
| 33 |
and "Unix". It is important to use a key which has already been used when |
| 34 |
applicable. |
| 35 |
|
| 36 |
.. rst:directive:option:: synopsis: purpose |
| 37 |
:type: text |
| 38 |
|
| 39 |
Consist of one sentence describing the module's purpose -- it is currently |
| 40 |
only used in the Global Module Index. |
| 41 |
|
| 42 |
.. rst:directive:option:: deprecated |
| 43 |
:type: no argument |
| 44 |
|
| 45 |
Mark a module as deprecated; it will be designated as such in various |
| 46 |
locations then. |
| 47 |
|
| 48 |
|
| 49 |
.. rst:directive:: .. py:currentmodule:: name |
| 50 |
|
| 51 |
This directive tells Sphinx that the classes, functions etc. documented from |
| 52 |
here are in the given module (like :rst:dir:`py:module`), but it will not |
| 53 |
create index entries, an entry in the Global Module Index, or a link target |
| 54 |
for :rst:role:`py:mod`. This is helpful in situations where documentation |
| 55 |
for things in a module is spread over multiple files or sections -- one |
| 56 |
location has the :rst:dir:`py:module` directive, the others only |
| 57 |
:rst:dir:`py:currentmodule`. |
| 58 |
|
| 59 |
The following directives are provided for module and class contents: |
| 60 |
|
| 61 |
.. rst:directive:: .. py:function:: name(parameters) |
| 62 |
.. py:function:: name[type parameters](parameters) |
| 63 |
|
| 64 |
Describes a module-level function. |
| 65 |
The signature should include the parameters, |
| 66 |
together with optional type parameters, |
| 67 |
as given in the Python function definition, see :ref:`signatures`. |
| 68 |
For example:: |
| 69 |
|
| 70 |
.. py:function:: Timer.repeat(repeat=3, number=1_000_000) |
| 71 |
.. py:function:: add[T](a: T, b: T) -> T |
| 72 |
|
| 73 |
For methods you should use :rst:dir:`py:method`. |
| 74 |
|
| 75 |
The description normally includes information about the parameters required |
| 76 |
and how they are used (especially whether mutable objects passed as |
| 77 |
parameters are modified), side effects, and possible exceptions. |
| 78 |
|
| 79 |
This information can (in any ``py`` directive) optionally be given in a |
| 80 |
structured form, see :ref:`info-field-lists`. |
| 81 |
|
| 82 |
.. rubric:: options |
| 83 |
|
| 84 |
.. rst:directive:option:: async |
| 85 |
:type: no value |
| 86 |
|
| 87 |
Indicate the function is an async function. |
| 88 |
|
| 89 |
.. versionadded:: 2.1 |
| 90 |
|
| 91 |
.. rst:directive:option:: canonical |
| 92 |
:type: full qualified name including module name |
| 93 |
|
| 94 |
Describe the location where the object is defined if the object is |
| 95 |
imported from other modules |
| 96 |
|
| 97 |
.. versionadded:: 4.0 |
| 98 |
|
| 99 |
.. rst::directive:option:: module |
| 100 |
:type: text |
| 101 |
|
| 102 |
Describe the location where the object is defined. The default value is |
| 103 |
the module specified by :rst:dir:`py:currentmodule`. |
| 104 |
|
| 105 |
.. rst:directive:option:: single-line-parameter-list |
| 106 |
:type: no value |
| 107 |
|
| 108 |
Ensures that the function's arguments will be emitted on a single logical |
| 109 |
line, overriding :confval:`python_maximum_signature_line_length` and |
| 110 |
:confval:`maximum_signature_line_length`. |
| 111 |
|
| 112 |
.. versionadded:: 7.1 |
| 113 |
|
| 114 |
.. rst:directive:option:: single-line-type-parameter-list |
| 115 |
:type: no value |
| 116 |
|
| 117 |
Ensure that the function's type parameters are emitted on a single |
| 118 |
logical line, overriding :confval:`python_maximum_signature_line_length` |
| 119 |
and :confval:`maximum_signature_line_length`. |
| 120 |
|
| 121 |
.. versionadded:: 7.1 |
| 122 |
|
| 123 |
|
| 124 |
.. rst:directive:: .. py:data:: name |
| 125 |
|
| 126 |
Describes global data in a module, including both variables and values used |
| 127 |
as "defined constants." |
| 128 |
Consider using :rst:dir:`py:type` for type aliases instead |
| 129 |
and :rst:dir:`py:attribute` for class variables and instance attributes. |
| 130 |
|
| 131 |
.. rubric:: options |
| 132 |
|
| 133 |
.. rst:directive:option:: type: type of the variable |
| 134 |
:type: text |
| 135 |
|
| 136 |
This will be parsed as a Python expression for cross-referencing |
| 137 |
the type annotation. |
| 138 |
As such, the argument to ``:type:`` should be a valid `annotation expression`_. |
| 139 |
|
| 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. |
| 147 |
|
| 148 |
.. versionadded:: 2.4 |
| 149 |
|
| 150 |
.. rst:directive:option:: value: initial value of the variable |
| 151 |
:type: text |
| 152 |
|
| 153 |
.. versionadded:: 2.4 |
| 154 |
|
| 155 |
.. rst:directive:option:: canonical |
| 156 |
:type: full qualified name including module name |
| 157 |
|
| 158 |
Describe the location where the object is defined if the object is |
| 159 |
imported from other modules |
| 160 |
|
| 161 |
.. versionadded:: 4.0 |
| 162 |
|
| 163 |
.. rst::directive:option:: module |
| 164 |
:type: text |
| 165 |
|
| 166 |
Describe the location where the object is defined. The default value is |
| 167 |
the module specified by :rst:dir:`py:currentmodule`. |
| 168 |
|
| 169 |
.. rst:directive:: .. py:exception:: name |
| 170 |
.. py:exception:: name(parameters) |
| 171 |
.. py:exception:: name[type parameters](parameters) |
| 172 |
|
| 173 |
Describes an exception class. |
| 174 |
The signature can, but need not include parentheses with constructor arguments, |
| 175 |
or may optionally include type parameters (see :pep:`695`). |
| 176 |
|
| 177 |
.. rubric:: options |
| 178 |
|
| 179 |
.. rst:directive:option:: final |
| 180 |
:type: no value |
| 181 |
|
| 182 |
Indicate the class is a final class. |
| 183 |
|
| 184 |
.. versionadded:: 3.1 |
| 185 |
|
| 186 |
.. rst::directive:option:: module |
| 187 |
:type: text |
| 188 |
|
| 189 |
Describe the location where the object is defined. The default value is |
| 190 |
the module specified by :rst:dir:`py:currentmodule`. |
| 191 |
|
| 192 |
.. rst:directive:option:: single-line-parameter-list |
| 193 |
:type: no value |
| 194 |
|
| 195 |
See :rst:dir:`py:class:single-line-parameter-list`. |
| 196 |
|
| 197 |
.. versionadded:: 7.1 |
| 198 |
|
| 199 |
.. rst:directive:option:: single-line-type-parameter-list |
| 200 |
:type: no value |
| 201 |
|
| 202 |
See :rst:dir:`py:class:single-line-type-parameter-list`. |
| 203 |
|
| 204 |
.. versionadded:: 7.1 |
| 205 |
|
| 206 |
.. rst:directive:: .. py:class:: name |
| 207 |
.. py:class:: name(parameters) |
| 208 |
.. py:class:: name[type parameters](parameters) |
| 209 |
|
| 210 |
Describes a class. |
| 211 |
The signature can optionally include type parameters (see :pep:`695`) |
| 212 |
or parentheses with parameters which will be shown as the constructor arguments. |
| 213 |
See also :ref:`signatures`. |
| 214 |
|
| 215 |
Methods and attributes belonging to the class should be placed in this |
| 216 |
directive's body. If they are placed outside, the supplied name should |
| 217 |
contain the class name so that cross-references still work. Example:: |
| 218 |
|
| 219 |
.. py:class:: Foo |
| 220 |
|
| 221 |
.. py:method:: quux() |
| 222 |
|
| 223 |
-- or -- |
| 224 |
|
| 225 |
.. py:class:: Bar |
| 226 |
|
| 227 |
.. py:method:: Bar.quux() |
| 228 |
|
| 229 |
The first way is the preferred one. |
| 230 |
|
| 231 |
.. rubric:: options |
| 232 |
|
| 233 |
.. rst:directive:option:: abstract |
| 234 |
:type: no value |
| 235 |
|
| 236 |
Indicate that the class is an abstract base class. |
| 237 |
This produces the following output: |
| 238 |
|
| 239 |
.. py:class:: Cheese |
| 240 |
:no-index: |
| 241 |
:abstract: |
| 242 |
|
| 243 |
A cheesy representation. |
| 244 |
|
| 245 |
.. versionadded:: 8.2 |
| 246 |
|
| 247 |
.. rst:directive:option:: canonical |
| 248 |
:type: full qualified name including module name |
| 249 |
|
| 250 |
Describe the location where the object is defined if the object is |
| 251 |
imported from other modules |
| 252 |
|
| 253 |
.. versionadded:: 4.0 |
| 254 |
|
| 255 |
.. rst:directive:option:: final |
| 256 |
:type: no value |
| 257 |
|
| 258 |
Indicate the class is a final class. |
| 259 |
|
| 260 |
.. versionadded:: 3.1 |
| 261 |
|
| 262 |
.. rst::directive:option:: module |
| 263 |
:type: text |
| 264 |
|
| 265 |
Describe the location where the object is defined. The default value is |
| 266 |
the module specified by :rst:dir:`py:currentmodule`. |
| 267 |
|
| 268 |
.. rst:directive:option:: single-line-parameter-list |
| 269 |
:type: no value |
| 270 |
|
| 271 |
Ensures that the class constructor's arguments will be emitted on a single |
| 272 |
logical line, overriding :confval:`python_maximum_signature_line_length` |
| 273 |
and :confval:`maximum_signature_line_length`. |
| 274 |
|
| 275 |
.. versionadded:: 7.1 |
| 276 |
|
| 277 |
.. rst:directive:option:: single-line-type-parameter-list |
| 278 |
:type: no value |
| 279 |
|
| 280 |
Ensure that the class type parameters are emitted on a single logical |
| 281 |
line, overriding :confval:`python_maximum_signature_line_length` and |
| 282 |
:confval:`maximum_signature_line_length`. |
| 283 |
|
| 284 |
.. rst:directive:: .. py:attribute:: name |
| 285 |
|
| 286 |
Describes an object data attribute. The description should include |
| 287 |
information about the type of the data to be expected and whether it may be |
| 288 |
changed directly. |
| 289 |
Type aliases should be documented with :rst:dir:`py:type`. |
| 290 |
|
| 291 |
.. rubric:: options |
| 292 |
|
| 293 |
.. rst:directive:option:: type: type of the attribute |
| 294 |
:type: text |
| 295 |
|
| 296 |
This will be parsed as a Python expression for cross-referencing |
| 297 |
the type annotation. |
| 298 |
As such, the argument to ``:type:`` should be a valid `annotation expression`_. |
| 299 |
|
| 300 |
.. caution:: |
| 301 |
The valid syntax for the ``:type:`` directive option differs from |
| 302 |
the syntax for the ``:type:`` `info field <info-field-lists_>`__. |
| 303 |
The ``:type:`` directive option does not understand |
| 304 |
reStructuredText markup or the ``or`` or ``of`` keywords, |
| 305 |
meaning unions must use ``|`` and sequences must use square brackets, |
| 306 |
and roles such as ``:ref:`...``` cannot be used. |
| 307 |
|
| 308 |
.. versionadded:: 2.4 |
| 309 |
|
| 310 |
.. rst:directive:option:: value: initial value of the attribute |
| 311 |
:type: text |
| 312 |
|
| 313 |
.. versionadded:: 2.4 |
| 314 |
|
| 315 |
.. rst:directive:option:: canonical |
| 316 |
:type: full qualified name including module name |
| 317 |
|
| 318 |
Describe the location where the object is defined if the object is |
| 319 |
imported from other modules |
| 320 |
|
| 321 |
.. versionadded:: 4.0 |
| 322 |
|
| 323 |
.. rst::directive:option:: module |
| 324 |
:type: text |
| 325 |
|
| 326 |
Describe the location where the object is defined. The default value is |
| 327 |
the module specified by :rst:dir:`py:currentmodule`. |
| 328 |
|
| 329 |
.. rst:directive:: .. py:property:: name |
| 330 |
|
| 331 |
Describes an object property. |
| 332 |
|
| 333 |
.. versionadded:: 4.0 |
| 334 |
|
| 335 |
.. rubric:: options |
| 336 |
|
| 337 |
.. rst:directive:option:: abstract |
| 338 |
abstractmethod |
| 339 |
:type: no value |
| 340 |
|
| 341 |
Indicate the property is abstract. |
| 342 |
This produces the following output: |
| 343 |
|
| 344 |
.. py:property:: Cheese.amount_in_stock |
| 345 |
:no-index: |
| 346 |
:abstractmethod: |
| 347 |
|
| 348 |
Cheese levels at the *National Cheese Emporium*. |
| 349 |
|
| 350 |
.. versionchanged:: 8.2 |
| 351 |
|
| 352 |
The ``:abstract:`` alias is also supported. |
| 353 |
|
| 354 |
.. rst:directive:option:: classmethod |
| 355 |
:type: no value |
| 356 |
|
| 357 |
Indicate the property is a classmethod. |
| 358 |
|
| 359 |
.. versionadded:: 4.2 |
| 360 |
|
| 361 |
.. rst:directive:option:: type: type of the property |
| 362 |
:type: text |
| 363 |
|
| 364 |
This will be parsed as a Python expression for cross-referencing |
| 365 |
the type annotation. |
| 366 |
As such, the argument to ``:type:`` should be a valid `annotation expression`_. |
| 367 |
|
| 368 |
.. caution:: |
| 369 |
The valid syntax for the ``:type:`` directive option differs from |
| 370 |
the syntax for the ``:type:`` `info field <info-field-lists_>`__. |
| 371 |
The ``:type:`` directive option does not understand |
| 372 |
reStructuredText markup or the ``or`` or ``of`` keywords, |
| 373 |
meaning unions must use ``|`` and sequences must use square brackets, |
| 374 |
and roles such as ``:ref:`...``` cannot be used. |
| 375 |
|
| 376 |
.. rst::directive:option:: module |
| 377 |
:type: text |
| 378 |
|
| 379 |
Describe the location where the object is defined. The default value is |
| 380 |
the module specified by :rst:dir:`py:currentmodule`. |
| 381 |
|
| 382 |
.. rst:directive:: .. py:type:: name |
| 383 |
|
| 384 |
Describe a :ref:`type alias <python:type-aliases>`. |
| 385 |
|
| 386 |
The type that the alias represents should be described |
| 387 |
with the :rst:dir:`!canonical` option. |
| 388 |
This directive supports an optional description body. |
| 389 |
|
| 390 |
For example: |
| 391 |
|
| 392 |
.. code-block:: rst |
| 393 |
|
| 394 |
.. py:type:: UInt64 |
| 395 |
|
| 396 |
Represent a 64-bit positive integer. |
| 397 |
|
| 398 |
will be rendered as follows: |
| 399 |
|
| 400 |
.. py:type:: UInt64 |
| 401 |
:no-contents-entry: |
| 402 |
:no-index-entry: |
| 403 |
|
| 404 |
Represent a 64-bit positive integer. |
| 405 |
|
| 406 |
.. rubric:: options |
| 407 |
|
| 408 |
.. rst:directive:option:: canonical |
| 409 |
:type: text |
| 410 |
|
| 411 |
The canonical type represented by this alias, for example: |
| 412 |
|
| 413 |
.. code-block:: rst |
| 414 |
|
| 415 |
.. py:type:: StrPattern |
| 416 |
:canonical: str | re.Pattern[str] |
| 417 |
|
| 418 |
Represent a regular expression or a compiled pattern. |
| 419 |
|
| 420 |
This is rendered as: |
| 421 |
|
| 422 |
.. py:type:: StrPattern |
| 423 |
:no-contents-entry: |
| 424 |
:no-index-entry: |
| 425 |
:canonical: str | re.Pattern[str] |
| 426 |
|
| 427 |
Represent a regular expression or a compiled pattern. |
| 428 |
|
| 429 |
.. versionadded:: 7.4 |
| 430 |
|
| 431 |
.. rst:directive:: .. py:method:: name(parameters) |
| 432 |
.. py:method:: name[type parameters](parameters) |
| 433 |
|
| 434 |
Describes an object method. The parameters should not include the ``self`` |
| 435 |
parameter. The description should include similar information to that |
| 436 |
described for ``function``. See also :ref:`signatures` and |
| 437 |
:ref:`info-field-lists`. |
| 438 |
|
| 439 |
.. rubric:: options |
| 440 |
|
| 441 |
.. rst:directive:option:: abstract |
| 442 |
abstractmethod |
| 443 |
:type: no value |
| 444 |
|
| 445 |
Indicate the method is an abstract method. |
| 446 |
This produces the following output: |
| 447 |
|
| 448 |
.. py:method:: Cheese.order_more_stock |
| 449 |
:no-index: |
| 450 |
:abstractmethod: |
| 451 |
|
| 452 |
Order more cheese (we're fresh out!). |
| 453 |
|
| 454 |
.. versionadded:: 2.1 |
| 455 |
.. versionchanged:: 8.2 |
| 456 |
|
| 457 |
The ``:abstract:`` alias is also supported. |
| 458 |
|
| 459 |
.. rst:directive:option:: async |
| 460 |
:type: no value |
| 461 |
|
| 462 |
Indicate the method is an async method. |
| 463 |
|
| 464 |
.. versionadded:: 2.1 |
| 465 |
|
| 466 |
.. rst:directive:option:: canonical |
| 467 |
:type: full qualified name including module name |
| 468 |
|
| 469 |
Describe the location where the object is defined if the object is |
| 470 |
imported from other modules |
| 471 |
|
| 472 |
.. versionadded:: 4.0 |
| 473 |
|
| 474 |
.. rst:directive:option:: classmethod |
| 475 |
:type: no value |
| 476 |
|
| 477 |
Indicate the method is a class method. |
| 478 |
|
| 479 |
.. versionadded:: 2.1 |
| 480 |
|
| 481 |
.. rst:directive:option:: final |
| 482 |
:type: no value |
| 483 |
|
| 484 |
Indicate the method is a final method. |
| 485 |
|
| 486 |
.. versionadded:: 3.1 |
| 487 |
|
| 488 |
.. rst::directive:option:: module |
| 489 |
:type: text |
| 490 |
|
| 491 |
Describe the location where the object is defined. The default value is |
| 492 |
the module specified by :rst:dir:`py:currentmodule`. |
| 493 |
|
| 494 |
.. rst:directive:option:: single-line-parameter-list |
| 495 |
:type: no value |
| 496 |
|
| 497 |
Ensures that the method's arguments will be emitted on a single logical |
| 498 |
line, overriding :confval:`python_maximum_signature_line_length` and |
| 499 |
:confval:`maximum_signature_line_length`. |
| 500 |
|
| 501 |
.. versionadded:: 7.1 |
| 502 |
|
| 503 |
.. rst:directive:option:: single-line-type-parameter-list |
| 504 |
:type: no value |
| 505 |
|
| 506 |
Ensure that the method's type parameters are emitted on a single logical |
| 507 |
line, overriding :confval:`python_maximum_signature_line_length` and |
| 508 |
:confval:`maximum_signature_line_length`. |
| 509 |
|
| 510 |
.. versionadded:: 7.2 |
| 511 |
|
| 512 |
.. rst:directive:option:: staticmethod |
| 513 |
:type: no value |
| 514 |
|
| 515 |
Indicate the method is a static method. |
| 516 |
|
| 517 |
.. versionadded:: 2.1 |
| 518 |
|
| 519 |
|
| 520 |
.. rst:directive:: .. py:staticmethod:: name(parameters) |
| 521 |
.. py:staticmethod:: name[type parameters](parameters) |
| 522 |
|
| 523 |
Like :rst:dir:`py:method`, but indicates that the method is a static method. |
| 524 |
|
| 525 |
.. versionadded:: 0.4 |
| 526 |
|
| 527 |
.. rst:directive:: .. py:classmethod:: name(parameters) |
| 528 |
.. py:classmethod:: name[type parameters](parameters) |
| 529 |
|
| 530 |
Like :rst:dir:`py:method`, but indicates that the method is a class method. |
| 531 |
|
| 532 |
.. versionadded:: 0.6 |
| 533 |
|
| 534 |
.. rst:directive:: .. py:decorator:: name |
| 535 |
.. py:decorator:: name(parameters) |
| 536 |
.. py:decorator:: name[type parameters](parameters) |
| 537 |
|
| 538 |
Describes a decorator function. The signature should represent the usage as |
| 539 |
a decorator. For example, given the functions |
| 540 |
|
| 541 |
.. code-block:: python |
| 542 |
|
| 543 |
def removename(func): |
| 544 |
func.__name__ = '' |
| 545 |
return func |
| 546 |
|
| 547 |
def setnewname(name): |
| 548 |
def decorator(func): |
| 549 |
func.__name__ = name |
| 550 |
return func |
| 551 |
return decorator |
| 552 |
|
| 553 |
the descriptions should look like this:: |
| 554 |
|
| 555 |
.. py:decorator:: removename |
| 556 |
|
| 557 |
Remove name of the decorated function. |
| 558 |
|
| 559 |
.. py:decorator:: setnewname(name) |
| 560 |
|
| 561 |
Set name of the decorated function to *name*. |
| 562 |
|
| 563 |
(as opposed to ``.. py:decorator:: removename(func)``.) |
| 564 |
|
| 565 |
Refer to a decorator function using the :rst:role:`py:deco` role. |
| 566 |
|
| 567 |
.. rst:directive:option:: single-line-parameter-list |
| 568 |
:type: no value |
| 569 |
|
| 570 |
Ensures that the decorator's arguments will be emitted on a single logical |
| 571 |
line, overriding :confval:`python_maximum_signature_line_length` and |
| 572 |
:confval:`maximum_signature_line_length`. |
| 573 |
|
| 574 |
.. versionadded:: 7.1 |
| 575 |
|
| 576 |
.. rst:directive:option:: single-line-type-parameter-list |
| 577 |
:type: no value |
| 578 |
|
| 579 |
Ensure that the decorator's type parameters are emitted on a single |
| 580 |
logical line, overriding :confval:`python_maximum_signature_line_length` |
| 581 |
and :confval:`maximum_signature_line_length`. |
| 582 |
|
| 583 |
.. versionadded:: 7.2 |
| 584 |
|
| 585 |
.. rst:directive:: .. py:decoratormethod:: name |
| 586 |
.. py:decoratormethod:: name(signature) |
| 587 |
.. py:decoratormethod:: name[type parameters](signature) |
| 588 |
|
| 589 |
Same as :rst:dir:`py:decorator`, but for decorators that are methods. |
| 590 |
|
| 591 |
Refer to a decorator method using the :rst:role:`py:deco` role. |
| 592 |
|
| 593 |
.. _annotation expression: https://typing.readthedocs.io/en/latest/spec/annotations.html#type-and-annotation-expressions |
| 594 |
|
| 595 |
.. _signatures: |
| 596 |
|
| 597 |
Python Signatures |
| 598 |
----------------- |
| 599 |
|
| 600 |
Signatures of functions, methods and class constructors can be given like they |
| 601 |
would be written in Python. |
| 602 |
This can include default values, positional-only or keyword-only parameters, |
| 603 |
type annotations, and type parameters. |
| 604 |
For example: |
| 605 |
|
| 606 |
.. code-block:: rst |
| 607 |
|
| 608 |
.. py:function:: compile(source: str, filename: Path, symbol: str = 'file') -> ast.AST |
| 609 |
|
| 610 |
.. py:function:: compile(source: str, filename: Path, symbol: str = 'file') -> ast.AST |
| 611 |
:no-index: |
| 612 |
|
| 613 |
For functions with optional parameters that don't have default values |
| 614 |
(typically functions implemented in C extension modules without keyword |
| 615 |
argument support), |
| 616 |
you can list multiple versions of the same signature in a single directive: |
| 617 |
|
| 618 |
.. py:function:: compile(source) |
| 619 |
compile(source, filename) |
| 620 |
compile(source, filename, symbol) |
| 621 |
:no-index: |
| 622 |
|
| 623 |
Another approach is to use square brackets to specify the optional parts. |
| 624 |
When using square brackets, it is customary to place |
| 625 |
the opening bracket before the comma (``[,``). |
| 626 |
|
| 627 |
.. py:function:: compile(source[, filename[, symbol]]) |
| 628 |
:no-index: |
| 629 |
|
| 630 |
Python 3.12 introduced *type parameters*, which are type variables |
| 631 |
declared directly within the class or function definition: |
| 632 |
|
| 633 |
.. code-block:: python |
| 634 |
|
| 635 |
class AnimalList[AnimalT](list[AnimalT]): |
| 636 |
... |
| 637 |
|
| 638 |
def add[T](a: T, b: T) -> T: |
| 639 |
return a + b |
| 640 |
|
| 641 |
The corresponding reStructuredText markup would be: |
| 642 |
|
| 643 |
.. code-block:: rst |
| 644 |
|
| 645 |
.. py:class:: AnimalList[AnimalT] |
| 646 |
|
| 647 |
.. py:function:: add[T](a: T, b: T) -> T |
| 648 |
|
| 649 |
.. seealso:: |
| 650 |
|
| 651 |
:pep:`695` and :pep:`696`, for details and the full specification. |
| 652 |
|
| 653 |
|
| 654 |
.. _info-field-lists: |
| 655 |
|
| 656 |
Info field lists |
| 657 |
---------------- |
| 658 |
|
| 659 |
.. versionadded:: 0.4 |
| 660 |
.. versionchanged:: 3.0 |
| 661 |
|
| 662 |
meta fields are added. |
| 663 |
|
| 664 |
Inside Python object description directives, |
| 665 |
reStructuredText field lists with these fields |
| 666 |
are recognized and formatted nicely: |
| 667 |
|
| 668 |
* ``param``, ``parameter``, ``arg``, ``argument``, ``key``, ``keyword``: |
| 669 |
Description of a parameter. |
| 670 |
* ``type``: Type of a parameter. Creates a link if possible. |
| 671 |
* ``raises``, ``raise``, ``except``, ``exception``: That (and when) a specific |
| 672 |
exception is raised. |
| 673 |
* ``var``, ``ivar``, ``cvar``: Description of a variable. |
| 674 |
* ``vartype``: Type of a variable. Creates a link if possible. |
| 675 |
* ``returns``, ``return``: Description of the return value. |
| 676 |
* ``rtype``: Return type. Creates a link if possible. |
| 677 |
* ``meta``: Add metadata to description of the python object. The metadata will |
| 678 |
not be shown on output document. For example, ``:meta private:`` indicates |
| 679 |
the python object is private member. It is used in |
| 680 |
:py:mod:`sphinx.ext.autodoc` for filtering members. |
| 681 |
|
| 682 |
.. note:: |
| 683 |
|
| 684 |
In current release, all ``var``, ``ivar`` and ``cvar`` are represented as |
| 685 |
"Variable". There is no difference at all. |
| 686 |
|
| 687 |
The field names must consist of one of these keywords and an argument (except |
| 688 |
for ``returns`` and ``rtype``, which do not need an argument). This is best |
| 689 |
explained by an example:: |
| 690 |
|
| 691 |
.. py:function:: send_message(sender, recipient, message_body, [priority=1]) |
| 692 |
|
| 693 |
Send a message to a recipient |
| 694 |
|
| 695 |
:param str sender: The person sending the message |
| 696 |
:param str recipient: The recipient of the message |
| 697 |
:param str message_body: The body of the message |
| 698 |
:param priority: The priority of the message, can be a number 1-5 |
| 699 |
:type priority: int or None |
| 700 |
:return: the message id |
| 701 |
:rtype: int |
| 702 |
:raises ValueError: if the message_body exceeds 160 characters |
| 703 |
:raises TypeError: if the message_body is not a basestring |
| 704 |
|
| 705 |
This will render like this: |
| 706 |
|
| 707 |
.. py:function:: send_message(sender, recipient, message_body, [priority=1]) |
| 708 |
:no-contents-entry: |
| 709 |
:no-index-entry: |
| 710 |
|
| 711 |
Send a message to a recipient |
| 712 |
|
| 713 |
:param str sender: The person sending the message |
| 714 |
:param str recipient: The recipient of the message |
| 715 |
:param str message_body: The body of the message |
| 716 |
:param priority: The priority of the message, can be a number 1-5 |
| 717 |
:type priority: int or None |
| 718 |
:return: the message id |
| 719 |
:rtype: int |
| 720 |
:raises ValueError: if the message_body exceeds 160 characters |
| 721 |
:raises TypeError: if the message_body is not a basestring |
| 722 |
|
| 723 |
It is also possible to combine parameter type and description, if the type is a |
| 724 |
single word, like this:: |
| 725 |
|
| 726 |
:param int priority: The priority of the message, can be a number 1-5 |
| 727 |
|
| 728 |
.. versionadded:: 1.5 |
| 729 |
|
| 730 |
Container types such as lists and dictionaries can be linked automatically |
| 731 |
using the following syntax:: |
| 732 |
|
| 733 |
:type priorities: list(int) |
| 734 |
:type priorities: list[int] |
| 735 |
:type mapping: dict(str, int) |
| 736 |
:type mapping: dict[str, int] |
| 737 |
:type point: tuple(float, float) |
| 738 |
:type point: tuple[float, float] |
| 739 |
|
| 740 |
Multiple types in a type field will be linked automatically |
| 741 |
if separated by either the vertical bar (``|``) or the word "or":: |
| 742 |
|
| 743 |
:type an_arg: int or None |
| 744 |
:vartype a_var: str or int |
| 745 |
:rtype: float or str |
| 746 |
|
| 747 |
:type an_arg: int | None |
| 748 |
:vartype a_var: str | int |
| 749 |
:rtype: float | str |
| 750 |
|
| 751 |
.. _python-xref-roles: |
| 752 |
|
| 753 |
Cross-referencing Python objects |
| 754 |
-------------------------------- |
| 755 |
|
| 756 |
The following roles refer to objects in modules and are possibly hyperlinked if |
| 757 |
a matching identifier is found: |
| 758 |
|
| 759 |
.. rst:role:: py:mod |
| 760 |
|
| 761 |
Reference a module; a dotted name may be used. This should also be used for |
| 762 |
package names. |
| 763 |
|
| 764 |
.. rst:role:: py:func |
| 765 |
|
| 766 |
Reference a Python function; dotted names may be used. The role text needs |
| 767 |
not include trailing parentheses to enhance readability; they will be added |
| 768 |
automatically by Sphinx if the :confval:`add_function_parentheses` config |
| 769 |
value is ``True`` (the default). |
| 770 |
|
| 771 |
.. rst:role:: py:deco |
| 772 |
|
| 773 |
Reference a Python decorator; dotted names may be used. |
| 774 |
The rendered output will be prepended with an at-sign (``@``), |
| 775 |
for example: ``:py:deco:`removename``` produces :py:deco:`removename`. |
| 776 |
|
| 777 |
.. py:decorator:: removename |
| 778 |
:no-contents-entry: |
| 779 |
:no-index-entry: |
| 780 |
:no-typesetting: |
| 781 |
|
| 782 |
.. rst:role:: py:data |
| 783 |
|
| 784 |
Reference a module-level variable. |
| 785 |
|
| 786 |
.. rst:role:: py:const |
| 787 |
|
| 788 |
Reference a "defined" constant. This may be a Python variable that is not |
| 789 |
intended to be changed. |
| 790 |
|
| 791 |
.. rst:role:: py:class |
| 792 |
|
| 793 |
Reference a class; a dotted name may be used. |
| 794 |
|
| 795 |
.. rst:role:: py:meth |
| 796 |
|
| 797 |
Reference a method of an object. The role text can include the type name |
| 798 |
and the method name; if it occurs within the description of a type, the type |
| 799 |
name can be omitted. A dotted name may be used. |
| 800 |
|
| 801 |
.. rst:role:: py:attr |
| 802 |
|
| 803 |
Reference a data attribute of an object. |
| 804 |
|
| 805 |
.. note:: The role is also able to refer to property. |
| 806 |
|
| 807 |
.. rst:role:: py:type |
| 808 |
|
| 809 |
Reference a type alias. |
| 810 |
|
| 811 |
.. rst:role:: py:exc |
| 812 |
|
| 813 |
Reference an exception. A dotted name may be used. |
| 814 |
|
| 815 |
.. rst:role:: py:obj |
| 816 |
|
| 817 |
Reference an object of unspecified type. Useful e.g. as the |
| 818 |
:confval:`default_role`. |
| 819 |
|
| 820 |
.. versionadded:: 0.4 |
| 821 |
|
| 822 |
|
| 823 |
Target specification |
| 824 |
^^^^^^^^^^^^^^^^^^^^ |
| 825 |
|
| 826 |
The target can be specified as a fully qualified name |
| 827 |
(e.g. ``:py:meth:`my_module.MyClass.my_method```) |
| 828 |
or any shortened version |
| 829 |
(e.g. ``:py:meth:`MyClass.my_method``` or ``:py:meth:`my_method```). |
| 830 |
See `target resolution`_ for details on the resolution of shortened names. |
| 831 |
|
| 832 |
:ref:`Cross-referencing modifiers <xref-modifiers>` can be applied. |
| 833 |
In short: |
| 834 |
|
| 835 |
* You may supply an explicit title and reference target: |
| 836 |
``:py:mod:`mathematical functions <math>``` will refer to the ``math`` module, |
| 837 |
but the link text will be "mathematical functions". |
| 838 |
|
| 839 |
* If you prefix the content with an exclamation mark (``!``), |
| 840 |
no reference/hyperlink will be created. |
| 841 |
|
| 842 |
* If you prefix the content with ``~``, the link text will only be the last |
| 843 |
component of the target. |
| 844 |
For example, ``:py:meth:`~queue.Queue.get``` will |
| 845 |
refer to ``queue.Queue.get`` but only display ``get`` as the link text. |
| 846 |
|
| 847 |
|
| 848 |
Target resolution |
| 849 |
^^^^^^^^^^^^^^^^^ |
| 850 |
|
| 851 |
A given link target name is resolved to an object using the following strategy: |
| 852 |
|
| 853 |
Names in these roles are searched first without any further qualification, |
| 854 |
then with the current module name prepended, |
| 855 |
then with the current module and class name (if any) prepended. |
| 856 |
|
| 857 |
If you prefix the name with a dot (``.``), this order is reversed. |
| 858 |
For example, in the documentation of Python's :py:mod:`codecs` module, |
| 859 |
``:py:func:`open``` always refers to the built-in function, |
| 860 |
while ``:py:func:`.open``` refers to :func:`codecs.open`. |
| 861 |
|
| 862 |
A similar heuristic is used to determine whether the name is an attribute of |
| 863 |
the currently documented class. |
| 864 |
|
| 865 |
Also, if the name is prefixed with a dot, and no exact match is found, the |
| 866 |
target is taken as a suffix and all object names with that suffix are searched. |
| 867 |
For example, ``:py:meth:`.TarFile.close``` references the |
| 868 |
``tarfile.TarFile.close()`` function, even if the current module is not |
| 869 |
``tarfile``. Since this can get ambiguous, if there is more than one possible |
| 870 |
match, you will get a warning from Sphinx. |
| 871 |
|
| 872 |
Note that you can combine the ``~`` and ``.`` prefixes: |
| 873 |
``:py:meth:`~.TarFile.close``` will reference the ``tarfile.TarFile.close()`` |
| 874 |
method, but the visible link caption will only be ``close()``. |