Beispiel #1
0
def test_annotation_formatter(annotation: str) -> None:
    """Perform two checks on the annotation formatter:
    - all type names in the annotation are passed to the linker
    - the plain text version of the output matches the input
    """

    expected_lookups = [
        found[1:-1] for found in re.findall('<[^>]*>', annotation)
    ]
    expected_text = annotation.replace('<', '').replace('>', '')

    mod = fromText(f'''
    value: {expected_text}
    ''')
    obj = mod.contents['value']
    parsed = epydoc2stan.get_parsed_type(obj)
    assert parsed is not None
    linker = RecordingAnnotationLinker()
    stan = parsed.to_stan(linker)
    assert linker.requests == expected_lookups
    html = flatten(stan)
    assert html.startswith('<code>')
    assert html.endswith('</code>')
    text = html[6:-7]
    assert text == expected_text
Beispiel #2
0
def test_annotated_variables(systemcls):
    mod = fromText('''
    class C:
        """class docstring

        @cvar a: first
        @type a: string

        @type b: string
        @cvar b: second
        """

        a: str = "A"

        b: str

        c: str = "C"
        """third"""

        d: str
        """fourth"""

        e: List['C']
        """fifth"""

        f: 'List[C]'
        """sixth"""

        g: 'List["C"]'
        """seventh"""

        def __init__(self):
            self.s: List[str] = []
            """instance"""

    m: bytes = b"M"
    """module-level"""
    ''',
                   modname='test',
                   systemcls=systemcls)
    C = mod.contents['C']
    a = C.contents['a']
    assert unwrap(a.parsed_docstring) == """first"""
    assert to_html(get_parsed_type(a)) == 'string'
    b = C.contents['b']
    assert unwrap(b.parsed_docstring) == """second"""
    assert to_html(get_parsed_type(b)) == 'string'
    c = C.contents['c']
    assert c.docstring == """third"""
    assert to_html(get_parsed_type(c)) == '<code>str</code>'
    d = C.contents['d']
    assert d.docstring == """fourth"""
    assert to_html(get_parsed_type(d)) == '<code>str</code>'
    e = C.contents['e']
    assert e.docstring == """fifth"""
    assert to_html(get_parsed_type(e)) == '<code>List[C]</code>'
    f = C.contents['f']
    assert f.docstring == """sixth"""
    assert to_html(get_parsed_type(f)) == '<code>List[C]</code>'
    g = C.contents['g']
    assert g.docstring == """seventh"""
    assert to_html(get_parsed_type(g)) == '<code>List[C]</code>'
    s = C.contents['s']
    assert s.docstring == """instance"""
    assert to_html(get_parsed_type(s)) == '<code>List[str]</code>'
    m = mod.contents['m']
    assert m.docstring == """module-level"""
    assert to_html(get_parsed_type(m)) == '<code>bytes</code>'
Beispiel #3
0
 def type2html(obj: model.Documentable) -> str:
     parsed_type = get_parsed_type(obj)
     assert parsed_type is not None
     return to_html(parsed_type)