Debian Codegraph @

docs/restructuredtext_demo/demo_restructuredtext_module.py

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