Skip to content

Commit

Permalink
misc: move Style to common and STYLES to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed May 26, 2021
1 parent 0b92d19 commit 1e01d2f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docstring_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
DocstringRaises,
DocstringReturns,
ParseError,
Style,
)
from .parser import parse
from .styles import Style

__all__ = [
"parse",
Expand Down
10 changes: 9 additions & 1 deletion docstring_parser/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common methods for parsing."""
import enum
import typing as T

PARAM_KEYWORDS = {
Expand All @@ -18,7 +19,14 @@
class ParseError(RuntimeError):
"""Base class for all parsing related errors."""

pass

class Style(enum.Enum):
"""Docstring style."""

rest = 1
google = 2
numpydoc = 3
auto = 255


class DocstringMeta:
Expand Down
14 changes: 11 additions & 3 deletions docstring_parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""The main parsing routine."""

from docstring_parser.common import Docstring, ParseError
from docstring_parser.styles import STYLES, Style
from docstring_parser import google, numpydoc, rest
from docstring_parser.common import Docstring, ParseError, Style

STYLES = {
Style.rest: rest.parse,
Style.google: google.parse,
Style.numpydoc: numpydoc.parse,
}


def parse(text: str, style: Style = Style.auto) -> Docstring:
Expand All @@ -11,15 +17,17 @@ def parse(text: str, style: Style = Style.auto) -> Docstring:
:param style: docstring style
:returns: parsed docstring representation
"""

if style != Style.auto:
return STYLES[style](text)

rets = []
for parse_ in STYLES.values():
try:
rets.append(parse_(text))
except ParseError as e:
exc = e

if not rets:
raise exc

return sorted(rets, key=lambda d: len(d.meta), reverse=True)[0]
19 changes: 0 additions & 19 deletions docstring_parser/styles.py

This file was deleted.

6 changes: 3 additions & 3 deletions docstring_parser/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ def test_default_args():
"""A sample function
A function the demonstrates docstrings
Args:
arg1 (int): The firsty arg
arg2 (str): The second arg
arg3 (float, optional): The third arg. Defaults to 1.0.
arg4 (Optional[Dict[str, Any]], optional): The fourth arg. Defaults to None.
arg5 (str, optional): The fifth arg. Defaults to DEFAULT_ARG5.
Returns:
Mapping[str, Any]: The args packed in a mapping
"""
Expand Down Expand Up @@ -596,7 +596,7 @@ def test_broken_meta() -> None:
def test_unknown_meta() -> None:
docstring = parse(
"""Short desc
Unknown 0:
title0: content0
Expand Down

0 comments on commit 1e01d2f

Please sign in to comment.