Skip to content

Commit

Permalink
Fix Google dialect match regexp (Neoteroi#471)
Browse files Browse the repository at this point in the history
* Fix Google dialect match regexp

Update tests to verify is_match for docstring dialects

* Fix code style
  • Loading branch information
tyzhnenko authored Jan 20, 2024
1 parent f034f6a commit bec8e14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion blacksheep/server/openapi/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def parse_docstring(self, docstring: str) -> DocstringInfo:

class GoogleDocDialect(IndentDocstringDialect):
def is_match(self, docstring: str) -> bool:
return re.search(r"^([\s\t]*Args:?)", docstring) is not None
return re.search(r"^([\s\t]*Args:?)", docstring, re.M) is not None

_sections = {"Args", "Returns", "Raises"}

Expand Down
27 changes: 23 additions & 4 deletions tests/test_openapi_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@pytest.mark.parametrize(
"docstring,expected_info",
"docstring,expected_info,match",
[
(
"""
Expand All @@ -33,6 +33,7 @@
"c": ParameterInfo("list of str", value_type=List[str]),
},
),
True,
),
(
"""
Expand All @@ -54,6 +55,7 @@
"c": ParameterInfo("list of str", value_type=List[str]),
},
),
True,
),
(
"""
Expand Down Expand Up @@ -88,6 +90,7 @@
return_type=float,
return_description="the x intercept of the line M{y=m*x+b}.",
),
True,
),
(
"""
Expand Down Expand Up @@ -124,6 +127,7 @@
return_type=float,
return_description="the x intercept of the line M{y=m*x+b}.",
),
True,
),
(
"""
Expand All @@ -141,6 +145,7 @@
return_type=None,
return_description=None,
),
True,
),
(
"""
Expand All @@ -160,6 +165,7 @@
return_type=None,
return_description=None,
),
True,
),
(
"""
Expand All @@ -179,6 +185,7 @@
return_type=None,
return_description=None,
),
True,
),
(
"""
Expand All @@ -198,6 +205,7 @@
},
return_description="this is a description of what is returned",
),
True,
),
(
"""
Expand Down Expand Up @@ -228,11 +236,13 @@
),
parameters={},
),
False,
),
],
)
def test_epytext_dialect(docstring, expected_info):
def test_epytext_dialect(docstring, expected_info, match):
dialect = EpytextDialect()
assert dialect.is_match(docstring) is match

info = dialect.parse_docstring(docstring)
assert expected_info == info
Expand Down Expand Up @@ -371,13 +381,14 @@ def test_epytext_dialect(docstring, expected_info):
)
def test_rest_dialect(docstring, expected_info):
dialect = ReStructuredTextDialect()
assert dialect.is_match(docstring)

info = dialect.parse_docstring(docstring)
assert expected_info == info


@pytest.mark.parametrize(
"docstring,expected_info",
"docstring,expected_info,match",
[
(
"""
Expand Down Expand Up @@ -424,6 +435,7 @@ def test_rest_dialect(docstring, expected_info):
return_type=str,
return_description="a value in a string",
),
True,
),
(
"""
Expand All @@ -447,6 +459,7 @@ def test_rest_dialect(docstring, expected_info):
"c": ParameterInfo("list of str", value_type=List[str]),
},
),
True,
),
(
"""
Expand Down Expand Up @@ -492,6 +505,7 @@ def test_rest_dialect(docstring, expected_info):
return_type=None,
return_description=None,
),
True,
),
(
"""
Expand Down Expand Up @@ -538,6 +552,7 @@ def test_rest_dialect(docstring, expected_info):
return_type=str,
return_description="a value in a string",
),
True,
),
(
"""
Expand Down Expand Up @@ -578,6 +593,7 @@ def test_rest_dialect(docstring, expected_info):
return_type=str,
return_description="a value in a string",
),
True,
),
(
"""
Expand All @@ -592,11 +608,13 @@ def test_rest_dialect(docstring, expected_info):
description="Lorem ipsum dolor sit amet.",
parameters={},
),
False,
),
],
)
def test_numpydoc_dialect(docstring, expected_info):
def test_numpydoc_dialect(docstring, expected_info, match):
dialect = NumpydocDialect()
assert dialect.is_match(docstring) is match

info = dialect.parse_docstring(docstring)
assert expected_info == info
Expand Down Expand Up @@ -777,6 +795,7 @@ def test_googledoc_dialect_warns_about_invalid_parameter():
)
def test_googledoc_dialect(docstring, expected_info):
dialect = GoogleDocDialect()
assert dialect.is_match(docstring)

info = dialect.parse_docstring(docstring)
assert expected_info == info

0 comments on commit bec8e14

Please sign in to comment.