Beispiel #1
0
    def test_to_function_emit_as_kwonlyargs(self) -> None:
        """
        Tests whether `function` produces method with keyword only arguments
        """
        function_def = reindent_docstring(
            deepcopy(
                next(
                    filter(
                        rpartial(isinstance, FunctionDef),
                        ast.parse(
                            class_with_method_types_str.replace(
                                "self", "self, *")).body[0].body,
                    ))))
        function_name = function_def.name
        function_type = get_function_type(function_def)

        gen_ast = emit.function(
            parse.docstring(docstring_str),
            function_name=function_name,
            function_type=function_type,
            emit_default_doc=False,
            type_annotations=True,
            emit_separating_tab=True,
            indent_level=1,
            emit_as_kwonlyargs=True,
        )

        run_ast_test(
            self,
            gen_ast=gen_ast,
            gold=function_def,
        )
Beispiel #2
0
    def test_to_function(self) -> None:
        """
        Tests whether `function` produces method from `class_with_method_types_ast` given `docstring_str`
        """

        function_def = reindent_docstring(
            deepcopy(
                next(
                    filter(
                        rpartial(isinstance, FunctionDef),
                        class_with_method_types_ast.body,
                    ))))

        function_name = function_def.name
        function_type = get_function_type(function_def)

        gen_ast = emit.function(
            parse.docstring(docstring_str),
            function_name=function_name,
            function_type=function_type,
            emit_default_doc=False,
            type_annotations=True,
            emit_separating_tab=True,
            indent_level=1,
            emit_as_kwonlyargs=False,
        )

        run_ast_test(
            self,
            gen_ast=gen_ast,
            gold=function_def,
        )
Beispiel #3
0
 def test_from_docstring_numpydoc(self) -> None:
     """
     Tests whether `docstring` produces `intermediate_repr_no_default_doc`
           from `docstring_numpydoc_str`"""
     ir, returns = parse.docstring(docstring_numpydoc_str,
                                   return_tuple=True,
                                   emit_default_doc=False)
     self.assertTrue(returns)
     self.assertDictEqual(ir, intermediate_repr_no_default_doc)
Beispiel #4
0
 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,
     )
Beispiel #5
0
 def test_from_docstring_numpydoc_only_params(self) -> None:
     """
     Tests whether `docstring` produces `intermediate_repr_no_default_doc`
           from `docstring_numpydoc_only_params_str`"""
     ir, returns = parse.docstring(
         docstring_numpydoc_only_params_str,
         return_tuple=True,
         emit_default_doc=False,
     )
     self.assertFalse(returns)
     gold = deepcopy(intermediate_repr_no_default_doc)
     del gold["returns"]
     gold.update({"doc": "", "returns": None})
     self.assertDictEqual(ir, gold)
Beispiel #6
0
 def test_from_docstring_numpydoc_only_doc_str(self) -> None:
     """
     Tests whether `docstring` produces `intermediate_repr_no_default_doc`
           from `docstring_numpydoc_only_doc_str`"""
     ir, returns = parse.docstring(docstring_numpydoc_only_doc_str.strip(),
                                   return_tuple=True)
     self.assertFalse(returns)
     self.assertDictEqual(
         ir,
         {
             "doc": intermediate_repr_no_default_doc["doc"],
             "name": None,
             "params": OrderedDict(),
             "returns": None,
             "type": "static",
         },
     )
Beispiel #7
0
    def _get_ass_typ(self, node):
        """
        Get the type of the assignment

        :param node: Assignment
        :type node: ```Union[Assign, AnnAssign]```

        :returns: The type of the assignment, e.g., `int`
        :rtype: ```Optional[str]```
        """
        name, typ_dict = ((node.targets[0], {
            "typ": node.type_comment
        }) if isinstance(node, Assign) else
                          (node.target, {
                              "typ": node.annotation or node.type_comment
                          }))
        if not hasattr(node, "_location"):
            return typ_dict["typ"]
        search = node._location[:-1]
        search_str = ".".join(search)

        self.memoized[search_str] = ir = (self.memoized.get(
            search_str,
            (lambda parent:
             ((lambda doc_str: None
               if doc_str is None else parse.docstring(doc_str))
              (get_docstring(parent))
              if isinstance(parent,
                            (ClassDef, AsyncFunctionDef, FunctionDef)) else {
                                "params": OrderedDict()
                            }))(find_in_ast(search, self.whole_ast)),
        ) or {
            "params": OrderedDict()
        })

        return ir["params"].get(name, typ_dict)[
            "typ"]  # if ir is not None and ir.get("params") else None