Docstring parsers¤
Main API¤
parse ¤
parse(
docstring: Docstring,
parser: DocstringStyle | Parser | None,
**options: Any,
) -> list[DocstringSection]
Parse the docstring.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(parser¤DocstringStyle | Parser | None) –The docstring parser to use. If None, return a single text section.
-
(**options¤Any, default:{}) –The options accepted by the parser.
Returns:
-
list[DocstringSection]–A list of docstring sections.
Source code in src/griffe/_internal/docstrings/parsers.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
parse_auto ¤
parse_auto(
docstring: Docstring,
*,
method: DocstringDetectionMethod = "heuristics",
style_order: list[Parser]
| list[DocstringStyle]
| None = None,
default: Parser | DocstringStyle | None = None,
per_style_options: PerStyleOptions | None = None,
**options: Any,
) -> list[DocstringSection]
Parse a docstring by automatically detecting the style it uses.
See infer_docstring_style for more information on the available parameters.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(method¤DocstringDetectionMethod, default:'heuristics') –The method to use to infer the parser.
-
(style_order¤list[Parser] | list[DocstringStyle] | None, default:None) –The order of the styles to try when inferring the parser.
-
(default¤Parser | DocstringStyle | None, default:None) –The default parser to use if the inference fails.
-
(per_style_options¤PerStyleOptions | None, default:None) –Additional parsing options per style.
-
(**options¤Any, default:{}) –Deprecated. Use
per_style_optionsinstead.
Returns:
-
list[DocstringSection]–A list of docstring sections.
Source code in src/griffe/_internal/docstrings/auto.py
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 | |
parse_google ¤
parse_google(
docstring: Docstring,
*,
ignore_init_summary: bool = False,
trim_doctest_flags: bool = True,
returns_multiple_items: bool = True,
returns_named_value: bool = True,
returns_type_in_property_summary: bool = False,
receives_multiple_items: bool = True,
receives_named_value: bool = True,
warn_unknown_params: bool = True,
warn_missing_types: bool = True,
warnings: bool = True,
**options: Any,
) -> list[DocstringSection]
Parse a Google-style docstring.
This function iterates on lines of a docstring to build sections. It then returns this list of sections.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(ignore_init_summary¤bool, default:False) –Whether to ignore the summary in
__init__methods' docstrings. -
(trim_doctest_flags¤bool, default:True) –Whether to remove doctest flags from Python example blocks.
-
(returns_multiple_items¤bool, default:True) –Whether to parse multiple items in
YieldsandReturnssections. When true, each item's continuation lines must be indented. When false (single item), no further indentation is required. -
(returns_named_value¤bool, default:True) –Whether to parse
YieldsandReturnssection items as name and description, rather than type and description. When true, type must be wrapped in parentheses:(int): Description.. Names are optional:name (int): Description.. When false, parentheses are optional but the items cannot be named:int: Description. -
(receives_multiple_items¤bool, default:True) –Whether to parse multiple items in
Receivessections. When true, each item's continuation lines must be indented. When false (single item), no further indentation is required. -
(receives_named_value¤bool, default:True) –Whether to parse
Receivessection items as name and description, rather than type and description. When true, type must be wrapped in parentheses:(int): Description.. Names are optional:name (int): Description.. When false, parentheses are optional but the items cannot be named:int: Description. -
(returns_type_in_property_summary¤bool, default:False) –Whether to parse the return type of properties at the beginning of their summary:
str: Summary of the property. -
(warn_unknown_params¤bool, default:True) –Warn about documented parameters not appearing in the signature.
-
(warn_missing_types¤bool, default:True) –Warn about missing types/annotations for parameters, return values, etc.
-
(warnings¤bool, default:True) –Whether to log warnings at all.
-
(**options¤Any, default:{}) –Swallowing keyword arguments for backward-compatibility.
Returns:
-
list[DocstringSection]–A list of docstring sections.
Source code in src/griffe/_internal/docstrings/google.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 | |
parse_numpy ¤
parse_numpy(
docstring: Docstring,
*,
ignore_init_summary: bool = False,
trim_doctest_flags: bool = True,
warn_unknown_params: bool = True,
warn_missing_types: bool = True,
warnings: bool = True,
**options: Any,
) -> list[DocstringSection]
Parse a Numpydoc-style docstring.
This function iterates on lines of a docstring to build sections. It then returns this list of sections.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(ignore_init_summary¤bool, default:False) –Whether to ignore the summary in
__init__methods' docstrings. -
(trim_doctest_flags¤bool, default:True) –Whether to remove doctest flags from Python example blocks.
-
(warn_unknown_params¤bool, default:True) –Warn about documented parameters not appearing in the signature.
-
(warn_missing_types¤bool, default:True) –Warn about missing types/annotations for parameters, return values, etc.
-
(warnings¤bool, default:True) –Whether to log warnings at all.
-
(**options¤Any, default:{}) –Swallowing keyword arguments for backward-compatibility.
Returns:
-
list[DocstringSection]–A list of docstring sections.
Source code in src/griffe/_internal/docstrings/numpy.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
parse_sphinx ¤
parse_sphinx(
docstring: Docstring,
*,
warn_unknown_params: bool = True,
warnings: bool = True,
**options: Any,
) -> list[DocstringSection]
Parse a Sphinx-style docstring.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(warn_unknown_params¤bool, default:True) –Warn about documented parameters not appearing in the signature.
-
(warnings¤bool, default:True) –Whether to log warnings at all.
-
(**options¤Any, default:{}) –Swallowing keyword arguments for backward-compatibility.
Returns:
-
list[DocstringSection]–A list of docstring sections.
Source code in src/griffe/_internal/docstrings/sphinx.py
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 | |
DocstringStyle module-attribute ¤
DocstringStyle = Literal[
"google", "numpy", "sphinx", "auto"
]
The supported docstring styles (literal values of the Parser enumeration).
Parser options¤
DocstringOptions module-attribute ¤
DocstringOptions = Union[
GoogleOptions, NumpyOptions, SphinxOptions, AutoOptions
]
The options for each docstring style.
GoogleOptions typed-dict ¤
GoogleOptions(
*,
ignore_init_summary: bool = ...,
trim_doctest_flags: bool = ...,
returns_multiple_items: bool = ...,
returns_named_value: bool = ...,
returns_type_in_property_summary: bool = ...,
receives_multiple_items: bool = ...,
receives_named_value: bool = ...,
warn_unknown_params: bool = ...,
warn_missing_types: bool = ...,
warnings: bool = ...,
)
Bases: TypedDict
flowchart TD
griffe.GoogleOptions[GoogleOptions]
click griffe.GoogleOptions href "" "griffe.GoogleOptions"
Options for parsing Google-style docstrings.
Parameters:
-
(ignore_init_summary¤bool, default:...) –Whether to ignore the summary in
__init__methods' docstrings. -
(trim_doctest_flags¤bool, default:...) –Whether to remove doctest flags from Python example blocks.
-
(returns_multiple_items¤bool, default:...) –Whether to parse multiple items in
YieldsandReturnssections. -
(returns_named_value¤bool, default:...) –Whether to parse
YieldsandReturnssection items as name and description, rather than type and description. -
(returns_type_in_property_summary¤bool, default:...) –Whether to parse the return type of properties at the beginning of their summary.
-
(receives_multiple_items¤bool, default:...) –Whether to parse multiple items in
Receivessections. -
(receives_named_value¤bool, default:...) –Whether to parse
Receivessection items as name and description, rather than type and description. -
(warn_unknown_params¤bool, default:...) –Whether to warn about unknown parameters.
-
(warn_missing_types¤bool, default:...) –Whether to warn about missing types/annotations for parameters, return values, etc.
-
(warnings¤bool, default:...) –Whether to issue warnings for parsing issues.
NumpyOptions typed-dict ¤
NumpyOptions(
*,
ignore_init_summary: bool = ...,
trim_doctest_flags: bool = ...,
warn_unknown_params: bool = ...,
warn_missing_types: bool = ...,
warnings: bool = ...,
)
Bases: TypedDict
flowchart TD
griffe.NumpyOptions[NumpyOptions]
click griffe.NumpyOptions href "" "griffe.NumpyOptions"
Options for parsing Numpydoc-style docstrings.
Parameters:
-
(ignore_init_summary¤bool, default:...) –Whether to ignore the summary in
__init__methods' docstrings. -
(trim_doctest_flags¤bool, default:...) –Whether to remove doctest flags from Python example blocks.
-
(warn_unknown_params¤bool, default:...) –Whether to warn about unknown parameters.
-
(warn_missing_types¤bool, default:...) –Whether to warn about missing types/annotations for parameters, return values, etc.
-
(warnings¤bool, default:...) –Whether to issue warnings for parsing issues.
SphinxOptions typed-dict ¤
SphinxOptions(
*, warn_unknown_params: bool = ..., warnings: bool = ...
)
Bases: TypedDict
flowchart TD
griffe.SphinxOptions[SphinxOptions]
click griffe.SphinxOptions href "" "griffe.SphinxOptions"
Options for parsing Sphinx-style docstrings.
Parameters:
-
(warn_unknown_params¤bool, default:...) –Whether to warn about unknown parameters.
-
(warnings¤bool, default:...) –Whether to issue warnings for parsing issues.
AutoOptions typed-dict ¤
AutoOptions(
*,
method: DocstringDetectionMethod = ...,
style_order: list[Parser]
| list[DocstringStyle]
| None = ...,
default: Parser | DocstringStyle | None = ...,
per_style_options: PerStyleOptions | None = ...,
)
Bases: TypedDict
flowchart TD
griffe.AutoOptions[AutoOptions]
click griffe.AutoOptions href "" "griffe.AutoOptions"
Options for Auto-style docstrings.
Parameters:
-
(method¤DocstringDetectionMethod, default:...) –The method to use to infer the parser.
-
(style_order¤list[Parser] | list[DocstringStyle] | None, default:...) –The order of styles to try when inferring the parser.
-
(default¤Parser | DocstringStyle | None, default:...) –The default parser to use if the inference fails.
-
(per_style_options¤PerStyleOptions | None, default:...) –Additional parsing options per style.
PerStyleOptions typed-dict ¤
PerStyleOptions(
*,
google: GoogleOptions = ...,
numpy: NumpyOptions = ...,
sphinx: SphinxOptions = ...,
)
Bases: TypedDict
flowchart TD
griffe.PerStyleOptions[PerStyleOptions]
click griffe.PerStyleOptions href "" "griffe.PerStyleOptions"
Per-style options for docstring parsing.
Parameters:
-
(google¤GoogleOptions, default:...) –Options for Google-style docstrings.
-
(numpy¤NumpyOptions, default:...) –Options for Numpy-style docstrings.
-
(sphinx¤SphinxOptions, default:...) –Options for Sphinx-style docstrings.
Advanced API¤
Parser ¤
flowchart TD
griffe.Parser[Parser]
click griffe.Parser href "" "griffe.Parser"
Enumeration of the different docstring parsers.
Attributes:
-
auto–Infer docstring parser.
-
google–Google-style docstrings parser.
-
numpy–Numpydoc-style docstrings parser.
-
sphinx–Sphinx-style docstrings parser.
parsers module-attribute ¤
parsers: dict[
Parser, Callable[[Docstring], list[DocstringSection]]
] = {
auto: parse_auto,
google: parse_google,
sphinx: parse_sphinx,
numpy: parse_numpy,
}
parse_docstring_annotation ¤
parse_docstring_annotation(
annotation: str,
docstring: Docstring,
log_level: LogLevel = error,
) -> str | Expr
Parse a string into a true name or expression that can be resolved later.
Parameters:
-
(annotation¤str) –The annotation to parse.
-
(docstring¤Docstring) –The docstring in which the annotation appears. The docstring's parent is accessed to bind a resolver to the resulting name/expression.
-
(log_level¤LogLevel, default:error) –Log level to use to log a message.
Returns:
Source code in src/griffe/_internal/docstrings/utils.py
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 | |
docstring_warning ¤
docstring_warning(
docstring: Docstring,
offset: int,
message: str,
log_level: LogLevel = warning,
) -> None
Log a warning when parsing a docstring.
This function logs a warning message by prefixing it with the filepath and line number.
Parameters:
-
(docstring¤Docstring) –The docstring object.
-
(offset¤int) –The offset in the docstring lines.
-
(message¤str) –The message to log.
Returns:
-
None–A function used to log parsing warnings if
namewas passed, else none.
Source code in src/griffe/_internal/docstrings/utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
DocstringDetectionMethod module-attribute ¤
DocstringDetectionMethod = Literal[
"heuristics", "max_sections"
]
The supported methods to infer docstring styles.
infer_docstring_style ¤
infer_docstring_style(
docstring: Docstring,
*,
method: DocstringDetectionMethod = "heuristics",
style_order: list[Parser]
| list[DocstringStyle]
| None = None,
default: Parser | DocstringStyle | None = None,
per_style_options: PerStyleOptions | None = None,
**options: Any,
) -> tuple[Parser | None, list[DocstringSection] | None]
Infer the parser to use for the docstring.
The 'heuristics' method uses regular expressions. The 'max_sections' method parses the docstring with all parsers specified in style_order and returns the one who parsed the most sections.
If heuristics fail, the default parser is returned. If multiple parsers parsed the same number of sections, style_order is used to decide which one to return. The default parser is never used with the 'max_sections' method.
Additional options are parsed to the detected parser, if any.
Parameters:
-
(docstring¤Docstring) –The docstring to parse.
-
(method¤DocstringDetectionMethod, default:'heuristics') –The method to use to infer the parser.
-
(style_order¤list[Parser] | list[DocstringStyle] | None, default:None) –The order of the styles to try when inferring the parser.
-
(default¤Parser | DocstringStyle | None, default:None) –The default parser to use if the inference fails.
-
(per_style_options¤PerStyleOptions | None, default:None) –Additional parsing options per style.
-
(**options¤Any, default:{}) –Deprecated. Use
per_style_optionsinstead.
Returns:
-
tuple[Parser | None, list[DocstringSection] | None]–The inferred parser, and optionally parsed sections (when method is 'max_sections').
Source code in src/griffe/_internal/docstrings/auto.py
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 | |