示例#1
0
def test_name_properties_on_method():
    assert not Method(name="c", path="a.b.c", file_path="a.py").name_properties
    assert "private" in Method(name="_c", path="a.b._c",
                               file_path="a.py").name_properties
    assert not Method(name="__c", path="a.b.__c",
                      file_path="a.py").name_properties
    assert "special" in Method(name="__c__",
                               path="a.b.__c__",
                               file_path="a.py").name_properties
示例#2
0
    def get_method_documentation(
            self,
            node: ObjectNode,
            properties: Optional[List[str]] = None) -> Method:
        """
        Get the documentation for a method.

        Arguments:
            node: The node representing the method and its parents.
            properties: A list of properties to apply to the method.

        Return:
            The documented method object.
        """
        method = node.obj
        path = node.dotted_path
        source: Optional[Source]

        try:
            source = Source(*inspect.getsourcelines(method))
        except OSError as error:
            self.errors.append(f"Couldn't read source for '{path}': {error}")
            source = None
        except TypeError:
            source = None

        return Method(
            name=node.name,
            path=path,
            file_path=node.file_path,
            docstring=inspect.getdoc(method) or "",
            signature=inspect.signature(method),
            properties=properties or [],
            source=source,
        )
示例#3
0
def test_add_children():
    root = Object(name="o", path="o", file_path="o.py")

    class_ = Class(name="c", path="o.c", file_path="o.py")
    attribute = Attribute(name="a", path="o.c.a", file_path="o.py")
    class_.add_child(attribute)

    root.add_children(
        [
            # class has wrong path
            Class(name="w", path="wrong.path.w", file_path="/wrong/path/w.py"),
            # class OK
            class_,
            # not a direct child,
            attribute,
            # function OK
            Function(name="f", path="o.f", file_path="o.py"),
            # not a direct child, not even a child of known child
            Method(name="missing_node", path="o.mn.missing_node", file_path="o.py"),
        ]
    )

    assert len(root.children) == 2
    assert root.classes and root.classes[0] is class_
    assert root.functions and root.functions[0].name == "f"
示例#4
0
    def get_method_documentation(
            self,
            node: ObjectNode,
            properties: Optional[List[str]] = None) -> Method:
        """
        Get the documentation for a method or method descriptor.

        Arguments:
            node: The node representing the method and its parents.
            properties: A list of properties to apply to the method.

        Returns:
            The documented method object.
        """
        method = node.obj
        path = node.dotted_path
        signature: Optional[inspect.Signature]
        source: Optional[Source]

        try:
            source = Source(*inspect.getsourcelines(method))
        except OSError as error:
            self.errors.append(f"Couldn't read source for '{path}': {error}")
            source = None
        except TypeError:
            source = None

        if node.is_coroutine_function():
            if properties is None:
                properties = ["async"]
            else:
                properties.append("async")

        try:
            # for "built-in" functions, e.g. those implemented in C,
            # inspect.signature() uses the __text_signature__ attribute, which
            # provides a limited but still useful amount of signature information.
            # "built-in" functions with no __text_signature__ will
            # raise a ValueError().
            signature = inspect.signature(method)
        except ValueError as error:
            self.errors.append(
                f"Couldn't read signature for '{path}': {error}")
            signature = None

        return Method(
            name=node.name,
            path=path,
            file_path=node.file_path,
            docstring=inspect.getdoc(method),
            signature=signature,
            properties=properties or [],
            source=source,
        )
示例#5
0
def test_get_root():
    root = Module(name="my_module", path="my.dotted.path", file_path="")
    node1 = Class(name="my_class1", path="my.dotted.path.my_class1", file_path="")
    node2 = Class(name="my_class2", path="my.dotted.path.my_class2", file_path="")
    leaf = Method(name="my_method", path="my.dotted.path.my_class1.my_method", file_path="")

    root.add_children([node1, node2])
    node1.add_child(leaf)

    assert root.root is root
    assert node1.root is root
    assert node2.root is root
    assert leaf.root is root
示例#6
0
def test_creating_method():
    assert Method(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")