Skip to content

Commit

Permalink
feat: Allow passing a docstring parser name instead of its enumeratio…
Browse files Browse the repository at this point in the history
…n value
  • Loading branch information
pawamoy committed Aug 20, 2023
1 parent cc3ca2e commit ce59b7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from contextlib import suppress
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING, Any, Callable, Sequence, Union, cast
from typing import TYPE_CHECKING, Any, Callable, Literal, Sequence, Union, cast

from griffe.c3linear import c3linear_merge
from griffe.docstrings.parsers import Parser, parse
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(
lineno: int | None = None,
endlineno: int | None = None,
parent: Object | None = None,
parser: Parser | None = None,
parser: Literal["google", "numpy", "sphinx"] | Parser | None = None,
parser_options: dict[str, Any] | None = None,
) -> None:
"""Initialize the docstring.
Expand All @@ -107,7 +107,7 @@ def __init__(
self.lineno: int | None = lineno
self.endlineno: int | None = endlineno
self.parent: Object | None = parent
self.parser: Parser | None = parser
self.parser: Literal["google", "numpy", "sphinx"] | Parser | None = parser
self.parser_options: dict[str, Any] = parser_options or {}

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

def parse(self, parser: Parser | None = None, **options: Any) -> list[DocstringSection]:
def parse(
self,
parser: Literal["google", "numpy", "sphinx"] | Parser | None = None,
**options: Any,
) -> list[DocstringSection]:
"""Parse the docstring into structured data.
Parameters:
Expand Down
10 changes: 8 additions & 2 deletions src/griffe/docstrings/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Literal

from griffe.docstrings.dataclasses import DocstringSection, DocstringSectionText
from griffe.docstrings.google import parse as parse_google
Expand All @@ -20,7 +20,11 @@
}


def parse(docstring: Docstring, parser: Parser | None, **options: Any) -> list[DocstringSection]:
def parse(
docstring: Docstring,
parser: Literal["google", "numpy", "sphinx"] | Parser | None,
**options: Any,
) -> list[DocstringSection]:
"""Parse the docstring.
Parameters:
Expand All @@ -32,6 +36,8 @@ def parse(docstring: Docstring, parser: Parser | None, **options: Any) -> list[D
A list of docstring sections.
"""
if parser:
if isinstance(parser, str):
parser = Parser(parser)
return parsers[parser](docstring, **options) # type: ignore[operator]
return [DocstringSectionText(docstring.value)]

Expand Down

0 comments on commit ce59b7d

Please sign in to comment.