Skip to content

google

This module defines functions and classes to parse docstrings into structured data.

RE_DOCTEST_BLANKLINE = re.compile('^\\s*<BLANKLINE>\\s*$') module-attribute ¤

Regular expression to match lines of the form <BLANKLINE>.

RE_DOCTEST_FLAGS = re.compile('(\\s*#\\s*doctest:.+)$') module-attribute ¤

Regular expression to match lines containing doctest flags of the form # doctest: +FLAG.

RE_GOOGLE_STYLE_ADMONITION = re.compile('^(?P<indent>\\s*)(?P<type>[\\w-]+):((?:\\s+)(?P<title>.+))?$') module-attribute ¤

Regular expressions to match lines starting admonitions, of the form TYPE: [TITLE].

Google ¤

Bases: Parser

A Google-style docstrings parser.

Source code in src/pytkdocs/parsers/docstrings/google.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
class Google(Parser):
    """A Google-style docstrings parser."""

    def __init__(self, replace_admonitions: bool = True, trim_doctest_flags: bool = True, **kwargs: Any) -> None:  # noqa: FBT001, FBT002, ARG002
        """Initialize the object.

        Arguments:
            replace_admonitions: Whether to replace admonitions by their Markdown equivalent.
            trim_doctest_flags: Whether to remove doctest flags.
        """
        super().__init__()
        self.replace_admonitions = replace_admonitions
        self.trim_doctest_flags = trim_doctest_flags
        self.section_reader = {
            Section.Type.PARAMETERS: self.read_parameters_section,
            Section.Type.KEYWORD_ARGS: self.read_keyword_arguments_section,
            Section.Type.EXCEPTIONS: self.read_exceptions_section,
            Section.Type.EXAMPLES: self.read_examples_section,
            Section.Type.ATTRIBUTES: self.read_attributes_section,
            Section.Type.RETURN: self.read_return_section,
            Section.Type.YIELD: self.read_yield_section,
        }

    def parse_sections(self, docstring: str) -> list[Section]:  # noqa: D102
        if "signature" not in self.context:
            self.context["signature"] = getattr(self.context["obj"], "signature", None)
        if "annotation" not in self.context:
            self.context["annotation"] = getattr(self.context["obj"], "type", empty)
        if "attributes" not in self.context:
            self.context["attributes"] = {}

        sections = []
        current_section = []

        in_code_block = False

        lines = docstring.split("\n")
        i = 0

        while i < len(lines):
            line_lower = lines[i].lower()

            if in_code_block:
                if line_lower.lstrip(" ").startswith("```"):
                    in_code_block = False
                current_section.append(lines[i])

            elif line_lower in SECTIONS_TITLES:
                if current_section:
                    if any(current_section):
                        sections.append(Section(Section.Type.MARKDOWN, "\n".join(current_section)))
                    current_section = []
                section_reader = self.section_reader[SECTIONS_TITLES[line_lower]]
                section, i = section_reader(lines, i + 1)
                if section:
                    sections.append(section)

            elif line_lower.lstrip(" ").startswith("```"):
                in_code_block = True
                current_section.append(lines[i])

            else:
                if self.replace_admonitions and not in_code_block and i + 1 < len(lines):
                    match = RE_GOOGLE_STYLE_ADMONITION.match(lines[i])
                    if match:
                        groups = match.groupdict()
                        indent = groups["indent"]
                        if lines[i + 1].startswith(indent + " " * 4):
                            lines[i] = f"{indent}!!! {groups['type'].lower()}"
                            if groups["title"]:
                                lines[i] += f' "{groups["title"]}"'
                current_section.append(lines[i])

            i += 1

        if current_section:
            sections.append(Section(Section.Type.MARKDOWN, "\n".join(current_section)))

        return sections

    def read_block_items(self, lines: list[str], start_index: int) -> tuple[list[str], int]:
        """Parse an indented block as a list of items.

        The first indentation level is used as a reference to determine if the next lines are new items
        or continuation lines.

        Arguments:
            lines: The block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing the list of concatenated lines and the index at which to continue parsing.
        """
        if start_index >= len(lines):
            return [], start_index

        i = start_index
        items: list[str] = []

        # skip first empty lines
        while is_empty_line(lines[i]):
            i += 1

        # get initial indent
        indent = len(lines[i]) - len(lines[i].lstrip())

        if indent == 0:
            # first non-empty line was not indented, abort
            return [], i - 1

        # start processing first item
        current_item = [lines[i][indent:]]
        i += 1

        # loop on next lines
        while i < len(lines):
            line = lines[i]

            if line.startswith(indent * 2 * " "):
                # continuation line
                current_item.append(line[indent * 2 :])

            elif line.startswith((indent + 1) * " "):
                # indent between initial and continuation: append but add error
                cont_indent = len(line) - len(line.lstrip())
                current_item.append(line[cont_indent:])
                self.error(
                    f"Confusing indentation for continuation line {i + 1} in docstring, "
                    f"should be {indent} * 2 = {indent * 2} spaces, not {cont_indent}",
                )

            elif line.startswith(indent * " "):
                # indent equal to initial one: new item
                items.append("\n".join(current_item))
                current_item = [line[indent:]]

            elif is_empty_line(line):
                # empty line: preserve it in the current item
                current_item.append("")

            else:
                # indent lower than initial one: end of section
                break

            i += 1

        if current_item:
            items.append("\n".join(current_item).rstrip("\n"))

        return items, i - 1

    def read_block(self, lines: list[str], start_index: int) -> tuple[str, int]:
        """Parse an indented block.

        Arguments:
            lines: The block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing the list of lines and the index at which to continue parsing.
        """
        if start_index >= len(lines):
            return "", start_index

        i = start_index
        block: list[str] = []

        # skip first empty lines
        while is_empty_line(lines[i]):
            i += 1

        # get initial indent
        indent = len(lines[i]) - len(lines[i].lstrip())

        if indent == 0:
            # first non-empty line was not indented, abort
            return "", i - 1

        # start processing first item
        block.append(lines[i].lstrip())
        i += 1

        # loop on next lines
        while i < len(lines) and (lines[i].startswith(indent * " ") or is_empty_line(lines[i])):
            block.append(lines[i][indent:])
            i += 1

        return "\n".join(block).rstrip("\n"), i - 1

    def _parse_parameters_section(self, lines: list[str], start_index: int) -> tuple[list[Parameter], int]:
        """Parse a "parameters" or "keyword args" section.

        Arguments:
            lines: The parameters block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        parameters = []
        type_: Any
        block, i = self.read_block_items(lines, start_index)

        for param_line in block:
            # Check that there is an annotation in the docstring
            try:
                name_with_type, description = param_line.split(":", 1)
            except ValueError:
                self.error(f"Failed to get 'name: description' pair from '{param_line}'")
                continue

            # Setting defaults
            default = empty
            annotation = empty
            kind = None
            # Can only get description from docstring - keep if no type was given
            description = description.lstrip()

            # If we have managed to find a type in the docstring use this
            if " " in name_with_type:
                name, type_ = name_with_type.split(" ", 1)
                annotation = type_.strip("()")
                if annotation.endswith(", optional"):  # type: ignore[attr-defined]
                    annotation = annotation[:-10]  # type: ignore[misc]
            # Otherwise try to use the signature as `annotation` would still be empty
            else:
                name = name_with_type

            # Check in the signature to get extra details
            try:
                signature_param = self.context["signature"].parameters[name.lstrip("*")]
            except (AttributeError, KeyError):
                if annotation is empty:
                    self.error(f"No type annotation for parameter '{name}'")
            else:
                if annotation is empty:
                    annotation = signature_param.annotation
                # If signature_param.X are empty it doesnt matter as defaults are empty anyway
                default = signature_param.default
                kind = signature_param.kind

            parameters.append(
                Parameter(name=name, annotation=annotation, description=description, default=default, kind=kind),
            )

        return parameters, i

    def read_parameters_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse a "parameters" section.

        Arguments:
            lines: The parameters block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        parameters, i = self._parse_parameters_section(lines, start_index)

        if parameters:
            return Section(Section.Type.PARAMETERS, parameters), i

        self.error(f"Empty parameters section at line {start_index}")
        return None, i

    def read_keyword_arguments_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse a "keyword arguments" section.

        Arguments:
            lines: The parameters block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        parameters, i = self._parse_parameters_section(lines, start_index)
        for parameter in parameters:
            parameter.kind = inspect.Parameter.KEYWORD_ONLY

        if parameters:
            return Section(Section.Type.KEYWORD_ARGS, parameters), i

        self.error(f"Empty keyword arguments section at line {start_index}")
        return None, i

    def read_attributes_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse an "attributes" section.

        Arguments:
            lines: The parameters block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        attributes = []
        block, i = self.read_block_items(lines, start_index)

        for attr_line in block:
            try:
                name_with_type, description = attr_line.split(":", 1)
            except ValueError:
                self.error(f"Failed to get 'name: description' pair from '{attr_line}'")
                continue

            description = description.lstrip()

            if " " in name_with_type:
                name, annotation = name_with_type.split(" ", 1)
                annotation = annotation.strip("()")
                if annotation.endswith(", optional"):
                    annotation = annotation[:-10]
            else:
                name = name_with_type
                annotation = self.context["attributes"].get(name, {}).get("annotation", empty)

            attributes.append(Attribute(name=name, annotation=annotation, description=description))

        if attributes:
            return Section(Section.Type.ATTRIBUTES, attributes), i

        self.error(f"Empty attributes section at line {start_index}")
        return None, i

    def read_exceptions_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse an "exceptions" section.

        Arguments:
            lines: The exceptions block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        exceptions = []
        block, i = self.read_block_items(lines, start_index)

        for exception_line in block:
            try:
                annotation, description = exception_line.split(": ", 1)
            except ValueError:
                self.error(f"Failed to get 'exception: description' pair from '{exception_line}'")
            else:
                exceptions.append(AnnotatedObject(annotation, description.lstrip(" ")))

        if exceptions:
            return Section(Section.Type.EXCEPTIONS, exceptions), i

        self.error(f"Empty exceptions section at line {start_index}")
        return None, i

    def read_return_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse an "returns" section.

        Arguments:
            lines: The return block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        text, i = self.read_block(lines, start_index)

        # Early exit if there is no text in the return section
        if not text:
            self.error(f"Empty return section at line {start_index}")
            return None, i

        # First try to get the annotation and description from the docstring
        try:
            type_, text = text.split(":", 1)
        except ValueError:
            description = text
            annotation = self.context["annotation"]
            # If there was no annotation in the docstring then move to signature
            if annotation is empty and self.context["signature"]:
                annotation = self.context["signature"].return_annotation
        else:
            annotation = type_.lstrip()
            description = text.lstrip()

        # There was no type in the docstring and no annotation
        if annotation is empty:
            self.error("No return type/annotation in docstring/signature")

        return Section(Section.Type.RETURN, AnnotatedObject(annotation, description)), i

    def read_yield_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse a "yields" section.

        Arguments:
            lines: The return block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        text, i = self.read_block(lines, start_index)

        # Early exit if there is no text in the yield section
        if not text:
            self.error(f"Empty yield section at line {start_index}")
            return None, i

        # First try to get the annotation and description from the docstring
        try:
            type_, text = text.split(":", 1)
        except ValueError:
            description = text
            annotation = self.context["annotation"]
            # If there was no annotation in the docstring then move to signature
            if annotation is empty and self.context["signature"]:
                annotation = self.context["signature"].return_annotation
        else:
            annotation = type_.lstrip()
            description = text.lstrip()

        # There was no type in the docstring and no annotation
        if annotation is empty:
            self.error("No yield type/annotation in docstring/signature")

        return Section(Section.Type.YIELD, AnnotatedObject(annotation, description)), i

    def read_examples_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
        """Parse an "examples" section.

        Arguments:
            lines: The examples block lines.
            start_index: The line number to start at.

        Returns:
            A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
        """
        text, i = self.read_block(lines, start_index)

        sub_sections = []
        in_code_example = False
        in_code_block = False
        current_text: list[str] = []
        current_example: list[str] = []

        for line in text.split("\n"):
            if is_empty_line(line):
                if in_code_example:
                    if current_example:
                        sub_sections.append((Section.Type.EXAMPLES, "\n".join(current_example)))
                        current_example = []
                    in_code_example = False
                else:
                    current_text.append(line)

            elif in_code_example:
                if self.trim_doctest_flags:
                    line = RE_DOCTEST_FLAGS.sub("", line)  # noqa: PLW2901
                    line = RE_DOCTEST_BLANKLINE.sub("", line)  # noqa: PLW2901
                current_example.append(line)

            elif line.startswith("```"):
                in_code_block = not in_code_block
                current_text.append(line)

            elif in_code_block:
                current_text.append(line)

            elif line.startswith(">>>"):
                if current_text:
                    sub_sections.append((Section.Type.MARKDOWN, "\n".join(current_text)))
                    current_text = []
                in_code_example = True

                if self.trim_doctest_flags:
                    line = RE_DOCTEST_FLAGS.sub("", line)  # noqa: PLW2901
                current_example.append(line)

            else:
                current_text.append(line)

        if current_text:
            sub_sections.append((Section.Type.MARKDOWN, "\n".join(current_text)))
        elif current_example:
            sub_sections.append((Section.Type.EXAMPLES, "\n".join(current_example)))

        if sub_sections:
            return Section(Section.Type.EXAMPLES, sub_sections), i

        self.error(f"Empty examples section at line {start_index}")
        return None, i

__init__(replace_admonitions=True, trim_doctest_flags=True, **kwargs) ¤

Initialize the object.

Parameters:

Name Type Description Default
replace_admonitions bool

Whether to replace admonitions by their Markdown equivalent.

True
trim_doctest_flags bool

Whether to remove doctest flags.

True
Source code in src/pytkdocs/parsers/docstrings/google.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def __init__(self, replace_admonitions: bool = True, trim_doctest_flags: bool = True, **kwargs: Any) -> None:  # noqa: FBT001, FBT002, ARG002
    """Initialize the object.

    Arguments:
        replace_admonitions: Whether to replace admonitions by their Markdown equivalent.
        trim_doctest_flags: Whether to remove doctest flags.
    """
    super().__init__()
    self.replace_admonitions = replace_admonitions
    self.trim_doctest_flags = trim_doctest_flags
    self.section_reader = {
        Section.Type.PARAMETERS: self.read_parameters_section,
        Section.Type.KEYWORD_ARGS: self.read_keyword_arguments_section,
        Section.Type.EXCEPTIONS: self.read_exceptions_section,
        Section.Type.EXAMPLES: self.read_examples_section,
        Section.Type.ATTRIBUTES: self.read_attributes_section,
        Section.Type.RETURN: self.read_return_section,
        Section.Type.YIELD: self.read_yield_section,
    }

_parse_parameters_section(lines, start_index) ¤

Parse a "parameters" or "keyword args" section.

Parameters:

Name Type Description Default
lines list[str]

The parameters block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[list[Parameter], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def _parse_parameters_section(self, lines: list[str], start_index: int) -> tuple[list[Parameter], int]:
    """Parse a "parameters" or "keyword args" section.

    Arguments:
        lines: The parameters block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    parameters = []
    type_: Any
    block, i = self.read_block_items(lines, start_index)

    for param_line in block:
        # Check that there is an annotation in the docstring
        try:
            name_with_type, description = param_line.split(":", 1)
        except ValueError:
            self.error(f"Failed to get 'name: description' pair from '{param_line}'")
            continue

        # Setting defaults
        default = empty
        annotation = empty
        kind = None
        # Can only get description from docstring - keep if no type was given
        description = description.lstrip()

        # If we have managed to find a type in the docstring use this
        if " " in name_with_type:
            name, type_ = name_with_type.split(" ", 1)
            annotation = type_.strip("()")
            if annotation.endswith(", optional"):  # type: ignore[attr-defined]
                annotation = annotation[:-10]  # type: ignore[misc]
        # Otherwise try to use the signature as `annotation` would still be empty
        else:
            name = name_with_type

        # Check in the signature to get extra details
        try:
            signature_param = self.context["signature"].parameters[name.lstrip("*")]
        except (AttributeError, KeyError):
            if annotation is empty:
                self.error(f"No type annotation for parameter '{name}'")
        else:
            if annotation is empty:
                annotation = signature_param.annotation
            # If signature_param.X are empty it doesnt matter as defaults are empty anyway
            default = signature_param.default
            kind = signature_param.kind

        parameters.append(
            Parameter(name=name, annotation=annotation, description=description, default=default, kind=kind),
        )

    return parameters, i

