示例#1
0
文件: signature.py 项目: tony/mkapi
    def set_attributes(self):
        """
        Examples:
            >>> from mkapi.core.base import Base
            >>> s = Signature(Base)
            >>> s.parameters['name'].to_tuple()
            ('name', 'str, optional', 'Name of self.')
            >>> s.attributes['html'].to_tuple()
            ('html', 'str', 'HTML output after conversion.')
        """
        items = []
        for name, (type, description) in get_attributes(self.obj).items():
            if isinstance(type, str) and type:
                type = resolve_forward_ref(self.obj, type)
            else:
                type = to_string(type, obj=self.obj) if type else ""
            if not type:
                type, description = preprocess.split_type(description)

            item = Item(name, Type(type), Inline(description))
            if is_dataclass(self.obj):
                if name in self.parameters:
                    self.parameters[name].set_description(item.description)
                if self.obj.__dataclass_fields__[name].type != InitVar:
                    items.append(item)
            else:
                items.append(item)
        self.attributes = Section("Attributes", items=items)
示例#2
0
def test_dataclass_attribute():
    attrs = get_attributes(C)
    for k, (name, (type, markdown)) in enumerate(attrs.items()):
        assert name == ["x", "y", "z"][k]
        assert markdown == ["int", "A", "B\n\nend."][k]
        if k == 0:
            assert type is int
示例#3
0
 def get_attributes(self):
     for name, (type, desc) in get_attributes(self.obj).items():
         type = to_string(type) if type else ""
         if not type:
             type, desc = preprocess.split_type(desc)
         self.attributes[name] = type
         self.attributes_desc[name] = desc
示例#4
0
def test_name_error():
    abc = "abc"

    class Name:
        def __init__(self):
            self.x: abc = 1  #: abc.

    attrs = get_attributes(Name)
    assert attrs["x"] == ("abc", "abc.")
示例#5
0
def test_dataclass_attribute_without_desc():
    attrs = get_attributes(C)
    for k, (name, (type, markdown)) in enumerate(attrs.items()):
        assert name == ["x", "y"][k]
        assert markdown == ""
        if k == 0:
            assert type is int
        elif k == 1:
            x = signature.to_string(type)
            assert x == "list of str"
示例#6
0
def test_class_attribute():
    attrs = get_attributes(A)
    for k, (name, (type, markdown)) in enumerate(attrs.items()):
        assert name == ["x", "y", "z"][k]
        assert markdown.startswith(["Doc ", "list of", "Docstring *after*"][k])
        assert markdown.endswith(["attribute.", "specified.", "supported."][k])
        if k == 0:
            assert type is int
        elif k == 1:
            assert type is None
        elif k == 2:
            x = signature.to_string(type)
            assert x == "(list of int, dict(str: list of float))"
示例#7
0
def test_module_attribute_without_desc():
    attrs = get_attributes(google_style)
    for k, (name, (type, markdown)) in enumerate(attrs.items()):
        if k == 0:
            assert name == "first_attribute"
            assert type is int
            assert markdown.startswith("The first module level attribute.")
        if k == 1:
            assert name == "second_attribute"
            assert type is None
            assert markdown.startswith(
                "str: The second module level attribute.")
        if k == 2:
            assert name == "third_attribute"
            assert signature.to_string(type) == "list of int"
            assert markdown.startswith("The third module level attribute.")
            assert markdown.endswith("supported.")
示例#8
0
def test_class_attribute_without_desc():
    attrs = get_attributes(B)
    for k, (name, (type, markdown)) in enumerate(attrs.items()):
        assert name == ["x", "y"][k]
        assert markdown == ""
示例#9
0
def test_multiple_assignments():
    attrs = get_attributes(E)
    assert attrs["a"] == (int, "a")
    assert attrs["b"] == (str, "b")
示例#10
0
def test_module_attribute_tye():
    from mkapi.core import renderer

    assert get_attributes(renderer)["renderer"][0] is renderer.Renderer
示例#11
0
def test_multiple_assignments():
    attrs = get_attributes(E)
    assert attrs['a'] == (int, 'a')
    assert attrs['b'] == (str, 'b')