-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#562) Signed-off-by: Sean Lin <[email protected]>
- Loading branch information
1 parent
7a4694a
commit e646831
Showing
10 changed files
with
168 additions
and
100 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Callable, Dict, Optional | ||
|
||
from docstring_parser import parse | ||
|
||
|
||
class Docstring(object): | ||
def __init__(self, docstring: str = None, callable_: Callable = None): | ||
if docstring is not None: | ||
self._parsed_docstring = parse(docstring) | ||
else: | ||
self._parsed_docstring = parse(callable_.__doc__) | ||
|
||
@property | ||
def input_descriptions(self) -> Dict[str, str]: | ||
return {p.arg_name: p.description for p in self._parsed_docstring.params} | ||
|
||
@property | ||
def output_descriptions(self) -> Dict[str, str]: | ||
return {p.return_name: p.description for p in self._parsed_docstring.many_returns} | ||
|
||
@property | ||
def short_description(self) -> Optional[str]: | ||
return self._parsed_docstring.short_description | ||
|
||
@property | ||
def long_description(self) -> Optional[str]: | ||
return self._parsed_docstring.long_description |
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
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import typing | ||
|
||
from flytekit.core.docstring import Docstring | ||
|
||
|
||
def test_get_variable_descriptions(): | ||
# sphinx style | ||
def z(a: int, b: str) -> typing.Tuple[int, str]: | ||
""" | ||
function z | ||
longer description here | ||
:param a: foo | ||
:param b: bar | ||
:return: ramen | ||
""" | ||
... | ||
|
||
docstring = Docstring(callable_=z) | ||
input_descriptions = docstring.input_descriptions | ||
output_descriptions = docstring.output_descriptions | ||
assert input_descriptions["a"] == "foo" | ||
assert input_descriptions["b"] == "bar" | ||
assert len(output_descriptions) == 1 | ||
assert next(iter(output_descriptions.items()))[1] == "ramen" | ||
assert docstring.short_description == "function z" | ||
assert docstring.long_description == "longer description here" | ||
|
||
# numpy style | ||
def z(a: int, b: str) -> typing.Tuple[int, str]: | ||
""" | ||
function z | ||
longer description here | ||
Parameters | ||
---------- | ||
a : int | ||
foo | ||
b : str | ||
bar | ||
Returns | ||
------- | ||
out : tuple | ||
ramen | ||
""" | ||
... | ||
|
||
docstring = Docstring(callable_=z) | ||
input_descriptions = docstring.input_descriptions | ||
output_descriptions = docstring.output_descriptions | ||
assert input_descriptions["a"] == "foo" | ||
assert input_descriptions["b"] == "bar" | ||
assert len(output_descriptions) == 1 | ||
assert next(iter(output_descriptions.items()))[1] == "ramen" | ||
assert docstring.short_description == "function z" | ||
assert docstring.long_description == "longer description here" | ||
|
||
# google style | ||
def z(a: int, b: str) -> typing.Tuple[int, str]: | ||
"""function z | ||
longer description here | ||
Args: | ||
a(int): foo | ||
b(str): bar | ||
Returns: | ||
str: ramen | ||
""" | ||
... | ||
|
||
docstring = Docstring(callable_=z) | ||
input_descriptions = docstring.input_descriptions | ||
output_descriptions = docstring.output_descriptions | ||
assert input_descriptions["a"] == "foo" | ||
assert input_descriptions["b"] == "bar" | ||
assert len(output_descriptions) == 1 | ||
assert next(iter(output_descriptions.items()))[1] == "ramen" | ||
assert docstring.short_description == "function z" | ||
assert docstring.long_description == "longer description here" | ||
|
||
# empty doc | ||
def z(a: int, b: str) -> typing.Tuple[int, str]: | ||
... | ||
|
||
docstring = Docstring(callable_=z) | ||
input_descriptions = docstring.input_descriptions | ||
output_descriptions = docstring.output_descriptions | ||
assert len(input_descriptions) == 0 | ||
assert len(output_descriptions) == 0 | ||
assert docstring.short_description is None | ||
assert docstring.long_description is None |
Oops, something went wrong.