| 1 |
""" |
| 2 |
This is a module demonstrating epydoc code documentation features. |
| 3 |
|
| 4 |
Most part of this documentation is using Python type hinting. |
| 5 |
""" |
| 6 |
|
| 7 |
from abc import ABC |
| 8 |
import math |
| 9 |
from typing import overload, AnyStr, Dict, Generator, List, Union, Callable, Tuple, TYPE_CHECKING |
| 10 |
from somelib import SomeInterface |
| 11 |
import zope.interface |
| 12 |
import zope.schema |
| 13 |
from typing import Sequence, Optional |
| 14 |
from incremental import Version |
| 15 |
from twisted.python.deprecate import deprecated, deprecatedProperty |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from typing_extensions import Final |
| 19 |
|
| 20 |
Parser = Callable[[str], Tuple[int, bytes, bytes]] |
| 21 |
""" |
| 22 |
Type aliases are documented as such and their value is shown just like constants. |
| 23 |
""" |
| 24 |
|
| 25 |
LANG = 'Fr' |
| 26 |
""" |
| 27 |
This is a constant. See L{constants} for more examples. |
| 28 |
""" |
| 29 |
|
| 30 |
lang: 'Final[Sequence[str]]' = ['Fr', 'En'] |
| 31 |
""" |
| 32 |
This is also a constant, but annotated with typing.Final. |
| 33 |
""" |
| 34 |
|
| 35 |
@deprecated(Version("demo", "NEXT", 0, 0), replacement=math.prod) |
| 36 |
def demo_product_deprecated(x, y) -> float: # type: ignore |
| 37 |
return float(x * y) |
| 38 |
|
| 39 |
def demo_fields_docstring_arguments(m, b): # type: ignore |
| 40 |
""" |
| 41 |
Fields are used to describe specific properties of a documented object. |
| 42 |
|
| 43 |
This function can be used in conjunction with L{demo_typing_arguments} to |
| 44 |
find an arbitrary function's zeros. |
| 45 |
|
| 46 |
@type m: number |
| 47 |
@param m: The slope of the line. |
| 48 |
@type b: number |
| 49 |
@param b: The y intercept of the line. |
| 50 |
@rtype: number |
| 51 |
@return: the x intercept of the line M{y=m*x+b}. |
| 52 |
""" |
| 53 |
return -b/m |
| 54 |
|
| 55 |
def demo_typing_arguments(name: str, size: Optional[bytes] = None) -> bool: |
| 56 |
""" |
| 57 |
Type documentation can be extracted from standard Python type hints. |
| 58 |
|
| 59 |
@param name: The human readable name for something. |
| 60 |
@param size: How big the name should be. Leave none if you don't care. |
| 61 |
@return: Always C{True}. |
| 62 |
""" |
| 63 |
return True |
| 64 |
|
| 65 |
def demo_long_function_and_parameter_names__this_indeed_very_long( |
| 66 |
this_is_a_very_long_parameter_name_aahh: str, |
| 67 |
what__another_super_super_long_name__ho_no: Generator[Union[List[AnyStr], Dict[str, AnyStr]], None, None]) -> bool: |
| 68 |
""" |
| 69 |
Long names and annotations should display on several lines when they don't fit in a single line. |
| 70 |
""" |
| 71 |
return True |
| 72 |
|
| 73 |
def demo_cross_reference() -> None: |
| 74 |
""" |
| 75 |
The inline markup construct C{LE{lb}text<object>E{rb}} is used to create links to the documentation for other Python objects. |
| 76 |
'text' is the text that should be displayed for the link, and 'object' is the name of the Python object that should be linked to. |
| 77 |
|
| 78 |
If you wish to use the name of the Python object as the text for the link, you can simply write C{LE{lb}objectE{rb}}. |
| 79 |
|
| 80 |
- L{demo_typing_arguments} |
| 81 |
- L{Custom name <demo_typing_arguments>} |
| 82 |
""" |
| 83 |
|
| 84 |
@overload |
| 85 |
def demo_overload(s: str) -> str: |
| 86 |
... |
| 87 |
|
| 88 |
@overload |
| 89 |
def demo_overload(s: bytes) -> bytes: |
| 90 |
... |
| 91 |
|
| 92 |
def demo_overload(s: Union[str, bytes]) -> Union[str, bytes]: |
| 93 |
""" |
| 94 |
Overload signatures appear without the main signature and with C{@overload} decorator. |
| 95 |
|
| 96 |
@param s: Some string or bytes param. |
| 97 |
@return: Some string or bytes result. |
| 98 |
""" |
| 99 |
raise NotImplementedError |
| 100 |
|
| 101 |
def demo_undocumented(s: str) -> str: |
| 102 |
raise NotImplementedError |
| 103 |
|
| 104 |
|
| 105 |
class _PrivateClass: |
| 106 |
""" |
| 107 |
This is the docstring of a private class. |
| 108 |
""" |
| 109 |
|
| 110 |
def method_inside_private(self) -> bool: |
| 111 |
""" |
| 112 |
A public method inside a private class. |
| 113 |
|
| 114 |
@return: Something. |
| 115 |
""" |
| 116 |
return True |
| 117 |
|
| 118 |
|
| 119 |
def _private_inside_private(self) -> bool: |
| 120 |
""" |
| 121 |
A private method inside a private class. |
| 122 |
|
| 123 |
@return: Something. |
| 124 |
""" |
| 125 |
return True |
| 126 |
|
| 127 |
|
| 128 |
class DemoClass(ABC, SomeInterface, _PrivateClass): |
| 129 |
""" |
| 130 |
This is the docstring of this class. |
| 131 |
""" |
| 132 |
|
| 133 |
def __init__(self, one: str, two: bytes) -> None: |
| 134 |
""" |
| 135 |
Documentation for class initialization. |
| 136 |
|
| 137 |
@param one: Docs for first argument. |
| 138 |
@param two: Docs for second argument. |
| 139 |
""" |
| 140 |
|
| 141 |
@property |
| 142 |
def read_only(self) -> int: |
| 143 |
""" |
| 144 |
This is a read-only property. |
| 145 |
""" |
| 146 |
return 1 |
| 147 |
|
| 148 |
@deprecatedProperty(Version("demo", 1, 3, 0), replacement=read_only) |
| 149 |
def read_only_deprecated(self) -> int: |
| 150 |
""" |
| 151 |
This is a deprecated read-only property. |
| 152 |
""" |
| 153 |
return 1 |
| 154 |
|
| 155 |
@property |
| 156 |
def read_and_write(self) -> int: |
| 157 |
""" |
| 158 |
This is a read-write property. |
| 159 |
""" |
| 160 |
return 1 |
| 161 |
|
| 162 |
@read_and_write.setter |
| 163 |
def read_and_write(self, value: int) -> None: |
| 164 |
""" |
| 165 |
This is a docstring for setter. |
| 166 |
""" |
| 167 |
|
| 168 |
@property |
| 169 |
def read_and_write_delete(self) -> int: |
| 170 |
""" |
| 171 |
This is a read-write-delete property. |
| 172 |
""" |
| 173 |
return 1 |
| 174 |
|
| 175 |
@read_and_write_delete.setter |
| 176 |
def read_and_write_delete(self, value: int) -> None: |
| 177 |
""" |
| 178 |
This is a docstring for setter. |
| 179 |
""" |
| 180 |
|
| 181 |
@read_and_write_delete.deleter |
| 182 |
def read_and_write_delete(self) -> None: |
| 183 |
""" |
| 184 |
This is a docstring for deleter. |
| 185 |
""" |
| 186 |
|
| 187 |
|
| 188 |
class IContact(zope.interface.Interface): |
| 189 |
""" |
| 190 |
Example of an interface with schemas. |
| 191 |
|
| 192 |
Provides access to basic contact information. |
| 193 |
""" |
| 194 |
|
| 195 |
first = zope.schema.TextLine(description="First name") |
| 196 |
|
| 197 |
email = zope.schema.TextLine(description="Electronic mail address") |
| 198 |
|
| 199 |
address = zope.schema.Text(description="Postal address") |
| 200 |
|
| 201 |
def send_email(text: str) -> None: |
| 202 |
pass |