Skip to content

Commit ce59b7d

Browse files
committed
feat: Allow passing a docstring parser name instead of its enumeration value
1 parent cc3ca2e commit ce59b7d

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/griffe/dataclasses.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from contextlib import suppress
1212
from pathlib import Path
1313
from textwrap import dedent
14-
from typing import TYPE_CHECKING, Any, Callable, Sequence, Union, cast
14+
from typing import TYPE_CHECKING, Any, Callable, Literal, Sequence, Union, cast
1515

1616
from griffe.c3linear import c3linear_merge
1717
from griffe.docstrings.parsers import Parser, parse
@@ -90,7 +90,7 @@ def __init__(
9090
lineno: int | None = None,
9191
endlineno: int | None = None,
9292
parent: Object | None = None,
93-
parser: Parser | None = None,
93+
parser: Literal["google", "numpy", "sphinx"] | Parser | None = None,
9494
parser_options: dict[str, Any] | None = None,
9595
) -> None:
9696
"""Initialize the docstring.
@@ -107,7 +107,7 @@ def __init__(
107107
self.lineno: int | None = lineno
108108
self.endlineno: int | None = endlineno
109109
self.parent: Object | None = parent
110-
self.parser: Parser | None = parser
110+
self.parser: Literal["google", "numpy", "sphinx"] | Parser | None = parser
111111
self.parser_options: dict[str, Any] = parser_options or {}
112112

113113
def __bool__(self) -> bool:
@@ -131,7 +131,11 @@ def parsed(self) -> list[DocstringSection]:
131131
"""
132132
return self.parse()
133133

134-
def parse(self, parser: Parser | None = None, **options: Any) -> list[DocstringSection]:
134+
def parse(
135+
self,
136+
parser: Literal["google", "numpy", "sphinx"] | Parser | None = None,
137+
**options: Any,
138+
) -> list[DocstringSection]:
135139
"""Parse the docstring into structured data.
136140
137141
Parameters:

src/griffe/docstrings/parsers.py

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

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Any
5+
from typing import TYPE_CHECKING, Any, Literal
66

77
from griffe.docstrings.dataclasses import DocstringSection, DocstringSectionText
88
from griffe.docstrings.google import parse as parse_google
@@ -20,7 +20,11 @@
2020
}
2121

2222

23-
def parse(docstring: Docstring, parser: Parser | None, **options: Any) -> list[DocstringSection]:
23+
def parse(
24+
docstring: Docstring,
25+
parser: Literal["google", "numpy", "sphinx"] | Parser | None,
26+
**options: Any,
27+
) -> list[DocstringSection]:
2428
"""Parse the docstring.
2529
2630
Parameters:
@@ -32,6 +36,8 @@ def parse(docstring: Docstring, parser: Parser | None, **options: Any) -> list[D
3236
A list of docstring sections.
3337
"""
3438
if parser:
39+
if isinstance(parser, str):
40+
parser = Parser(parser)
3541
return parsers[parser](docstring, **options) # type: ignore[operator]
3642
return [DocstringSectionText(docstring.value)]
3743

0 commit comments

Comments
 (0)