Exemplo n.º 1
0
 def test_param2argparse_param_default_ast_expr_with_list(self) -> None:
     """
     Tests that param2argparse_param works to change the type based on the default
       whence said default is an ast.List inside an ast.Expr
     """
     run_ast_test(
         gen_ast=param2argparse_param(
             (
                 "byo",
                 {
                     "default": Expr(
                         List(
                             elts=[],
                             ctx=Load(),
                             expr=None,
                         ),
                         expr_value=None,
                     ),
                     "typ": "str",
                 },
             ),
         ),
         gold=argparse_add_argument_expr,
         test_case_instance=self,
     )
Exemplo n.º 2
0
 def test_param2argparse_param_default_simple_type(self) -> None:
     """
     Tests that param2argparse_param works to change the type based on the default
     """
     run_ast_test(
         gen_ast=param2argparse_param(
             ("byo", {"default": 5, "typ": "str"}),
         ),
         gold=Expr(
             Call(
                 args=[set_value("--byo")],
                 func=Attribute(
                     Name("argument_parser", Load()),
                     "add_argument",
                     Load(),
                 ),
                 keywords=[
                     keyword(arg="type", value=Name("int", Load()), identifier=None),
                     keyword(arg="required", value=set_value(True), identifier=None),
                     keyword(arg="default", value=set_value(5), identifier=None),
                 ],
                 expr=None,
                 expr_func=None,
             )
         ),
         test_case_instance=self,
     )
Exemplo n.º 3
0
    def test_param2argparse_param_default_function(self) -> None:
        """
        Tests that param2argparse_param works to change the type based on the default
          whence said default is an in-memory function
        """

        function_str = (
            "from operator import add\n"
            "def adder(a, b):\n"
            "{tab}return add(a, b)".format(tab=tab)
        )
        adder = getattr(
            inspectable_compile(function_str),
            "adder",
        )
        pickled_adder = pickle.dumps(adder)  # eww

        run_ast_test(
            gen_ast=param2argparse_param(
                (
                    "byo",
                    {
                        "default": adder,
                        "typ": "str",
                    },
                ),
            ),
            gold=Expr(
                Call(
                    args=[set_value("--byo")],
                    func=Attribute(
                        Name("argument_parser", Load()),
                        "add_argument",
                        Load(),
                    ),
                    keywords=[
                        keyword(
                            arg="type",
                            value=Name("pickle.loads", Load()),
                            identifier=None,
                        ),
                        keyword(
                            arg="default",
                            value=set_value(pickled_adder),
                            identifier=None,
                        ),
                    ],
                    expr=None,
                    expr_func=None,
                )
            ),
            test_case_instance=self,
        )
Exemplo n.º 4
0
 def test_param2argparse_param_default_list(self) -> None:
     """
     Tests that param2argparse_param works to change the type based on the default
       whence said default is a list
     """
     run_ast_test(
         gen_ast=param2argparse_param(
             ("byo", {"default": [], "typ": "str"}),
         ),
         gold=argparse_add_argument_expr,
         test_case_instance=self,
     )
Exemplo n.º 5
0
    def test_param2argparse_param_default_torch(self) -> None:
        """
        Tests that param2argparse_param works to change the type based on the default
          whence said default is a proxy for an internal PyTorch type
        """

        class FakeTorch(object):
            """Not a real torch"""

            def __str__(self):
                """But a real str

                :returns: An actual str
                :rtype: ```Literal['<required parameter>']```
                """
                return "<required parameter>"

        # type("FakeTorch", tuple(), {"__str__": lambda _: "<required parameter>"})

        run_ast_test(
            gen_ast=param2argparse_param(
                (
                    "byo",
                    {
                        "default": FakeTorch(),
                    },
                ),
            ),
            gold=Expr(
                Call(
                    args=[set_value("--byo")],
                    func=Attribute(
                        Name("argument_parser", Load()),
                        "add_argument",
                        Load(),
                    ),
                    keywords=[
                        keyword(
                            arg="type",
                            value=Name(FakeTorch.__name__, Load()),
                            identifier=None,
                        ),
                        keyword(arg="required", value=set_value(True), identifier=None),
                    ],
                    expr=None,
                    expr_func=None,
                )
            ),
            test_case_instance=self,
        )
