Skip to content

Commit

Permalink
Merge pull request #26 from jepperaskdk/bugfix/sphinx-generics
Browse files Browse the repository at this point in the history
Bugfix/sphinx generics
  • Loading branch information
jepperaskdk authored Oct 13, 2021
2 parents aeae3e4 + 833f655 commit c12bf97
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pydoctest/parsers/numpy_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self) -> None:
]
# Split by word, newline, a number of '-' and newline
self.section_regex = re.compile("([a-zA-Z]+)\n[-]+\n")
self.parameter_regex = re.compile(r"(\w+)\s*:\s*(\w+)")
self.returns_with_name_regex = re.compile(r"(\w+)\s*:\s*(\w+)")
self.parameter_regex = re.compile(r"(\w+)\s*:\s*([\w\[\], ]+)")
self.returns_with_name_regex = re.compile(r"(\w+)\s*:\s*([\w\[\], ]+)")

def get_exceptions_raised(self, doc: str) -> List[str]:
"""Returns the exceptions listed as raised in the docstring.
Expand Down
4 changes: 2 additions & 2 deletions pydoctest/parsers/sphinx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(self) -> None:
"""Parser for Sphinx docstring style."""
super().__init__()
self.parameter_name_regex = re.compile(r":param\s+(\w+):")
self.parameter_type_regex = re.compile(r":type\s+\w+:\s*(\w+)")
self.return_type_regex = re.compile(r":rtype:\s*(\w+)")
self.parameter_type_regex = re.compile(r":type\s+\w+:\s*([\w\[\], ]+)")
self.return_type_regex = re.compile(r":rtype:\s*([\w\[\], ]+)")
self.raises_regex = re.compile(r":raises\s+(\w+):")

def get_exceptions_raised(self, doc: str) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion pydoctest/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.1.13"
VERSION = "0.1.14"
18 changes: 18 additions & 0 deletions tests/test_parsers/numpy_class.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict


def failing_global_function(a: int) -> int:
"""[summary]
Expand Down Expand Up @@ -137,6 +139,22 @@ def func_parse_exception(self, a: int) -> int:


class CorrectTestClass():

def func_with_generics(self, a_a: Dict[str, Any]) -> Dict[str, Any]:
"""[summary]
Parameters
----------
a_a : Dict[str, Any]
[description]
Returns
-------
Dict[str, Any]
[description]
"""
pass

def empty_func(self) -> None:
"""Empty func
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parsers/sphinx_class.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any, Dict


def failing_global_function(a: int) -> int:
Expand Down Expand Up @@ -105,6 +106,16 @@ def func_parse_exception(self, a: int) -> int:


class CorrectTestClass():
def func_with_generics(self, a_a: Dict[str, Any]) -> Dict[str, Any]:
"""[summary]
:param a_a: [description]
:type a_a: Dict[str, Any]
:return: [description]
:rtype: Dict[str, Any]
"""
pass

def empty_func(self) -> None:
"""Empty func
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parsers/test_numpy_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pydoc
from typing import Any, Dict
import pytest

from pydoctest.parsers.numpy_parser import NumpyParser
Expand Down Expand Up @@ -146,3 +147,14 @@ def test_func_with_raise(self) -> None:

intersection = set(expected_exceptions) - set(actual_exceptions)
assert len(intersection) == 0

def test_func_with_generics(self) -> None:
parser = NumpyParser()
doc = pydoc.getdoc(tests.test_parsers.numpy_class.CorrectTestClass.func_with_generics)
parameters = parser.get_parameters(doc, tests.test_parsers.numpy_class)
assert len(parameters) == 1
assert parameters[0].type == Dict[str, Any]
assert parameters[0].name == 'a_a'

return_type = parser.get_return_type(doc, tests.test_parsers.numpy_class)
assert return_type == Dict[str, Any]
12 changes: 12 additions & 0 deletions tests/test_parsers/test_sphinx_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pydoc
from typing import Dict, Any
import pytest

from pydoctest.parsers.sphinx_parser import SphinxParser
Expand Down Expand Up @@ -139,3 +140,14 @@ def test_func_with_raise(self) -> None:

intersection = set(expected_exceptions) - set(actual_exceptions)
assert len(intersection) == 0

def test_func_with_generics(self) -> None:
parser = SphinxParser()
doc = pydoc.getdoc(tests.test_parsers.sphinx_class.CorrectTestClass.func_with_generics)
parameters = parser.get_parameters(doc, tests.test_parsers.sphinx_class)
assert len(parameters) == 1
assert parameters[0].type == Dict[str, Any]
assert parameters[0].name == 'a_a'

return_type = parser.get_return_type(doc, tests.test_parsers.sphinx_class)
assert return_type == Dict[str, Any]

0 comments on commit c12bf97

Please sign in to comment.