Example #1
0
 def test_parse_json(self):
     # type: () -> None
     data = """
     [
         {
             "path": "pkg/thing.py",
             "line": 422,
             "func_name": "my_function",
             "type_comments": [
                 "(int) -> None",
                 "(str) -> None"
             ],
             "samples": 3
         }
     ]
     """
     f = None
     try:
         with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
             f.write(data)
         result = parse_json(f.name)
     finally:
         if f is not None:
             os.remove(f.name)
     assert len(result) == 1
     item = result[0]
     assert item.path == 'pkg/thing.py'
     assert item.line == 422
     assert item.func_name == 'my_function'
     assert item.type_comments == ['(int) -> None', '(str) -> None']
     assert item.samples == 3
Example #2
0
    def test_parse_json(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(int) -> None",
                    "(str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with tempfile.NamedTemporaryFile(mode='w+') as f:
            f.write(data)
            f.flush()
            f.seek(0)

            result = parse_json(f.name, f)
        assert len(result) == 1
        item = result[0]
        assert item.path == 'pkg/thing.py'
        assert item.line == 422
        assert item.func_name == 'my_function'
        assert item.type_comments == ['(int) -> None', '(str) -> None']
        assert item.samples == 3
Example #3
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 #4
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 #5
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:
        signature = unify_type_comments(item.type_comments)
        data = {
            'path': item.path,
            'line': item.line,
            'func_name': item.func_name,
            'signature': signature,
            'samples': item.samples
        }  # type: FunctionData
        results.append(data)
    return results