read_attributes_section(lines, start_index) ¤

Parse an "attributes" section.

Parameters:

Name Type Description Default
lines list[str]

The parameters block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
def read_attributes_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse an "attributes" section.

    Arguments:
        lines: The parameters block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    attributes = []
    block, i = self.read_block_items(lines, start_index)

    for attr_line in block:
        try:
            name_with_type, description = attr_line.split(":", 1)
        except ValueError:
            self.error(f"Failed to get 'name: description' pair from '{attr_line}'")
            continue

        description = description.lstrip()

        if " " in name_with_type:
            name, annotation = name_with_type.split(" ", 1)
            annotation = annotation.strip("()")
            if annotation.endswith(", optional"):
                annotation = annotation[:-10]
        else:
            name = name_with_type
            annotation = self.context["attributes"].get(name, {}).get("annotation", empty)

        attributes.append(Attribute(name=name, annotation=annotation, description=description))

    if attributes:
        return Section(Section.Type.ATTRIBUTES, attributes), i

    self.error(f"Empty attributes section at line {start_index}")
    return None, i

read_block(lines, start_index) ¤

Parse an indented block.

Parameters:

Name Type Description Default
lines list[str]

The block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[str, int]

A tuple containing the list of lines and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def read_block(self, lines: list[str], start_index: int) -> tuple[str, int]:
    """Parse an indented block.

    Arguments:
        lines: The block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing the list of lines and the index at which to continue parsing.
    """
    if start_index >= len(lines):
        return "", start_index

    i = start_index
    block: list[str] = []

    # skip first empty lines
    while is_empty_line(lines[i]):
        i += 1

    # get initial indent
    indent = len(lines[i]) - len(lines[i].lstrip())

    if indent == 0:
        # first non-empty line was not indented, abort
        return "", i - 1

    # start processing first item
    block.append(lines[i].lstrip())
    i += 1

    # loop on next lines
    while i < len(lines) and (lines[i].startswith(indent * " ") or is_empty_line(lines[i])):
        block.append(lines[i][indent:])
        i += 1

    return "\n".join(block).rstrip("\n"), i - 1

