Example #1
0
def generate_annotations_json_string(source_path):
    # type: (str) -> List[FunctionData]
    """Produce annotation data JSON file from a JSON file with runtime-collected types.

    Data formats:

    * The source JSON is a list of pyannotate_tools.annotations.parse.RawEntry items.
    * The output JSON is a list of FunctionData items.
    """
    items = parse_json(source_path)
    results = []
    for item in items:
        arg_types, return_type = infer_annotation(item.type_comments)
        arg_strs = []
        for arg, kind in arg_types:
            arg_str = str(arg)
            if kind == ARG_STAR:
                arg_str = '*%s' % arg_str
            elif kind == ARG_STARSTAR:
                arg_str = '**%s' % arg_str
            arg_strs.append(arg_str)
        signature = {
            'arg_types': arg_strs,
            'return_type': str(return_type),
        }  # type: Signature
        data = {
            'path': item.path,
            'line': item.line,
            'func_name': item.func_name,
            'signature': signature,
            'samples': item.samples
        }  # type: FunctionData
        results.append(data)
    return results
Example #2
0
def unify_type_comments(type_comments):
    # type: (List[str]) -> Signature
    arg_types, return_type = infer_annotation(type_comments)
    arg_strs = []
    for arg, kind in arg_types:
        arg_str = str(arg)
        if kind == ARG_STAR:
            arg_str = '*%s' % arg_str
        elif kind == ARG_STARSTAR:
            arg_str = '**%s' % arg_str
        arg_strs.append(arg_str)
    return {
        'arg_types': arg_strs,
        'return_type': str(return_type),
    }
Example #3
0
def generate_annotations_json(source_path,
                              target_path,
                              source_stream=None,
                              target_stream=None):
    # type: (str, str, Optional[IO[str]], Optional[IO[str]]) -> None
    """Produce annotation data JSON file from a JSON file with runtime-collected types.

    Data formats:

    * The source JSON is a list of pyannotate_tools.annotations.parse.RawEntry items.
    * The output JSON is a list of FunctionData items.
    """
    items = parse_json(source_path, source_stream)
    results = []
    for item in items:
        arg_types, return_type = infer_annotation(item.type_comments)
        arg_strs = []
        for arg, kind in arg_types:
            arg_str = str(arg)
            if kind == ARG_STAR:
                arg_str = '*%s' % arg_str
            elif kind == ARG_STARSTAR:
                arg_str = '**%s' % arg_str
            arg_strs.append(arg_str)
        signature = {
            'arg_types': arg_strs,
            'return_type': str(return_type),
        }  # type: Signature
        data = {
            'path': item.path,
            'line': item.line,
            'func_name': item.func_name,
            'signature': signature,
            'samples': item.samples
        }  # type: FunctionData
        results.append(data)
    if target_stream:
        json.dump(results, target_stream, sort_keys=True, indent=4)
    else:
        with open(target_path, 'w') as f:
            json.dump(results, f, sort_keys=True, indent=4)
Example #4
0
 def assert_infer(self, comments, expected):
     # type: (List[str], Tuple[List[Tuple[AbstractType, str]], AbstractType]) -> None
     actual = infer_annotation(comments)
     assert actual == expected