From 1e01d2f5e7f0e1c3ebbcae20d69d83b91b53cfef Mon Sep 17 00:00:00 2001 From: rr- Date: Wed, 26 May 2021 17:10:09 +0200 Subject: [PATCH] misc: move Style to common and STYLES to parser --- docstring_parser/__init__.py | 2 +- docstring_parser/common.py | 10 +++++++++- docstring_parser/parser.py | 14 +++++++++++--- docstring_parser/styles.py | 19 ------------------- docstring_parser/tests/test_google.py | 6 +++--- 5 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 docstring_parser/styles.py diff --git a/docstring_parser/__init__.py b/docstring_parser/__init__.py index c69dd3f..56c4870 100644 --- a/docstring_parser/__init__.py +++ b/docstring_parser/__init__.py @@ -8,9 +8,9 @@ DocstringRaises, DocstringReturns, ParseError, + Style, ) from .parser import parse -from .styles import Style __all__ = [ "parse", diff --git a/docstring_parser/common.py b/docstring_parser/common.py index 527c6a0..e1166bb 100644 --- a/docstring_parser/common.py +++ b/docstring_parser/common.py @@ -1,4 +1,5 @@ """Common methods for parsing.""" +import enum import typing as T PARAM_KEYWORDS = { @@ -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: diff --git a/docstring_parser/parser.py b/docstring_parser/parser.py index a231ba2..042d146 100644 --- a/docstring_parser/parser.py +++ b/docstring_parser/parser.py @@ -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: @@ -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] diff --git a/docstring_parser/styles.py b/docstring_parser/styles.py deleted file mode 100644 index 29dde77..0000000 --- a/docstring_parser/styles.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Style enum declaration.""" - -import enum - -from . import google, numpydoc, rest - - -class Style(enum.Enum): - rest = 1 - google = 2 - numpydoc = 3 - auto = 255 - - -STYLES = { - Style.rest: rest.parse, - Style.google: google.parse, - Style.numpydoc: numpydoc.parse, -} diff --git a/docstring_parser/tests/test_google.py b/docstring_parser/tests/test_google.py index a725b7f..5f71b9f 100644 --- a/docstring_parser/tests/test_google.py +++ b/docstring_parser/tests/test_google.py @@ -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 """ @@ -596,7 +596,7 @@ def test_broken_meta() -> None: def test_unknown_meta() -> None: docstring = parse( """Short desc - + Unknown 0: title0: content0