read_block_items(lines, start_index) ¤

Parse an indented block as a list of items.

The first indentation level is used as a reference to determine if the next lines are new items or continuation lines.

Parameters:

Name Type Description Default
lines list[str]

The block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[list[str], int]

A tuple containing the list of concatenated lines and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def read_block_items(self, lines: list[str], start_index: int) -> tuple[list[str], int]:
    """Parse an indented block as a list of items.

    The first indentation level is used as a reference to determine if the next lines are new items
    or continuation lines.

    Arguments:
        lines: The block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing the list of concatenated lines and the index at which to continue parsing.
    """
    if start_index >= len(lines):
        return [], start_index

    i = start_index
    items: list[str] = []

    # skip first empty lines
    while is_empty_line(lines[i]):
        i += 1

    # get initial indent
    indent = len(lines[i]) - len(lines[i].lstrip())

    if indent == 0:
        # first non-empty line was not indented, abort
        return [], i - 1

    # start processing first item
    current_item = [lines[i][indent:]]
    i += 1

    # loop on next lines
    while i < len(lines):
        line = lines[i]

        if line.startswith(indent * 2 * " "):
            # continuation line
            current_item.append(line[indent * 2 :])

        elif line.startswith((indent + 1) * " "):
            # indent between initial and continuation: append but add error
            cont_indent = len(line) - len(line.lstrip())
            current_item.append(line[cont_indent:])
            self.error(
                f"Confusing indentation for continuation line {i + 1} in docstring, "
                f"should be {indent} * 2 = {indent * 2} spaces, not {cont_indent}",
            )

        elif line.startswith(indent * " "):
            # indent equal to initial one: new item
            items.append("\n".join(current_item))
            current_item = [line[indent:]]

        elif is_empty_line(line):
            # empty line: preserve it in the current item
            current_item.append("")

        else:
            # indent lower than initial one: end of section
            break

        i += 1

    if current_item:
        items.append("\n".join(current_item).rstrip("\n"))

    return items, i - 1

