Example #1
0
def main(args_override=None):
    # type: (Optional[List[str]]) -> None

    # Parse command line.
    args = parser.parse_args(args_override)
    if not args.files and not args.dump:
        parser.error("At least one file/directory is required")

    if args.python_version not in ('2', '3'):
        sys.exit('--python-version must be 2 or 3')

    annotation_style = 'py' + args.python_version

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

    if args.dump:
        dump_annotations(args.type_info, args.files)
        return

    if args.auto_any:
        fixers = ['pyannotate_tools.fixes.fix_annotate']
    else:
        # Produce nice error message if type_info.json not found.
        try:
            with open(args.type_info) as f:
                contents = f.read()
        except IOError as err:
            sys.exit("Can't open type info file: %s" % err)

        # Run pass 2 with output into a variable.
        if args.uses_signature:
            data = json.loads(contents)  # type: List[Any]
        else:
            data = generate_annotations_json_string(
                args.type_info,
                only_simple=args.only_simple)

        # Run pass 3 with input from that variable.
        FixAnnotateJson.init_stub_json_from_data(data, args.files[0])
        fixers = ['pyannotate_tools.fixes.fix_annotate_json']

    flags = {'print_function': args.print_function,
             'annotation_style': annotation_style}
    rt = ModifiedRefactoringTool(
        fixers=fixers,
        options=flags,
        explicit=fixers,
        nobackups=True,
        show_diffs=not args.quiet)
    if not rt.errors:
        with FixAnnotateJson.max_line_drift_set(args.max_line_drift):
            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 #2
0
def main(args_override=None):
    # type: (Optional[List[str]]) -> None
    # Parse command line.
    args = parser.parse_args(args_override)
    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)

    # Run pass 2 with output into a variable.
    infile = args.type_info
    data = generate_annotations_json_string(infile)  # type: List[Any]

    # Run pass 3 with input from that variable.
    FixAnnotateJson.init_stub_json_from_data(data, args.files[0])
    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")
 def test_line_number_drift_allowed(self):
     self.setTestData(
         [{"func_name": "yep",
           "path": "<string>",
           "line": 10,
           "signature": {
               "arg_types": ["int"],
               "return_type": "int"},
           }])
     a = """\
         def yep(a):
             return a
         """
     b = """\
         def yep(a):
             # type: (int) -> int
             return a
         """
     with FixAnnotateJson.max_line_drift_set(10):
         self.check(a, b)