Esempio n. 1
0
    def test_complex_typehints(self):
        func_node = FunctionNode("test", None,
                                 TestClass.with_python2_union_typehint, "test")
        assert func_node.return_type == "List[Union[str, int]]"

        func_node = FunctionNode("test", None,
                                 TestClass.with_python3_union_typehint, "test")
        assert func_node.return_type == "List[Union[str, int]]"
Esempio n. 2
0
    def test_typehint_and_docstring_return_types(self):
        func_node = FunctionNode("test", None,
                                 TestClass.with_python2_list_typehint, "test")
        assert func_node.return_type == "List[TestClass]"

        func_node = FunctionNode("test", None,
                                 TestClass.with_python3_list_typehint, "test")
        assert func_node.return_type == "List[TestClass]"

        func_node = FunctionNode("test", None,
                                 TestClass.with_python3_str_typehint, "test")
        assert func_node.return_type == "List[str]"
 def test_alphabetical_kwargs(self):
     node = FunctionNode("test",
                         None,
                         obj=SpecialArgsClient.with_sorted_kwargs)
     actual = _render_string(_tokenize(node))
     expected = 'def with_sorted_kwargs(self, *, a, b, c, d, **kwargs) -> None'
     _check(actual, expected, SpecialArgsClient)
 def test_class_default(self):
     node = FunctionNode("test",
                         None,
                         obj=DefaultValuesClient.with_class_default)
     actual = _render_string(_tokenize(node))
     expected = 'def with_class_default(my_class: Any = FakeObject) -> None'
     _check(actual, expected, DefaultValuesClient)
 def test_simple_default(self):
     node = FunctionNode("test",
                         None,
                         obj=DefaultValuesClient.with_simple_default)
     actual = _render_string(_tokenize(node))
     expected = 'def with_simple_default(name: str = "Bill", *, age: int = 21) -> None'
     _check(actual, expected, DefaultValuesClient)
 def test_keyword_only_args(self):
     node = FunctionNode("test",
                         None,
                         obj=SpecialArgsClient.with_keyword_only_args)
     actual = _render_string(_tokenize(node))
     expected = 'def with_keyword_only_args(self, *, value, **kwargs) -> None'
     _check(actual, expected, SpecialArgsClient)
 def test_positional_only_args(self):
     node = FunctionNode("test",
                         None,
                         obj=SpecialArgsClient.with_positional_only_args)
     actual = _render_string(_tokenize(node))
     expected = 'def with_positional_only_args(self, a, b, /, c) -> None'
     _check(actual, expected, SpecialArgsClient)
 def test_nonstandard_names(self):
     node = FunctionNode("test",
                         None,
                         obj=SpecialArgsClient.with_nonstandard_names)
     actual = _render_string(_tokenize(node))
     expected = 'def with_nonstandard_names(self, *vars, **kwds) -> None'
     _check(actual, expected, SpecialArgsClient)
 def test_enum_defaults(self):
     node = FunctionNode("test",
                         None,
                         obj=DefaultValuesClient.with_enum_defaults)
     actual = _render_string(_tokenize(node))
     expected = 'def with_enum_defaults(enum1: Union[PetEnumPy3Metaclass, str] = "DOG", enum2: Union[PetEnumPy3Metaclass, str] = PetEnumPy3Metaclass.DOG) -> None'
     _check(actual, expected, DefaultValuesClient)
