Skip to content

Commit e48fbde

Browse files
committed
Add documentation
1 parent 9a25e1e commit e48fbde

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

docs/usage/configuration/docstrings.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,128 @@ class ClassWithoutDocstring:
606606
////
607607
///
608608

609+
610+
## `docstring_inherit_strategy`
611+
612+
- **:octicons-package-24: Type [`str`][] :material-equal: `"default"`{ title="default value" } | `"if_not_present"` | `"merge"`**
613+
614+
This setting controls how docstrings from parent classes are handled in the auto-generated API documentation when subclass methods override parent methods.
615+
616+
- `default`: The default behavior, where the subclass docstring is used if present. If the subclass does not provide a docstring, no docstring is inherited from the parent class. This option is useful if you want to document each method independently, even when subclassing. The default behavior is also the fastest, as it does not require any traversal of the class hierarchy.
617+
- `if_not_present`: This option allows the docstring of the parent method to be inherited if the subclass method does not provide its own docstring. It ensures that a method without documentation in a subclass still displays useful inherited information from its parent.
618+
- `merge`: This setting merges the docstrings from parent methods with overriding subclass methods, concatenating all parent classes' docstrings with any additional text provided by the subclass. This is useful for methods where the subclass adds supplementary notes or overrides part of the behavior but still shares the general purpose of the parent method. To control the concatenation delimiter, you can use the `docstring_inherit_delimiter` option.
619+
620+
```yaml title="in mkdocs.yml (global configuration)"
621+
plugins:
622+
- mkdocstrings:
623+
handlers:
624+
python:
625+
options:
626+
docstring_inherit_strategy: default
627+
```
628+
629+
/// admonition | Preview
630+
type: preview
631+
632+
//// tab | default
633+
```python
634+
class Shape:
635+
contour: list[Point]
636+
def surface_area(self):
637+
"""Return the surface area in square meters."""
638+
return numerical_integration(self.contour)
639+
640+
class Rectange(Shape)
641+
def surface_area(self):
642+
return distance(self.cotour[2], self.contour[0]) * distance(self.contour[3], self.contour[1])
643+
```
644+
645+
Should output a documentation like this:
646+
```
647+
Shape
648+
surface_area
649+
Return the surface area in square meters.
650+
...
651+
652+
Rectangle
653+
...
654+
```
655+
////
656+
657+
658+
//// tab | if_not_present
659+
```python
660+
class Shape:
661+
contour: list[Point]
662+
def surface_area(self):
663+
"""Return the surface area in square meters."""
664+
return numerical_integration(self.contour)
665+
666+
class Rectange(Shape)
667+
def surface_area(self):
668+
return distance(self.cotour[2], self.contour[0]) * distance(self.contour[3], self.contour[1])
669+
```
670+
671+
Should output a documentation like this:
672+
```
673+
Shape
674+
surface_area
675+
Return the surface area in square meters.
676+
...
677+
678+
Rectangle
679+
surface_area
680+
Return the surface area in square meters.
681+
...
682+
```
683+
////
684+
685+
//// tab | merge
686+
```python
687+
class Shape:
688+
contour: list[Point]
689+
def surface_area(self):
690+
"""Return the surface area in square meters."""
691+
return numerical_integration(self.contour)
692+
693+
class Rectange(Shape)
694+
def surface_area(self):
695+
"""Note: This is way faster than the calculation for general shapes!"""
696+
return distance(self.cotour[2], self.contour[0]) * distance(self.contour[3], self.contour[1])
697+
```
698+
699+
Should output a documentation like this:
700+
```
701+
Shape
702+
surface_area
703+
Return the surface area in square meters.
704+
...
705+
706+
Rectangle
707+
surface_area
708+
Return the surface area in square meters.
709+
Note: This is way faster than the calculation for general shapes!
710+
...
711+
```
712+
////
713+
///
714+
715+
## `docstring_merge_delimiter`
716+
717+
- **:octicons-package-24: Type [`str`][] :material-equal: `"\n\n"`{ title="default value" }**
718+
719+
The delimiter is used to join docstrings when the `docstring_inherit_strategy` is set to `merge`.
720+
721+
```yaml title="in mkdocs.yml (global configuration)"
722+
plugins:
723+
- mkdocstrings:
724+
handlers:
725+
python:
726+
options:
727+
docstring_merge_delimiter: "\n\n"
728+
```
729+
730+
609731
## `show_docstring_attributes`
610732

611733
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**

0 commit comments

Comments
 (0)