def test_from_docstring_google_str(self) -> None: """ Tests whether `parse_docstring` produces `intermediate_repr_no_default_doc` from `docstring_google_str` """ ir = parse_docstring(docstring_google_str) self.assertDictEqual(ir, intermediate_repr_no_default_doc)
def test_ir_equality(self) -> None: """ Tests whether `parse_docstring` produces `intermediate_repr` from `docstring_str`""" self.assertDictEqual( parse_docstring(docstring_str), intermediate_repr_no_default_doc )
def test_intermediate_repr_only_return_type_equality(self) -> None: """ Tests whether `parse_docstring` produces `docstring_str_no_default_doc` from `docstring_str_no_default_doc`""" self.assertDictEqual( parse_docstring(docstring_only_return_type_str, emit_default_doc=False), intermediate_repr_only_return_type, )
def test_intermediate_repr_extra_colons_equality(self) -> None: """ Tests whether `parse_docstring` produces `docstring_str_no_default_doc` from `docstring_str_no_default_doc`""" self.assertDictEqual( parse_docstring(docstring_extra_colons_str, emit_default_doc=False), intermediate_repr_extra_colons, )
def test_from_docstring_google_tf_adadelta_str(self) -> None: """ Tests whether `parse_docstring` produces the right IR from `docstring_google_tf_adadelta_str` """ self.assertDictEqual( parse_docstring(docstring_google_tf_adadelta_str, emit_default_doc=True, infer_type=True), docstring_google_tf_adadelta_ir, )
def test_intermediate_repr_no_default_doc_equality(self) -> None: """ Tests whether `parse_docstring` produces `docstring_str_no_default_doc` from `docstring_str_no_default_doc`""" ir = parse_docstring( docstring_no_default_doc_str, emit_default_doc=False, emit_default_prop=False, ) self.assertDictEqual( ir, intermediate_repr_no_default_doc_or_prop, )
def test_from_docstring_google_pytorch_lbfgs_str(self) -> None: """ Tests whether `parse_docstring` produces the right IR from `docstring_google_pytorch_lbfgs_str` """ self.assertDictEqual( parse_docstring( docstring_google_pytorch_lbfgs_str, emit_default_doc=False, infer_type=True, ), docstring_google_pytorch_lbfgs_ir, )
def test_from_docstring_google_tf_squared_hinge(self) -> None: """ Tests whether `parse_docstring` produces the right IR from `docstring_google_tf_squared_hinge_str` """ self.assertDictEqual( parse_docstring( docstring_google_tf_squared_hinge_str, emit_default_doc=True, infer_type=True, default_search_announce=("Default value is", "defaults to"), ), docstring_google_tf_squared_hinge_ir, )
def docstring( doc_string, infer_type=False, return_tuple=False, emit_default_prop=True, emit_default_doc=True, ): """ Converts a docstring to an AST :param doc_string: docstring portion :type doc_string: ```Union[str, Dict]``` :param infer_type: Whether to try inferring the typ (from the default) :type infer_type: ```bool``` :param return_tuple: Whether to return a tuple, or just the intermediate_repr :type return_tuple: ```bool``` :param emit_default_prop: Whether to include the default dictionary property. :type emit_default_prop: ```bool``` :param emit_default_doc: Whether help/docstring should include 'With default' text :type emit_default_doc: ```bool``` :return: intermediate_repr, whether it returns or not :rtype: ```Optional[Union[dict, Tuple[dict, bool]]]``` """ assert isinstance(doc_string, str), "Expected 'str' got {!r}".format( type(doc_string).__name__ ) parsed = ( doc_string if isinstance(doc_string, dict) else parse_docstring( doc_string, infer_type=infer_type, emit_default_prop=emit_default_prop, emit_default_doc=emit_default_doc, ) ) if return_tuple: return parsed, ( "returns" in parsed and parsed["returns"] is not None and "return_type" in (parsed.get("returns") or iter(())) ) return parsed
def test_docstring_header_and_return_str(self) -> None: """ Tests that `docstring_header_and_return_str` can produce IR """ _intermediate_repr = deepcopy(intermediate_repr_no_default_doc) _intermediate_repr["params"] = OrderedDict() self.assertDictEqual(parse_docstring(docstring_header_and_return_str), _intermediate_repr)
def argparse_ast(function_def, function_type=None, function_name=None): """ Converts an argparse AST to our IR :param function_def: AST of argparse function_def :type function_def: ```FunctionDef``` :param function_type: Type of function, static is static or global method, others just become first arg :type function_type: ```Literal['self', 'cls', 'static']``` :param function_name: name of function_def :type function_name: ```str``` :returns: a dictionary of form { "name": Optional[str], "type": Optional[str], "doc": Optional[str], "params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}] "returns": Optional[OrderedDict[Literal['return_type'], {'typ': str, 'doc': Optional[str], 'default': Any}),)]] } :rtype: ```dict``` """ assert isinstance(function_def, FunctionDef), "Expected 'FunctionDef' got `{!r}`".format( type(function_def).__name__) doc_string = get_docstring(function_def) intermediate_repr = { "name": function_name, "type": function_type or get_function_type(function_def), "doc": "", "params": OrderedDict(), } ir = parse_docstring(doc_string, emit_default_doc=True) # Whether a default is required, if not found in doc, infer the proper default from type require_default = False # Parse all relevant nodes from function body body = function_def.body if doc_string is None else function_def.body[1:] for node in body: if is_argparse_add_argument(node): name, _param = parse_out_param(node, emit_default_doc=False, require_default=require_default) (intermediate_repr["params"][name].update if name in intermediate_repr["params"] else partial( setitem, intermediate_repr["params"], name))(_param) if not require_default and _param.get("default") is not None: require_default = True elif isinstance(node, Assign) and is_argparse_description(node): intermediate_repr["doc"] = get_value(node.value) elif isinstance(node, Return) and isinstance(node.value, Tuple): intermediate_repr["returns"] = OrderedDict((_parse_return( node, intermediate_repr=ir, function_def=function_def, emit_default_doc=False, ), )) inner_body = list( filterfalse( is_argparse_description, filterfalse(is_argparse_add_argument, body), )) if inner_body: intermediate_repr["_internal"] = { "body": inner_body, "from_name": function_def.name, "from_type": "static", } # if "return_type" in intermediate_repr.get("returns", {}): # pp({'intermediate_repr["returns"]["return_type"]': intermediate_repr["returns"]["return_type"]}) # intermediate_repr["returns"]["return_type"].update = dict( # interpolate_defaults(intermediate_repr["returns"]["return_type"]) # ) return intermediate_repr