read_examples_section(lines, start_index) ¤

Parse an "examples" section.

Parameters:

Name Type Description Default
lines list[str]

The examples block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
def read_examples_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse an "examples" section.

    Arguments:
        lines: The examples block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    text, i = self.read_block(lines, start_index)

    sub_sections = []
    in_code_example = False
    in_code_block = False
    current_text: list[str] = []
    current_example: list[str] = []

    for line in text.split("\n"):
        if is_empty_line(line):
            if in_code_example:
                if current_example:
                    sub_sections.append((Section.Type.EXAMPLES, "\n".join(current_example)))
                    current_example = []
                in_code_example = False
            else:
                current_text.append(line)

        elif in_code_example:
            if self.trim_doctest_flags:
                line = RE_DOCTEST_FLAGS.sub("", line)  # noqa: PLW2901
                line = RE_DOCTEST_BLANKLINE.sub("", line)  # noqa: PLW2901
            current_example.append(line)

        elif line.startswith("```"):
            in_code_block = not in_code_block
            current_text.append(line)

        elif in_code_block:
            current_text.append(line)

        elif line.startswith(">>>"):
            if current_text:
                sub_sections.append((Section.Type.MARKDOWN, "\n".join(current_text)))
                current_text = []
            in_code_example = True

            if self.trim_doctest_flags:
                line = RE_DOCTEST_FLAGS.sub("", line)  # noqa: PLW2901
            current_example.append(line)

        else:
            current_text.append(line)

    if current_text:
        sub_sections.append((Section.Type.MARKDOWN, "\n".join(current_text)))
    elif current_example:
        sub_sections.append((Section.Type.EXAMPLES, "\n".join(current_example)))

    if sub_sections:
        return Section(Section.Type.EXAMPLES, sub_sections), i

    self.error(f"Empty examples section at line {start_index}")
    return None, i

