diff --git a/docstring_parser/epydoc.py b/docstring_parser/epydoc.py index 4d4dde6..96c9726 100644 --- a/docstring_parser/epydoc.py +++ b/docstring_parser/epydoc.py @@ -26,7 +26,7 @@ def _clean_str(string: str) -> T.Optional[str]: return None -def parse(text: str) -> Docstring: +def parse(text: T.Optional[str]) -> Docstring: """Parse the epydoc-style docstring into its components. :returns: parsed docstring diff --git a/docstring_parser/google.py b/docstring_parser/google.py index aa4284e..244e698 100644 --- a/docstring_parser/google.py +++ b/docstring_parser/google.py @@ -202,7 +202,7 @@ def add_section(self, section: Section): self.sections[section.title] = section self._setup() - def parse(self, text: str) -> Docstring: + def parse(self, text: T.Optional[str]) -> Docstring: """Parse the Google-style docstring into its components. :returns: parsed docstring @@ -293,7 +293,7 @@ def parse(self, text: str) -> Docstring: return ret -def parse(text: str) -> Docstring: +def parse(text: T.Optional[str]) -> Docstring: """Parse the Google-style docstring into its components. :returns: parsed docstring diff --git a/docstring_parser/numpydoc.py b/docstring_parser/numpydoc.py index 836e776..eca5233 100644 --- a/docstring_parser/numpydoc.py +++ b/docstring_parser/numpydoc.py @@ -325,7 +325,7 @@ def add_section(self, section: Section): self.sections[section.title] = section self._setup() - def parse(self, text: str) -> Docstring: + def parse(self, text: T.Optional[str]) -> Docstring: """Parse the numpy-style docstring into its components. :returns: parsed docstring @@ -370,7 +370,7 @@ def parse(self, text: str) -> Docstring: return ret -def parse(text: str) -> Docstring: +def parse(text: T.Optional[str]) -> Docstring: """Parse the numpy-style docstring into its components. :returns: parsed docstring diff --git a/docstring_parser/parser.py b/docstring_parser/parser.py index 2710e63..c769a1f 100644 --- a/docstring_parser/parser.py +++ b/docstring_parser/parser.py @@ -20,7 +20,9 @@ } -def parse(text: str, style: DocstringStyle = DocstringStyle.AUTO) -> Docstring: +def parse( + text: T.Optional[str], style: DocstringStyle = DocstringStyle.AUTO +) -> Docstring: """Parse the docstring into its components. :param text: docstring text to parse diff --git a/docstring_parser/rest.py b/docstring_parser/rest.py index b707df8..772cf2f 100644 --- a/docstring_parser/rest.py +++ b/docstring_parser/rest.py @@ -99,7 +99,7 @@ def _build_meta(args: T.List[str], desc: str) -> DocstringMeta: return DocstringMeta(args=args, description=desc) -def parse(text: str) -> Docstring: +def parse(text: T.Optional[str]) -> Docstring: """Parse the ReST-style docstring into its components. :returns: parsed docstring diff --git a/docstring_parser/tests/test_epydoc.py b/docstring_parser/tests/test_epydoc.py index 965b440..e9821dc 100644 --- a/docstring_parser/tests/test_epydoc.py +++ b/docstring_parser/tests/test_epydoc.py @@ -10,6 +10,7 @@ @pytest.mark.parametrize( "source, expected", [ + pytest.param(None, None, id="No __doc__"), ("", None), ("\n", None), ("Short description", "Short description"), @@ -17,7 +18,9 @@ ("\n Short description\n", "Short description"), ], ) -def test_short_description(source: str, expected: str) -> None: +def test_short_description( + source: T.Optional[str], expected: T.Optional[str] +) -> None: """Test parsing short description.""" docstring = parse(source) assert docstring.short_description == expected diff --git a/docstring_parser/tests/test_google.py b/docstring_parser/tests/test_google.py index b32c04e..5a3570a 100644 --- a/docstring_parser/tests/test_google.py +++ b/docstring_parser/tests/test_google.py @@ -143,6 +143,7 @@ def test_google_parser_custom_sections_after() -> None: @pytest.mark.parametrize( "source, expected", [ + pytest.param(None, None, id="No __doc__"), ("", None), ("\n", None), ("Short description", "Short description"), @@ -150,7 +151,9 @@ def test_google_parser_custom_sections_after() -> None: ("\n Short description\n", "Short description"), ], ) -def test_short_description(source: str, expected: str) -> None: +def test_short_description( + source: T.Optional[str], expected: T.Optional[str] +) -> None: """Test parsing short description.""" docstring = parse(source) assert docstring.short_description == expected diff --git a/docstring_parser/tests/test_numpydoc.py b/docstring_parser/tests/test_numpydoc.py index 2394c61..2f2ef42 100644 --- a/docstring_parser/tests/test_numpydoc.py +++ b/docstring_parser/tests/test_numpydoc.py @@ -9,6 +9,7 @@ @pytest.mark.parametrize( "source, expected", [ + pytest.param(None, None, id="No __doc__"), ("", None), ("\n", None), ("Short description", "Short description"), @@ -16,7 +17,9 @@ ("\n Short description\n", "Short description"), ], ) -def test_short_description(source: str, expected: str) -> None: +def test_short_description( + source: T.Optional[str], expected: T.Optional[str] +) -> None: """Test parsing short description.""" docstring = parse(source) assert docstring.short_description == expected diff --git a/docstring_parser/tests/test_parser.py b/docstring_parser/tests/test_parser.py index 73b8c9c..e624976 100644 --- a/docstring_parser/tests/test_parser.py +++ b/docstring_parser/tests/test_parser.py @@ -1,10 +1,34 @@ """Tests for generic docstring routines.""" +import typing as T + import pytest from docstring_parser.common import DocstringStyle, ParseError from docstring_parser.parser import parse +@pytest.mark.parametrize( + "source, expected", + [ + pytest.param(None, None, id="No __doc__"), + ("", None), + ("\n", None), + ("Short description", "Short description"), + ("\nShort description\n", "Short description"), + ("\n Short description\n", "Short description"), + ], +) +def test_short_description( + source: T.Optional[str], expected: T.Optional[str] +) -> None: + """Test parsing short description.""" + docstring = parse(source) + assert docstring.short_description == expected + assert docstring.description == expected + assert docstring.long_description is None + assert not docstring.meta + + def test_rest() -> None: """Test ReST-style parser autodetection.""" docstring = parse( diff --git a/docstring_parser/tests/test_rest.py b/docstring_parser/tests/test_rest.py index 411c277..19ba3d8 100644 --- a/docstring_parser/tests/test_rest.py +++ b/docstring_parser/tests/test_rest.py @@ -10,6 +10,7 @@ @pytest.mark.parametrize( "source, expected", [ + pytest.param(None, None, id="No __doc__"), ("", None), ("\n", None), ("Short description", "Short description"), @@ -17,7 +18,9 @@ ("\n Short description\n", "Short description"), ], ) -def test_short_description(source: str, expected: str) -> None: +def test_short_description( + source: T.Optional[str], expected: T.Optional[str] +) -> None: """Test parsing short description.""" docstring = parse(source) assert docstring.short_description == expected