Skip to content

Commit

Permalink
[cdd/docstring/utils/parse_utils.py] Expand parse_adhoc_doc_for_typ
Browse files Browse the repository at this point in the history
… to check for 3-tuple if nothing matches at the end (finds another adhoc `float` definition)
  • Loading branch information
SamuelMarks committed Dec 17, 2023
1 parent c03af63 commit 3d34da8
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 135 deletions.
29 changes: 22 additions & 7 deletions cdd/docstring/utils/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from operator import contains, itemgetter
from typing import List, Union

from cdd.shared.defaults_utils import extract_default
from cdd.shared.pure_utils import count_iter_items, pp, sliding_window, type_to_name
from cdd.shared.pure_utils import count_iter_items, sliding_window, type_to_name

adhoc_type_to_type = {
"bool": "bool",
Expand Down Expand Up @@ -44,6 +43,7 @@
("True", " ", "on"): "bool",
("called", " ", "at"): "collections.abc.Callable",
("directory", " ", "where"): "str",
("floating", " ", "point"): "float",
}

adhoc_3_tuple_to_collection = {
Expand Down Expand Up @@ -192,7 +192,7 @@ def _union_literal_from_sentence(sentence):
return None


def parse_adhoc_doc_for_typ(doc, name):
def parse_adhoc_doc_for_typ(doc, name, default_is_none):
"""
Google's Keras and other frameworks have an adhoc syntax.
Expand All @@ -204,13 +204,18 @@ def parse_adhoc_doc_for_typ(doc, name):
:param name: Name of argument; useful for debugging and if the name hints as to the type
:type name: ```str```
:param default_is_none: Whether the default is `NoneStr`
:type default_is_none: ```bool```
:return: The type (if determined) else `None`
:rtype: ```Optional[str]```
"""

if not doc:
return None

wrap = "Optional[{}]" if default_is_none else "{}"

words: List[Union[List[str], str]] = [[]]
word_chars = "{0}{1}`'\"/|".format(string.digits, string.ascii_letters)
sentence_ends = -1
Expand Down Expand Up @@ -290,11 +295,21 @@ def parse_adhoc_doc_for_typ(doc, name):
return whole_sentence_as_type
if candidate_type is not None:
return candidate_type
elif len(words) > 2 and "/" in words[2]:
return "Union[{}]".format(",".join(sorted(words[2].split("/"))))
elif len(words) > 2:
if "/" in words[2]:
return "Union[{}]".format(",".join(sorted(words[2].split("/"))))
candidate_type = next(
map(
adhoc_3_tuple_to_type.__getitem__,
filter(
partial(contains, adhoc_3_tuple_to_type),
sliding_window(words, 3),
),
),
None,
)

pp({"{}::extract_default".format(name): extract_default(doc)})
return None
return candidate_type if candidate_type is None else wrap.format(candidate_type)


__all__ = ["parse_adhoc_doc_for_typ"]
14 changes: 7 additions & 7 deletions cdd/shared/docstring_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import ast
import collections # noqa
import sys
from ast import AST
from collections import OrderedDict
Expand Down Expand Up @@ -43,7 +44,6 @@
location_within,
none_types,
paren_wrap_code,
pp,
rpartial,
unquote,
update_d,
Expand Down Expand Up @@ -231,7 +231,7 @@ def _scan_phase_numpydoc_and_google(
scanned["doc"] = docstring
return scanned

# Scan all lines so that that each element in `stacker` refers to one 'unit'
# Scan all lines so that each element in `stacker` refers to one 'unit'
stacker, docstring_lines, line = [], docstring.splitlines(), None
first_indent = (
count_iter_items(takewhile(str.isspace, docstring_lines[0]))
Expand Down Expand Up @@ -565,12 +565,12 @@ def _set_name_and_type(param, infer_type, word_wrap, none_default_for_kwargs=Fal
else _param["doc"]
).rstrip()

typ = parse_adhoc_doc_for_typ(_param["doc"], name)
typ = parse_adhoc_doc_for_typ(
_param["doc"], name, _param.get("default") == NoneStr
)
if typ is not None:
try:
Int, Float, String, Bool = (None,) * 4
eval(typ, globals(), locals())
del Int, Float, String, Bool
_param["typ"] = typ
except (NameError, SyntaxError) as e:
print(e, file=sys.stderr)
Expand All @@ -582,8 +582,8 @@ def _set_name_and_type(param, infer_type, word_wrap, none_default_for_kwargs=Fal
):
_param["typ"] = "Optional[{typ}]".format(typ=_param["typ"])

pp({"b4": was, "l8": _param})
print("*" * 100, file=sys.stderr)
# pp({"b4": was, "l8": _param})
# print("*" * 100, file=sys.stderr)
return name, _param


Expand Down
Loading

0 comments on commit 3d34da8

Please sign in to comment.