def test_to_class_from_argparse_action_append_ast(self) -> None: """ Tests whether a class from an argparse function with `nargs` set """ run_ast_test( self, emit.class_( parse.argparse_ast(argparse_func_action_append_ast), ), gold=class_nargs_ast, )
def test_to_class_from_argparse_ast(self) -> None: """ Tests whether `class_` produces `class_ast` given `argparse_func_ast` """ run_ast_test( self, gen_ast=emit.class_( parse.argparse_ast(argparse_func_ast), emit_default_doc=True ), gold=class_ast, )
def test_to_class_from_docstring_str(self) -> None: """ Tests whether `class_` produces `class_ast` given `docstring_str` """ run_ast_test( self, emit.class_( parse.docstring(docstring_str, emit_default_doc=True), emit_default_doc=True, ), gold=class_ast, )
def test_from_function_google_tf_squared_hinge_str_to_class(self) -> None: """ Tests that `emit.function` produces correctly with: - __call__ """ gen_ast = emit.class_( parse.function( ast.parse(function_google_tf_squared_hinge_str).body[0], infer_type=True, ), class_name="SquaredHingeConfig", emit_call=True, emit_default_doc=True, ) run_ast_test( self, gen_ast=gen_ast, gold=class_squared_hinge_config_ast, )
def test_ground_truth_changes(self) -> None: """ Time for a new master. """ ir = deepcopy(intermediate_repr) ir["returns"]["return_type"]["typ"] = "Tuple[np.ndarray, np.ndarray]" with TemporaryDirectory() as tempdir: self.assertTupleEqual( tuple( map( lambda filename_unmodified: ( os.path.basename(filename_unmodified[0]), filename_unmodified[1], ), self.ground_truth_tester( tempdir=tempdir, _class_ast=emit.class_(ir, emit_default_doc=False), )[0].items(), )), (("argparse.py", False), ("classes.py", True), ("methods.py", False)), )
def populate_files(tempdir, input_module_str=None): """ Populate files in the tempdir :param tempdir: Temporary directory :type tempdir: ```str``` :param input_module_str: Input string to write to the input_filename. If None, uses preset mock module. :type input_module_str: ```Optional[str]``` :return: input filename, input str, expected_output :rtype: ```Tuple[str, str, str, Module]``` """ input_filename = os.path.join(tempdir, "input.py") input_class_name = "Foo" input_class_ast = emit.class_( parse.function(deepcopy(method_adder_ast)), emit_call=False, class_name=input_class_name, ) input_module_ast = Module( body=[ input_class_ast, Assign( targets=[Name("input_map", Store())], value=Dict( keys=[set_value(input_class_name)], values=[Name(input_class_name, Load())], expr=None, ), expr=None, lineno=None, **maybe_type_comment ), Assign( targets=[Name("__all__", Store())], value=List( ctx=Load(), elts=[set_value(input_class_name), set_value("input_map")], expr=None, ), expr=None, lineno=None, **maybe_type_comment ), ], type_ignores=[], stmt=None, ) input_module_str = input_module_str or to_code(input_module_ast) # expected_output_class_str = ( # "class FooConfig(object):\n" # ' """\n' # " The amazing Foo\n\n" # " :cvar a: An a. Defaults to 5\n" # ' :cvar b: A b. Defaults to 16"""\n' # " a = 5\n" # " b = 16\n\n" # " def __call__(self):\n" # " self.a = 5\n" # " self.b = 16\n" # ) expected_class_ast = emit.class_( parse.function(deepcopy(method_adder_ast)), emit_call=True, class_name="{input_class_name}Config".format(input_class_name=input_class_name), ) with open(input_filename, "wt") as f: f.write(input_module_str) return input_filename, input_module_ast, input_class_ast, expected_class_ast