-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: update parser to correctly parse desired tokens #55
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa1c7b8
fix: correct parser to scan specific tokens only
dandhlee 5830e6d
fix: update parser for varying input types
dandhlee aa9abfc
test: add unittest for extract_docstring_info
dandhlee 18757e8
fix: update parser and test
dandhlee 7baff61
Merge branch 'master' of github.com:googleapis/sphinx-docfx-yaml into…
dandhlee aabd1d3
fix: update to return summary directly
dandhlee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from docfx_yaml.extension import find_unique_name | ||
from docfx_yaml.extension import disambiguate_toc_name | ||
from docfx_yaml.extension import _extract_docstring_info | ||
|
||
import unittest | ||
|
||
|
@@ -44,5 +45,132 @@ def test_disambiguate_toc_name(self): | |
|
||
self.assertEqual(yaml_want, yaml_got) | ||
|
||
# Variables used for testing _extract_docstring_info | ||
top_summary1_want = "\nSimple test for docstring.\n\n" | ||
summary_info1_want = { | ||
'variables': { | ||
'arg1': { | ||
'var_type': 'int', | ||
'description': 'simple description.' | ||
}, | ||
'arg2': { | ||
'var_type': 'str', | ||
'description': 'simple description for `arg2`.' | ||
} | ||
}, | ||
'returns': [ | ||
{ | ||
'var_type': 'str', | ||
'description': 'simple description for return value.' | ||
} | ||
], | ||
'exceptions': [ | ||
{ | ||
'var_type': 'AttributeError', | ||
'description': 'if `condition x`.' | ||
} | ||
] | ||
} | ||
|
||
|
||
def test_extract_docstring_info_normal_input(self): | ||
|
||
## Test for normal input | ||
summary_info1_got = { | ||
'variables': {}, | ||
'returns': [], | ||
'exceptions': [] | ||
} | ||
|
||
summary1 = """ | ||
Simple test for docstring. | ||
|
||
Args: | ||
arg1(int): simple description. | ||
arg2(str): simple description for `arg2`. | ||
|
||
Returns: | ||
str: simple description for return value. | ||
|
||
Raises: | ||
AttributeError: if `condition x`. | ||
""" | ||
|
||
top_summary1_got = _extract_docstring_info(summary_info1_got, summary1, "") | ||
|
||
self.assertEqual(top_summary1_got, self.top_summary1_want) | ||
self.assertEqual(summary_info1_got, self.summary_info1_want) | ||
|
||
|
||
def test_extract_docstring_info_mixed_format(self): | ||
## Test for input coming in mixed format. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider creating a separate test case for each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
summary2 = """ | ||
Simple test for docstring. | ||
|
||
:type arg1: int | ||
:param arg1: simple description. | ||
:param arg2: simple description for `arg2`. | ||
:type arg2: str | ||
|
||
:rtype: str | ||
:returns: simple description for return value. | ||
|
||
:raises AttributeError: if `condition x`. | ||
""" | ||
|
||
summary_info2_got = { | ||
'variables': {}, | ||
'returns': [], | ||
'exceptions': [] | ||
} | ||
|
||
top_summary2_got = _extract_docstring_info(summary_info2_got, summary2, "") | ||
|
||
# Output should be same as test 1 with normal input. | ||
self.assertEqual(top_summary2_got, self.top_summary1_want) | ||
self.assertEqual(summary_info2_got, self.summary_info1_want) | ||
|
||
|
||
def test_extract_docstring_info_check_parser(self): | ||
## Test for parser to correctly scan docstring tokens and not custom fields | ||
summary_info3_want = { | ||
'variables': {}, | ||
'returns': [], | ||
'exceptions': [] | ||
} | ||
|
||
summary3 = """ | ||
Union[int, None]: Expiration time in milliseconds for a partition. | ||
|
||
If :attr:`partition_expiration` is set and <xref:type_> is | ||
not set, :attr:`type_` will default to | ||
:attr:`~google.cloud.bigquery.table.TimePartitioningType.DAY`. | ||
It could return :param: with :returns as well. | ||
""" | ||
|
||
summary_info3_got = { | ||
'variables': {}, | ||
'returns': [], | ||
'exceptions': [] | ||
} | ||
|
||
# Nothing should change | ||
top_summary3_want = summary3 | ||
|
||
top_summary3_got = _extract_docstring_info(summary_info3_got, summary3, "") | ||
|
||
self.assertEqual(top_summary3_got, top_summary3_want) | ||
self.assertEqual(summary_info3_got, summary_info3_want) | ||
|
||
def test_extract_docstring_info_check_error(self): | ||
## Test for incorrectly formmatted docstring raising error | ||
summary4 = """ | ||
Description of docstring which should fail. | ||
|
||
:returns:param: | ||
""" | ||
with self.assertRaises(ValueError): | ||
_extract_docstring_info({}, summary4, "error string") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we return at this point and avoid needing the
else
(and the indentation that comes with it)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just
return summary
directly?