read_exceptions_section(lines, start_index) ¤

Parse an "exceptions" section.

Parameters:

Name Type Description Default
lines list[str]

The exceptions block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
def read_exceptions_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse an "exceptions" section.

    Arguments:
        lines: The exceptions block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    exceptions = []
    block, i = self.read_block_items(lines, start_index)

    for exception_line in block:
        try:
            annotation, description = exception_line.split(": ", 1)
        except ValueError:
            self.error(f"Failed to get 'exception: description' pair from '{exception_line}'")
        else:
            exceptions.append(AnnotatedObject(annotation, description.lstrip(" ")))

    if exceptions:
        return Section(Section.Type.EXCEPTIONS, exceptions), i

    self.error(f"Empty exceptions section at line {start_index}")
    return None, i

read_keyword_arguments_section(lines, start_index) ¤

Parse a "keyword arguments" section.

Parameters:

Name Type Description Default
lines list[str]

The parameters block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def read_keyword_arguments_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse a "keyword arguments" section.

    Arguments:
        lines: The parameters block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    parameters, i = self._parse_parameters_section(lines, start_index)
    for parameter in parameters:
        parameter.kind = inspect.Parameter.KEYWORD_ONLY

    if parameters:
        return Section(Section.Type.KEYWORD_ARGS, parameters), i

    self.error(f"Empty keyword arguments section at line {start_index}")
    return None, i

