Example #1
0
 def test_ambiguous_kind(self):
     # type: () -> None
     data = """
     [
         {
             "path": "pkg/thing.py",
             "line": 422,
             "func_name": "my_function",
             "type_comments": [
                 "(List[int], str) -> None",
                 "(List[int], *str) -> None"
             ],
             "samples": 3
         }
     ]
     """
     with self.assertRaises(InferError) as e:
         with self.temporary_json_file(data) as source:
             generate_annotations_json(source.name,
                                       '/dummy',
                                       source_stream=source)
     assert str(e.exception) == textwrap.dedent("""\
         Ambiguous argument kinds:
         (List[int], str) -> None
         (List[int], *str) -> None""")
Example #2
0
def main():
    # type: () -> None
    # Parse command line.
    args = parser.parse_args()
    if not args.files:
        parser.error("At least one file/directory is required")

    # Set up logging handler.
    level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(format='%(message)s', level=level)

    # Set up temporary file for pass 2 -> pass 3.
    tf = tempfile.NamedTemporaryFile()

    # Run pass 2 with output written to a temporary file.
    infile = args.type_info
    generate_annotations_json(infile, tf.name)

    # Run pass 3 reading from a temporary file.
    FixAnnotateJson.stub_json_file = tf.name
    fixers = ['pyannotate_tools.fixes.fix_annotate_json']
    flags = {'print_function': args.print_function}
    rt = StdoutRefactoringTool(fixers=fixers,
                               options=flags,
                               explicit=fixers,
                               nobackups=True,
                               show_diffs=not args.quiet)
    if not rt.errors:
        rt.refactor(args.files, write=args.write, num_processes=args.processes)
        if args.processes == 1:
            rt.summarize()
        else:
            logging.info("(In multi-process per-file warnings are lost)")
    if not args.write:
        logging.info("NOTE: this was a dry run; use -w to write files")
Example #3
0
    def test_generation(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(List[int], str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        target = tempfile.NamedTemporaryFile(mode='w+')
        with self.temporary_json_file(data) as source:
            generate_annotations_json(source.name,
                                      target.name,
                                      source_stream=source,
                                      target_stream=target)

        target.flush()
        target.seek(0)
        actual = target.read()
        actual = actual.replace(' \n', '\n')
        expected = textwrap.dedent("""\
            [
                {
                    "func_name": "my_function",
                    "line": 422,
                    "path": "pkg/thing.py",
                    "samples": 3,
                    "signature": {
                        "arg_types": [
                            "List[int]",
                            "str"
                        ],
                        "return_type": "None"
                    }
                }
            ]""")
        assert actual == expected
Example #4
0
    def test_generation(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(List[int], str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with self.temporary_file() as target_path:
            with self.temporary_json_file(data) as source_path:
                generate_annotations_json(source_path, target_path)
            with open(target_path) as target:
                actual = target.read()

        actual = actual.replace(' \n', '\n')
        expected = textwrap.dedent("""\
            [
                {
                    "func_name": "my_function",
                    "line": 422,
                    "path": "pkg/thing.py",
                    "samples": 3,
                    "signature": {
                        "arg_types": [
                            "List[int]",
                            "str"
                        ],
                        "return_type": "None"
                    }
                }
            ]""")
        assert actual == expected