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 test_from_docstring_google_tf_lambda_callback_str(self) -> None: """ Tests whether `parse_docstring` produces the right IR from `docstring_google_tf_lambda_callback_str` """ self.assertDictEqual( parse_docstring( docstring_google_tf_lambda_callback_str, emit_default_doc=True, infer_type=True, parse_original_whitespace=True, ), docstring_google_tf_lambda_callback_ir, )
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_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 `{node_name!r}`".format( node_name=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", } return intermediate_repr