Example #1
0
def get_line_no(text, pos):  # type: (Text, int) -> SourceLocation
    """
    get line number (starting at 0) and column offset of ``pos`` in ``text``.

    :return: tuple of ``(lineno, col_offset)``
    """
    last_nl = text.rfind('\n', 0, pos)
    if last_nl < 0:
        return SourceLocation(0, pos)

    return SourceLocation(text[:pos].count('\n'), pos - (last_nl + 1))
Example #2
0
    def test_VarType(self):
        sut = DocUtilsStyle()

        input = DocString(content="""
            @vartype name1: long
            :vartype name2 \t: int
        """)

        sut(input)

        self.assertEqual([
            TypeInformation(VarType.VAR, "name1", "long", SourceLocation(1,
                                                                         0)),
            TypeInformation(VarType.VAR, "name2", "int", SourceLocation(2, 0)),
        ], input.type_info)
Example #3
0
    def test_RType(self):
        sut = DocUtilsStyle()

        input = DocString(content="""
            :rtype: int
            :rtype : list[int]
        """)

        sut(input)

        self.assertEqual([
            TypeInformation(VarType.RETURN, None, "int", SourceLocation(1, 0)),
            TypeInformation(VarType.RETURN, None, "list[int]",
                            SourceLocation(2, 0)),
        ], input.type_info)
Example #4
0
    def test_SameLine(self):
        sut = DocUtilsStyle()

        input = DocString(content="""
            :type name1: int
            @type name2: long
            :type\t\tname3\t\t:\t\tstr\t\t
        """)

        sut(input)

        self.assertEqual([
            TypeInformation(VarType.VAR, "name1", "int", SourceLocation(1, 0)),
            TypeInformation(VarType.VAR, "name2", "long", SourceLocation(2,
                                                                         0)),
            TypeInformation(VarType.VAR, "name3", "str", SourceLocation(3, 0))
        ], input.type_info)
Example #5
0
    def test_ParamType(self):
        sut = DocUtilsStyle()

        input = DocString(content="""
            @param long name1: doc1
            @param name2: doc2
            @key long name3: doc1
            @argument long name4: doc1
        """)

        sut(input)

        self.assertEqual([
            TypeInformation(VarType.PARAM, "name1", "long", SourceLocation(
                1, 0)),
            TypeInformation(VarType.PARAM, "name3", "long", SourceLocation(
                3, 0)),
            TypeInformation(VarType.PARAM, "name4", "long", SourceLocation(
                4, 0)),
        ], input.type_info)
Example #6
0
    def __call__(self, doc_string):  # type: (DocString) -> None
        type_info = doc_string.type_info or []  # type: List[TypeInformation]
        content = doc_string.content

        is_func = isinstance(doc_string, FunctionDocString)

        for match in self._re_type.finditer(content):
            field = match.group(1)
            if field in ("type", "vartype"):
                if match.group(3):
                    continue  # broken
                vartype = VarType.PARAM if is_func else VarType.VAR
                name = match.group(2).strip()
                expr = match.group(4).strip()

            elif field == "rtype":
                if match.group(2):
                    continue  # broken
                vartype = VarType.RETURN
                name = None
                expr = match.group(4).strip()

            elif field in ("param", "parameter", "arg", "argument", "key",
                           "keyword"):
                if not match.group(3):
                    continue  # no type info
                vartype = VarType.PARAM
                name = match.group(3).strip()
                expr = match.group(2).strip()
            else:
                continue

            type_info.append(
                TypeInformation(
                    vartype, name, expr,
                    SourceLocation.from_text_pos(content, match.start(0))))

        doc_string.type_info = type_info
Example #7
0
 def __init__(self, srcfile, destfile, source_replacements):
     # type: (TextIO, TextIO, Iterable[SourceReplacement]) -> None
     self.srcfile = srcfile
     self.destfile = destfile
     self.source_replacements = source_replacements
     self.srcpos = SourceLocation(0, 0)
Example #8
0
 def from_node(cls, obj_node, doc_node):
     # type: (ast.stmt, ast.Str) -> DocString
     return cls(doc_node.s, SourceLocation.from_node(obj_node),
                SourceLocation.from_node(doc_node))