10000 feat: Add option to search for stubs packages · mkdocstrings/python@0c6aa32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c6aa32

Browse files
authored
feat: Add option to search for stubs packages
This allows using the feature in Griffe that searches for stubs packages. PR #128: #128 PR griffe#221: : mkdocstrings/griffe#221
1 parent 548bdad commit 0c6aa32

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

docs/usage/configuration/general.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,60 @@ __all__ = ["their_object"]
191191
<p>Docstring of your module.</p>
192192
////
193193
///
194+
195+
## `find_stubs_package`
196+
197+
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
198+
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
199+
200+
When looking for documentation specified in [autodoc instructions][autodoc syntax] (`::: identifier`), also look for
201+
the stubs package as defined in [PEP 561](https://peps.python.org/pep-0561/) if it exists. This is useful when
202+
most of your documentation is separately provided by such a package and not inline in your main package.
203+
204+
```yaml title="in mkdocs.yml (global configuration)"
205+
plugins:
206+
- mkdocstrings:
207+
handlers:
208+
python:
209+
options:
210+
find_stubs_package: true
211+
```
212+
213+
```md title="or in docs/some_page.md (local configuration)"
214+
::: your_package.your_module.your_func
215+
options:
216+
find_stubs_package: true
217+
```
218+
219+
```python title="your_package/your_module.py"
220+
221+
def your_func(a, b):
222+
# Function code
223+
...
224+
225+
# rest of your code
226+
```
227+
228+
```python title="your_package-stubs/your_module.pyi"
229+
230+
def your_func(a: int, b: str):
231+
"""
232+
<Function docstring>
233+
"""
234+
...
235+
236+
# rest of your code
237+
```
238+
239+
/// admonition | Preview
240+
type: preview
241+
242+
//// tab | With find_stubs_package
243+
<h2><code>your_func</code></h2>
244+
<p>Function docstring</p>
245+
////
246+
247+
//// tab | Without find_stubs_package
248+
<h2><code>your_func</code></h2>
249+
////
250+
///

src/mkdocstrings_handlers/python/handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class PythonHandler(BaseHandler):
6161
fallback_config: ClassVar[dict] = {"fallback": True}
6262
"""The configuration used to collect item during autorefs fallback."""
6363
default_config: ClassVar[dict] = {
64+
"find_stubs_package": False,
6465
"docstring_style": "google",
6566
"docstring_options": {},
6667
"show_symbol_type_heading": False,
@@ -110,6 +111,7 @@ class PythonHandler(BaseHandler):
110111
"""Default handler configuration.
111112
112113
Attributes: General options:
114+
find_stubs_package (bool): Whether to load stubs package (package-stubs) when extracting docstrings.
113115
allow_inspection (bool): Whether to allow inspecting modules when visiting them is not possible. Default: `True`.
114116
show_bases (bool): Show the base classes of a class. Default: `True`.
115117
show_source (bool): Show the source code of this object. Default: `True`.
@@ -279,8 +281,8 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
279281
try:
280282
for pre_loaded_module in final_config.get("preload_modules") or []:
281283
if pre_loaded_module not in self._modules_collection:
282-
loader.load(pre_loaded_module)
283-
loader.load(module_name)
284+
loader.load(pre_loaded_module, find_stubs_package=final_config["find_stubs_package"])
285+
loader.load(module_name, find_stubs_package=final_config["find_stubs_package"])
284286
except ImportError as error:
285287
raise CollectionError(str(error)) from error
286288
unresolved, iterations = loader.resolve_aliases(

0 commit comments

Comments
 (0)
0