read_parameters_section(lines, start_index) ¤

Parse a "parameters" section.

Parameters:

Name Type Description Default
lines list[str]

The parameters block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def read_parameters_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse a "parameters" section.

    Arguments:
        lines: The parameters block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    parameters, i = self._parse_parameters_section(lines, start_index)

    if parameters:
        return Section(Section.Type.PARAMETERS, parameters), i

    self.error(f"Empty parameters section at line {start_index}")
    return None, i

read_return_section(lines, start_index) ¤

Parse an "returns" section.

Parameters:

Name Type Description Default
lines list[str]

The return block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
def read_return_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse an "returns" section.

    Arguments:
        lines: The return block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    text, i = self.read_block(lines, start_index)

    # Early exit if there is no text in the return section
    if not text:
        self.error(f"Empty return section at line {start_index}")
        return None, i

    # First try to get the annotation and description from the docstring
    try:
        type_, text = text.split(":", 1)
    except ValueError:
        description = text
        annotation = self.context["annotation"]
        # If there was no annotation in the docstring then move to signature
        if annotation is empty and self.context["signature"]:
            annotation = self.context["signature"].return_annotation
    else:
        annotation = type_.lstrip()
        description = text.lstrip()

    # There was no type in the docstring and no annotation
    if annotation is empty:
        self.error("No return type/annotation in docstring/signature")

    return Section(Section.Type.RETURN, AnnotatedObject(annotation, description)), i

