Skip to content

Commit e2ce64c

Browse files
committed
feat: Release Insiders project
1 parent 05e0a40 commit e2ce64c

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and [installation instructions](https://mkdocstrings.github.io/griffe-autodocstr
1414

1515
## Usage
1616

17-
[Enable(https://mkdocstrings.github.io/griffe/guide/users/extending/#using-extensions)] the `griffe_autodocstringstyle` extension. Now all packages loaded from a virtual environment will have their docstrings parsed with the `auto` style (automatically guessing the docstring style).
17+
[Enable](https://mkdocstrings.github.io/griffe/guide/users/extending/#using-extensions) the `griffe_autodocstringstyle` extension. Now all packages loaded from a virtual environment will have their docstrings parsed with the `auto` style (automatically guessing the docstring style).
1818

1919
Use the `exclude` option to pass package names that shouldn't be considered. This can be useful if you must first install your sources as a package before loading/documenting them (meaning they end up in the virtual environment too).
2020

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
from __future__ import annotations
22

3+
import re
4+
from typing import Any
5+
36
import griffe
47

8+
_external = re.compile("/(.venv|site-packages)/")
9+
510

611
class AutoDocstringStyleExtension(griffe.Extension):
712
"""Set `auto` docstring style on external packages."""
13+
14+
def __init__(self, exclude: list[str] | None = None) -> None:
15+
"""Initialize the extension.
16+
17+
Parameters:
18+
exclude: Package names to exclude.
19+
"""
20+
self._exclude = set(exclude or ())
21+
22+
def on_instance(self, *, obj: griffe.Object, **kwargs: Any) -> None: # noqa: ARG002
23+
"""Set docstring style to `auto` on all external Griffe objects.
24+
25+
Parameters:
26+
obj: A Griffe object.
27+
**kwargs: Additional arguments.
28+
"""
29+
if not obj.docstring or isinstance(obj.filepath, list) or obj.package.name in self._exclude:
30+
return
31+
filepath = obj.filepath.resolve().as_posix()
32+
if _external.search(filepath):
33+
obj.docstring.parser = griffe.Parser.auto

tests/test_extension.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22

33
from __future__ import annotations
44

5+
import griffe
56

6-
def test_noop() -> None:
7-
"""Dummy test."""
8-
assert True
97

8+
def test_extension() -> None:
9+
"""Load self and external package, assert styles."""
10+
self_api = griffe.load("griffe_autodocstringstyle", extensions=griffe.load_extensions("griffe_autodocstringstyle"))
11+
assert self_api.docstring
12+
assert self_api.docstring.parser is None
13+
14+
pytest_api = griffe.load("pytest", extensions=griffe.load_extensions("griffe_autodocstringstyle"))
15+
assert pytest_api.docstring
16+
assert pytest_api.docstring.parser is griffe.Parser.auto
17+
18+
19+
def test_exclude_option() -> None:
20+
"""Excluded packages are untouched."""
21+
pytest_api = griffe.load(
22+
"pytest",
23+
extensions=griffe.load_extensions({"griffe_autodocstringstyle": {"exclude": ["pytest"]}}),
24+
)
25+
assert pytest_api.docstring
26+
assert pytest_api.docstring.parser is None

0 commit comments

Comments
 (0)