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)
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
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
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.")
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"
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))"
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.")
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 == ""
def test_multiple_assignments(): attrs = get_attributes(E) assert attrs["a"] == (int, "a") assert attrs["b"] == (str, "b")
def test_module_attribute_tye(): from mkapi.core import renderer assert get_attributes(renderer)["renderer"][0] is renderer.Renderer
def test_multiple_assignments(): attrs = get_attributes(E) assert attrs['a'] == (int, 'a') assert attrs['b'] == (str, 'b')