Exemple #1
0
def test_parse_annotation(app):
    doctree = _parse_annotation("int", app.env)
    assert_node(doctree, ([pending_xref, "int"],))
    assert_node(doctree[0], pending_xref, refdomain="py", reftype="class", reftarget="int")

    doctree = _parse_annotation("List[int]", app.env)
    assert_node(doctree, ([pending_xref, "List"],
                          [desc_sig_punctuation, "["],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Tuple[int, int]", app.env)
    assert_node(doctree, ([pending_xref, "Tuple"],
                          [desc_sig_punctuation, "["],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, ", "],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Tuple[()]", app.env)
    assert_node(doctree, ([pending_xref, "Tuple"],
                          [desc_sig_punctuation, "["],
                          [desc_sig_punctuation, "("],
                          [desc_sig_punctuation, ")"],
                          [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Tuple[int, ...]", app.env)
    assert_node(doctree, ([pending_xref, "Tuple"],
                          [desc_sig_punctuation, "["],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, ", "],
                          [desc_sig_punctuation, "..."],
                          [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Callable[[int, int], int]", app.env)
    assert_node(doctree, ([pending_xref, "Callable"],
                          [desc_sig_punctuation, "["],
                          [desc_sig_punctuation, "["],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, ", "],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, "]"],
                          [desc_sig_punctuation, ", "],
                          [pending_xref, "int"],
                          [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("List[None]", app.env)
    assert_node(doctree, ([pending_xref, "List"],
                          [desc_sig_punctuation, "["],
                          [pending_xref, "None"],
                          [desc_sig_punctuation, "]"]))

    # None type makes an object-reference (not a class reference)
    doctree = _parse_annotation("None", app.env)
    assert_node(doctree, ([pending_xref, "None"],))
    assert_node(doctree[0], pending_xref, refdomain="py", reftype="obj", reftarget="None")
Exemple #2
0
def test_parse_annotation():
    doctree = _parse_annotation("int")
    assert_node(doctree, ([pending_xref, "int"], ))

    doctree = _parse_annotation("List[int]")
    assert_node(doctree, ([pending_xref, "List"], [desc_sig_punctuation, "["],
                          [pending_xref, "int"], [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Tuple[int, int]")
    assert_node(doctree, ([pending_xref, "Tuple"], [desc_sig_punctuation, "["],
                          [pending_xref, "int"], [desc_sig_punctuation, ", "],
                          [pending_xref, "int"], [desc_sig_punctuation, "]"]))

    doctree = _parse_annotation("Callable[[int, int], int]")
    assert_node(doctree,
                ([pending_xref, "Callable"], [desc_sig_punctuation, "["], [
                    desc_sig_punctuation, "["
                ], [pending_xref, "int"], [desc_sig_punctuation, ", "], [
                    pending_xref, "int"
                ], [desc_sig_punctuation, "]"], [desc_sig_punctuation, ", "],
                 [pending_xref, "int"], [desc_sig_punctuation, "]"]))