Esempio n. 10
0
    def test_variadic_typehints(self):
        func_node = FunctionNode("test", None,
                                 TestClass.with_variadic_python3_typehint,
                                 "test")
        arg = func_node.args["vars"]
        assert arg.argname == "*vars"
        assert arg.argtype == "str"
        assert arg.default == ""

        func_node = FunctionNode("test", None,
                                 TestClass.with_variadic_python2_typehint,
                                 "test")
        arg = func_node.args["vars"]
        assert arg.argname == "*vars"
        # the type annotation comes ONLY from the docstring. The Python2 type hint is not used!
        assert arg.argtype == "str"
        assert arg.default == ""
 def test_parsed_docstring_defaults(self):
     node = FunctionNode(
         "test",
         None,
         obj=DefaultValuesClient.with_parsed_docstring_defaults)
     actual = _render_string(_tokenize(node))
     expected = 'def with_parsed_docstring_defaults(name: str = "Bill", age: int = 21, some_class: class = ":py:class:`apistubgen.test.models.FakeObject`") -> None'
     _check(actual, expected, DefaultValuesClient)
 def test_falsy_optional_defaults_and_docstring(self):
     node = FunctionNode(
         "test",
         None,
         obj=DefaultValuesClient.with_falsy_optional_defaults_and_docstring)
     actual = _render_string(_tokenize(node))
     expected = 'def with_falsy_optional_defaults_and_docstring(*, bool: Optional[bool] = False, int: Optional[int] = 0, string: Optional[str] = "") -> None'
     _check(actual, expected, DefaultValuesClient)
 def test_list_return_type(self):
     clients = [
         Python2TypeHintClient, Python3TypeHintClient,
         DocstringTypeHintClient
     ]
     for client in clients:
         node = FunctionNode("test", None, obj=client.with_list_return_type)
         actual = _render_string(_tokenize(node))
         expected = "def with_list_return_type(self) -> List[TestClass]"
         _check(actual, expected, client)
 def test_simple_typehints(self):
     clients = [
         Python2TypeHintClient, Python3TypeHintClient,
         DocstringTypeHintClient
     ]
     for client in clients:
         node = FunctionNode("test", None, obj=client.with_simple_typehints)
         actual = _render_string(_tokenize(node))
         expected = "def with_simple_typehints(self, name: str, age: int) -> str"
         _check(actual, expected, client)
 def test_complex_typehints(self):
     clients = [
         Python2TypeHintClient, Python3TypeHintClient,
         DocstringTypeHintClient
     ]
     for client in clients:
         node = FunctionNode("test",
                             None,
                             obj=client.with_complex_typehints)
         actual = _render_string(_tokenize(node))
         expected = "def with_complex_typehints(self, value: List[ItemPaged[Union[FakeObject, FakeError]]]) -> None"
         _check(actual, expected, client)
 def test_datetime_typehint(self):
     clients = [
         Python2TypeHintClient, Python3TypeHintClient,
         DocstringTypeHintClient
     ]
     for client in clients:
         node = FunctionNode("test",
                             None,
                             obj=client.with_datetime_typehint)
         actual = _render_string(_tokenize(node))
         expected = "def with_datetime_typehint(self, date: datetime) -> datetime"
         _check(actual, expected, client)
 def test_variadic_typehints(self):
     clients = [
         # TODO: Known limitation of astroid (See: https://github.com/Azure/azure-sdk-tools/issues/3131)
         # Python2TypeHintClient,
         Python3TypeHintClient,
         DocstringTypeHintClient
     ]
     for client in clients:
         node = FunctionNode("test",
                             None,
                             obj=client.with_variadic_typehint)
         actual = _render_string(_tokenize(node))
         expected = "def with_variadic_typehint(self, *vars: str, **kwargs: Any) -> None"
         _check(actual, expected, client)
 def test_optional_none_default(self):
     node = FunctionNode(
         "test", None, obj=DefaultValuesClient.with_optional_none_defaults)
     actual = _render_string(_tokenize(node))
     expected = 'def with_optional_none_defaults(name: Optional[str] = None, *, age: Optional[int] = ...) -> None'
     _check(actual, expected, DefaultValuesClient)
Esempio n. 19
0
 def test_default_values(self):
     func_node = FunctionNode("test", None, TestClass.with_default_values,
                              "test")
     assert func_node.args["foo"].default == "1"
     assert func_node.kw_args["bar"].default == "2"
     assert func_node.kw_args["baz"].default == "..."
Esempio n. 20
0
 def test_optional_docstring(self):
     func_node = FunctionNode("test", None,
                              TestClass.with_optional_docstring, "test")
     arg = func_node.args["description"]
     assert arg.argtype == "str"
     assert arg.default == "..."
Esempio n. 21
0
 def test_optional_typehint(self):
     func_node = FunctionNode("test", None,
                              TestClass.with_optional_typehint, "test")
     arg = func_node.args["description"]
     assert arg.argtype == "Optional[str]"
     assert arg.default == "..."