read_yield_section(lines, start_index) ¤

Parse a "yields" section.

Parameters:

Name Type Description Default
lines list[str]

The return block lines.

required
start_index int

The line number to start at.

required

Returns:

Type Description
tuple[Optional[Section], int]

A tuple containing a Section (or None) and the index at which to continue parsing.

Source code in src/pytkdocs/parsers/docstrings/google.py
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
def read_yield_section(self, lines: list[str], start_index: int) -> tuple[Optional[Section], int]:
    """Parse a "yields" section.

    Arguments:
        lines: The return block lines.
        start_index: The line number to start at.

    Returns:
        A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
    """
    text, i = self.read_block(lines, start_index)

    # Early exit if there is no text in the yield section
    if not text:
        self.error(f"Empty yield section at line {start_index}")
        return None, i

    # First try to get the annotation and description from the docstring
    try:
        type_, text = text.split(":", 1)
    except ValueError:
        description = text
        annotation = self.context["annotation"]
        # If there was no annotation in the docstring then move to signature
        if annotation is empty and self.context["signature"]:
            annotation = self.context["signature"].return_annotation
    else:
        annotation = type_.lstrip()
        description = text.lstrip()

    # There was no type in the docstring and no annotation
    if annotation is empty:
        self.error("No yield type/annotation in docstring/signature")

    return Section(Section.Type.YIELD, AnnotatedObject(annotation, description)), i

is_empty_line(line) ¤

Tell if a line is empty.

Parameters:

Name Type Description Default
line str

The line to check.

required

Returns:

Type Description
bool

True if the line is empty or composed of blanks only, False otherwise.

Source code in src/pytkdocs/parsers/docstrings/google.py
528
529
530
531
532
533
534
535
536
537
def is_empty_line(line: str) -> bool:
    """Tell if a line is empty.

    Arguments:
        line: The line to check.

    Returns:
        True if the line is empty or composed of blanks only, False otherwise.
    """
    return not line.strip()