Exemplo n.º 1
0
def _find_type_from_comment_hint(context, node, varlist, name):
    index = None
    if varlist.type in ("testlist_star_expr", "exprlist"):
        # something like "a, b = 1, 2"
        index = 0
        for child in varlist.children:
            if child == name:
                break
            if child.type == "operator":
                continue
            index += 1
        else:
            return []

    comment = node.get_following_comment_same_line()
    if comment is None:
        return []
    match = re.match(r"^#\s*type:\s*([^#]*)", comment)
    if not match:
        return []
    annotation = tree.String(
        repr(str(match.group(1).strip())),
        node.start_pos)
    annotation.parent = node.parent
    return _evaluate_for_annotation(context, annotation, index)
Exemplo n.º 2
0
 def convert_leaf(self, grammar, type, value, prefix, start_pos):
     # print('leaf', repr(value), token.tok_name[type])
     if type == tokenize.NAME:
         if value in grammar.keywords:
             return tree.Keyword(value, start_pos, prefix)
         else:
             return tree.Name(value, start_pos, prefix)
     elif type == STRING:
         return tree.String(value, start_pos, prefix)
     elif type == NUMBER:
         return tree.Number(value, start_pos, prefix)
     elif type == NEWLINE:
         return tree.Newline(value, start_pos, prefix)
     elif type == ENDMARKER:
         return tree.EndMarker(value, start_pos, prefix)
     else:
         return tree.Operator(value, start_pos, prefix)
Exemplo n.º 3
0
def _get_faked(module, obj, name=None):
    result, fake_module = _faked(module, obj, name)
    if result is None:
        # We're not interested in classes. What we want is functions.
        raise FakeDoesNotExist
    elif result.type == 'classdef':
        return result, fake_module
    else:
        # Set the docstr which was previously not set (faked modules don't
        # contain it).
        assert result.type == 'funcdef'
        doc = '"""%s"""' % obj.__doc__  # TODO need escapes.
        suite = result.children[-1]
        string = tree.String(doc, (0, 0), '')
        new_line = tree.Newline('\n', (0, 0))
        docstr_node = tree.PythonNode('simple_stmt', [string, new_line])
        suite.children.insert(1, docstr_node)
        return result, fake_module
Exemplo n.º 4
0
def test_unicode_string():
    s = tree.String(None, u('bö'), (0, 0))
    assert repr(s)  # Should not raise an Error!