Exemplo n.º 6
0
    def test_param2argparse_param_default_notimplemented(self) -> None:
        """
        Tests that param2argparse_param works to change the type based on the default
          whence said default is a proxy for an unexpected type
        """

        with self.assertRaises(NotImplementedError) as cm:
            param2argparse_param(
                (
                    "byo",
                    {
                        "default": memoryview(b""),
                    },
                ),
            )
        self.assertEqual(
            *map(
                lambda s: s[: s.rfind("<") + 10],
                (
                    "Parsing type <class 'memoryview'>, which contains <memory at 0x10bb8c400>",
                    str(cm.exception),
                ),
            )
        )
Exemplo n.º 7
0
    def test_param2argparse_param_default_class_str(self) -> None:
        """
        Tests that param2argparse_param works to change the type based on the default
          whence said default is a proxy for an unexpected type
        """

        self.assertEqual(
            get_value(
                param2argparse_param(
                    (
                        "byo",
                        {"default": 5.5, "typ": "<class 'float'>"},
                    ),
                )
                .value.keywords[0]
                .value
            ),
            "float",
        )
Exemplo n.º 8
0
 def test_param2argparse_param_default_ast_binop(self) -> None:
     """
     Tests that param2argparse_param works to change the type based on the default
       whence said default is a non specially handled ast.AST
     """
     run_ast_test(
         gen_ast=param2argparse_param(
             (
                 "byo",
                 {
                     "default": BinOp(
                         set_value(5),
                         Mult(),
                         set_value(5),
                     ),
                     "typ": "str",
                 },
             ),
         ),
         gold=Expr(
             Call(
                 args=[set_value("--byo")],
                 func=Attribute(
                     Name("argument_parser", Load()),
                     "add_argument",
                     Load(),
                 ),
                 keywords=[
                     keyword(arg="required", value=set_value(True), identifier=None),
                     keyword(
                         arg="default",
                         value=set_value("```(5 * 5)```"),
                         identifier=None,
                     ),
                 ],
                 expr=None,
                 expr_func=None,
             )
         ),
         test_case_instance=self,
     )
Exemplo n.º 9
0
 def test_param2argparse_param_none_default(self) -> None:
     """
     Tests that param2argparse_param works to reparse the default
     """
     run_ast_test(
         gen_ast=param2argparse_param(("yup", {"default": NoneStr})),
         gold=Expr(
             Call(
                 args=[set_value("--yup")],
                 func=Attribute(
                     Name("argument_parser", Load()),
                     "add_argument",
                     Load(),
                 ),
                 keywords=[],
                 expr=None,
                 expr_func=None,
             )
         ),
         test_case_instance=self,
     )
Exemplo n.º 10
0
 def test_param2argparse_param_default_ast_tuple(self) -> None:
     """
     Tests that param2argparse_param works to change the type based on the default
       whence said default is an ast.Tuple
     """
     run_ast_test(
         gen_ast=param2argparse_param(
             (
                 "byo",
                 {
                     "default": Tuple(
                         elts=[],
                         ctx=Load(),
                         expr=None,
                     ),
                     "typ": "str",
                 },
             ),
         ),
         gold=Expr(
             Call(
                 args=[set_value("--byo")],
                 func=Attribute(
                     Name("argument_parser", Load()),
                     "add_argument",
                     Load(),
                 ),
                 keywords=[
                     keyword(
                         arg="type", value=Name("loads", Load()), identifier=None
                     ),
                     keyword(arg="required", value=set_value(True), identifier=None),
                     keyword(arg="default", value=set_value("()"), identifier=None),
                 ],
                 expr=None,
                 expr_func=None,
             )
         ),
         test_case_instance=self,
     )