Example #1
0
File: main.py Project: darjus/mypy
def expand_dir(arg: str) -> List[BuildSource]:
    """Convert a directory name to a list of sources to build."""
    dir, mod = crawl_up(arg)
    if not mod:
        # It's a directory without an __init__.py[i].
        # List all the .py[i] files (but not recursively).
        targets = []  # type: List[BuildSource]
        for name in os.listdir(dir):
            stripped = strip_py(name)
            if stripped:
                path = os.path.join(dir, name)
                targets.append(BuildSource(path, stripped, None))
        return targets

    else:
        lib_path = [dir]
        return build.find_modules_recursive(mod, lib_path)
Example #2
0
def expand_dir(arg: str) -> List[BuildSource]:
    """Convert a directory name to a list of sources to build."""
    dir, mod = crawl_up(arg)
    if not mod:
        # It's a directory without an __init__.py[i].
        # List all the .py[i] files (but not recursively).
        targets = []  # type: List[BuildSource]
        for name in os.listdir(dir):
            stripped = strip_py(name)
            if stripped:
                path = os.path.join(dir, name)
                targets.append(BuildSource(path, stripped, None))
        return targets

    else:
        lib_path = [dir]
        return build.find_modules_recursive(mod, lib_path)
Example #3
0
def build_stubs(mod):
    data_dir = default_data_dir(None)
    options = Options()
    options.python_version = (3, 6)
    lib_path = default_lib_path(data_dir,
                                options.python_version,
                                custom_typeshed_dir=None)
    sources = find_modules_recursive(mod, lib_path)
    try:
        res = build.build(sources=sources, options=options)
        messages = res.errors
    except CompileError as error:
        messages = error.messages

    if messages:
        for msg in messages:
            print(msg)
        sys.exit(1)
    return res.files
Example #4
0
def build_stubs(mod):
    data_dir = default_data_dir(None)
    options = Options()
    options.python_version = (3, 6)
    lib_path = default_lib_path(data_dir,
                                options.python_version,
                                custom_typeshed_dir=None)
    sources = find_modules_recursive(mod, lib_path)
    try:
        res = build.build(sources=sources,
                          options=options)
        messages = res.errors
    except CompileError as error:
        messages = error.messages

    if messages:
        for msg in messages:
            print(msg)
        sys.exit(1)
    return res.files
Example #5
0
def build_stubs(id):
    data_dir = default_data_dir(None)
    options = Options()
    options.python_version = (3, 6)
    lib_path = default_lib_path(data_dir,
                                options.python_version,
                                custom_typeshed_dir=None)
    sources = find_modules_recursive(id, lib_path)
    if not sources:
        sys.exit('Error: Cannot find module {}'.format(repr(id)))
    msg = []
    try:
        res = build.build(sources=sources,
                          options=options)
        msg = res.errors
    except CompileError as e:
        msg = e.messages
    if msg:
        for m in msg:
            print(m)
        sys.exit(1)
    return res
Example #6
0
File: main.py Project: tony/mypy
def process_options() -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    # Make the help output a little less jarring.
    help_factory = (lambda prog: argparse.RawDescriptionHelpFormatter(
        prog=prog, max_help_position=28))
    parser = argparse.ArgumentParser(prog='mypy',
                                     epilog=FOOTER,
                                     formatter_class=help_factory)

    def parse_version(v):
        m = re.match(r'\A(\d)\.(\d+)\Z', v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError(
                "Invalid python version '{}' (expected format: 'x.y')".format(
                    v))

    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        help="more verbose messages")
    parser.add_argument(
        '-V',
        '--version',
        action='version',  # type: ignore # see typeshed#124
        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version',
                        type=parse_version,
                        metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--py2',
                        dest='python_version',
                        action='store_const',
                        const=defaults.PYTHON2_VERSION,
                        help="use Python 2 mode")
    parser.add_argument('-s',
                        '--silent-imports',
                        action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument('--silent',
                        action='store_true',
                        help="deprecated name for --silent-imports")
    parser.add_argument(
        '--almost-silent',
        action='store_true',
        help="like --silent-imports but reports the imports as errors")
    parser.add_argument(
        '--disallow-untyped-calls',
        action='store_true',
        help="disallow calling functions without type annotations"
        " from functions with type annotations")
    parser.add_argument(
        '--disallow-untyped-defs',
        action='store_true',
        help="disallow defining functions without type annotations"
        " or with incomplete type annotations")
    parser.add_argument(
        '--check-untyped-defs',
        action='store_true',
        help="type check the interior of functions without type annotations")
    parser.add_argument(
        '--warn-incomplete-stub',
        action='store_true',
        help="warn if missing type annotation in typeshed, only relevant with"
        " --check-untyped-defs enabled")
    parser.add_argument(
        '--warn-redundant-casts',
        action='store_true',
        help="warn about casting an expression to its inferred type")
    parser.add_argument('--fast-parser',
                        action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i',
                        '--incremental',
                        action='store_true',
                        help="enable experimental module cache")
    parser.add_argument('--strict-optional',
                        action='store_true',
                        help="enable experimental strict Optional checks")
    parser.add_argument('-f',
                        '--dirty-stubs',
                        action='store_true',
                        help="don't warn if typeshed is out of sync")
    parser.add_argument('--pdb',
                        action='store_true',
                        help="invoke pdb on fatal error")
    parser.add_argument('--use-python-path',
                        action='store_true',
                        help="an anti-pattern")
    parser.add_argument('--stats', action='store_true', help="dump stats")
    parser.add_argument('--inferstats',
                        action='store_true',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing',
                        metavar='MODULE',
                        help="use a custom typing module")

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    report_group.add_argument('--html-report', metavar='DIR')
    report_group.add_argument('--old-html-report', metavar='DIR')
    report_group.add_argument('--xslt-html-report', metavar='DIR')
    report_group.add_argument('--xml-report', metavar='DIR')
    report_group.add_argument('--txt-report', metavar='DIR')
    report_group.add_argument('--xslt-txt-report', metavar='DIR')
    report_group.add_argument('--linecount-report', metavar='DIR')

    code_group = parser.add_argument_group(
        title='How to specify the code to type check')
    code_group.add_argument(
        '-m',
        '--module',
        action='append',
        dest='modules',
        help="type-check module; can repeat for more modules")
    # TODO: `mypy -c A -c B` and `mypy -p A -p B` currently silently
    # ignore A (last option wins).  Perhaps -c, -m and -p could just
    # be command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c',
                            '--command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p',
                            '--package',
                            help="type-check all files in a directory")
    code_group.add_argument('files',
                            nargs='*',
                            help="type-check given files or directories")

    args = parser.parse_args()

    # --use-python-path is no longer supported; explain why.
    if args.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )
    # --silent is deprecated; warn about this.
    if args.silent:
        print("Warning: --silent is deprecated; use --silent-imports",
              file=sys.stderr)

    # Check for invalid argument combinations.
    code_methods = sum(
        bool(c)
        for c in [args.modules, args.command, args.package, args.files])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error(
            "May only specify one of: module, package, files, or command.")

    if args.use_python_path and args.python_version and args.python_version[
            0] == 2:
        parser.error(
            'Python version 2 (or --py2) specified, '
            'but --use-python-path will search in sys.path of Python 3')

    # Set options.
    options = Options()
    options.dirty_stubs = args.dirty_stubs
    options.python_path = args.use_python_path
    options.pdb = args.pdb
    options.custom_typing_module = args.custom_typing

    # Set build flags.
    if args.python_version is not None:
        options.pyversion = args.python_version

    if args.verbose:
        options.build_flags.extend(args.verbose * [build.VERBOSE])

    if args.stats:
        options.build_flags.append(build.DUMP_TYPE_STATS)

    if args.inferstats:
        options.build_flags.append(build.DUMP_INFER_STATS)

    if args.silent_imports or args.silent:
        options.build_flags.append(build.SILENT_IMPORTS)
    if args.almost_silent:
        options.build_flags.append(build.SILENT_IMPORTS)
        options.build_flags.append(build.ALMOST_SILENT)

    if args.disallow_untyped_calls:
        options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)

    if args.disallow_untyped_defs:
        options.build_flags.append(build.DISALLOW_UNTYPED_DEFS)

    if args.check_untyped_defs:
        options.build_flags.append(build.CHECK_UNTYPED_DEFS)

    if args.warn_incomplete_stub:
        options.build_flags.append(build.WARN_INCOMPLETE_STUB)
    if args.warn_redundant_casts:
        options.build_flags.append(build.WARN_REDUNDANT_CASTS)

    # experimental
    if args.fast_parser:
        options.build_flags.append(build.FAST_PARSER)
    if args.incremental:
        options.build_flags.append(build.INCREMENTAL)
    if args.strict_optional:
        experiments.STRICT_OPTIONAL = True

    # Set reports.
    for flag, val in vars(args).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if args.modules:
        options.build_flags.append(build.MODULE)
        targets = [BuildSource(None, m, None) for m in args.modules]
        return targets, options
    elif args.package:
        if os.sep in args.package or os.altsep and os.altsep in args.package:
            fail("Package name '{}' cannot have a slash in it.".format(
                args.package))
        options.build_flags.append(build.MODULE)
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(args.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(args.package))
        return targets, options
    elif args.command:
        options.build_flags.append(build.PROGRAM_TEXT)
        return [BuildSource(None, None, args.command)], options
    else:
        targets = []
        for f in args.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'".format(
                        f))
                targets.extend(sub_targets)
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #7
0
def process_options(
    args: List[str],
    require_targets: bool = True,
    server_options: bool = False,
) -> Tuple[List[BuildSource], Options]:
    """Parse command line arguments."""

    parser = argparse.ArgumentParser(prog='mypy',
                                     epilog=FOOTER,
                                     fromfile_prefix_chars='@',
                                     formatter_class=AugmentedHelpFormatter)

    strict_flag_names = []  # type: List[str]
    strict_flag_assignments = []  # type: List[Tuple[str, bool]]

    def add_invertible_flag(flag: str,
                            *,
                            inverse: Optional[str] = None,
                            default: bool,
                            dest: Optional[str] = None,
                            help: str,
                            strict_flag: bool = False) -> None:
        if inverse is None:
            inverse = invert_flag_name(flag)

        if help is not argparse.SUPPRESS:
            help += " (inverse: {})".format(inverse)

        arg = parser.add_argument(
            flag,  # type: ignore  # incorrect stub for add_argument
            action='store_false' if default else 'store_true',
            dest=dest,
            help=help)
        dest = arg.dest
        arg = parser.add_argument(
            inverse,  # type: ignore  # incorrect stub for add_argument
            action='store_true' if default else 'store_false',
            dest=dest,
            help=argparse.SUPPRESS)
        if strict_flag:
            assert dest is not None
            strict_flag_names.append(flag)
            strict_flag_assignments.append((dest, not default))

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version',
                        type=parse_version,
                        metavar='x.y',
                        help='use Python x.y')
    parser.add_argument(
        '--platform',
        action='store',
        metavar='PLATFORM',
        help="typecheck special-cased code for the given OS platform "
        "(defaults to sys.platform).")
    parser.add_argument('-2',
                        '--py2',
                        dest='python_version',
                        action='store_const',
                        const=defaults.PYTHON2_VERSION,
                        help="use Python 2 mode")
    parser.add_argument('--ignore-missing-imports',
                        action='store_true',
                        help="silently ignore imports of missing modules")
    parser.add_argument('--follow-imports',
                        choices=['normal', 'silent', 'skip', 'error'],
                        default='normal',
                        help="how to treat imports (default normal)")
    parser.add_argument(
        '--disallow-any-unimported',
        default=False,
        action='store_true',
        help="disallow Any types resulting from unfollowed imports")
    parser.add_argument('--disallow-any-expr',
                        default=False,
                        action='store_true',
                        help='disallow all expressions that have type Any')
    parser.add_argument(
        '--disallow-any-decorated',
        default=False,
        action='store_true',
        help='disallow functions that have Any in their signature '
        'after decorator transformation')
    parser.add_argument('--disallow-any-explicit',
                        default=False,
                        action='store_true',
                        help='disallow explicit Any in type positions')
    parser.add_argument(
        '--disallow-any-generics',
        default=False,
        action='store_true',
        help='disallow usage of generic types that do not specify explicit '
        'type parameters')
    add_invertible_flag(
        '--disallow-untyped-calls',
        default=False,
        strict_flag=True,
        help="disallow calling functions without type annotations"
        " from functions with type annotations")
    add_invertible_flag(
        '--disallow-untyped-defs',
        default=False,
        strict_flag=True,
        help="disallow defining functions without type annotations"
        " or with incomplete type annotations")
    add_invertible_flag(
        '--disallow-incomplete-defs',
        default=False,
        strict_flag=True,
        help="disallow defining functions with incomplete type annotations")
    add_invertible_flag(
        '--check-untyped-defs',
        default=False,
        strict_flag=True,
        help="type check the interior of functions without type annotations")
    add_invertible_flag(
        '--disallow-subclassing-any',
        default=False,
        strict_flag=True,
        help="disallow subclassing values of type 'Any' when defining classes")
    add_invertible_flag(
        '--warn-incomplete-stub',
        default=False,
        help="warn if missing type annotation in typeshed, only relevant with"
        " --check-untyped-defs enabled")
    add_invertible_flag(
        '--disallow-untyped-decorators',
        default=False,
        strict_flag=True,
        help="disallow decorating typed functions with untyped decorators")
    add_invertible_flag(
        '--warn-redundant-casts',
        default=False,
        strict_flag=True,
        help="warn about casting an expression to its inferred type")
    add_invertible_flag(
        '--no-warn-no-return',
        dest='warn_no_return',
        default=True,
        help="do not warn about functions that end without returning")
    add_invertible_flag('--warn-return-any',
                        default=False,
                        strict_flag=True,
                        help="warn about returning values of type Any"
                        " from non-Any typed functions")
    add_invertible_flag('--warn-unused-ignores',
                        default=False,
                        strict_flag=True,
                        help="warn about unneeded '# type: ignore' comments")
    add_invertible_flag(
        '--warn-unused-configs',
        default=False,
        strict_flag=True,
        help="warn about unused '[mypy-<pattern>]' config sections")
    add_invertible_flag(
        '--show-error-context',
        default=False,
        dest='show_error_context',
        help='Precede errors with "note:" messages explaining context')
    add_invertible_flag(
        '--no-implicit-optional',
        default=False,
        strict_flag=True,
        help="don't assume arguments with default values of None are Optional")
    parser.add_argument(
        '-i',
        '--incremental',
        action='store_true',
        help="enable module cache, (inverse: --no-incremental)")
    parser.add_argument('--no-incremental',
                        action='store_false',
                        dest='incremental',
                        help=argparse.SUPPRESS)
    parser.add_argument('--quick-and-dirty',
                        action='store_true',
                        help="use cache even if dependencies out of date "
                        "(implies --incremental)")
    parser.add_argument(
        '--cache-dir',
        action='store',
        metavar='DIR',
        help="store module cache info in the given folder in incremental mode "
        "(defaults to '{}')".format(defaults.CACHE_DIR))
    parser.add_argument(
        '--cache-fine-grained',
        action='store_true',
        help="include fine-grained dependency information in the cache")
    parser.add_argument('--skip-version-check',
                        action='store_true',
                        help="allow using cache written by older mypy version")
    add_invertible_flag('--strict-optional',
                        default=False,
                        strict_flag=True,
                        help="enable experimental strict Optional checks")
    parser.add_argument(
        '--strict-optional-whitelist',
        metavar='GLOB',
        nargs='*',
        help="suppress strict Optional errors in all but the provided files "
        "(experimental -- read documentation before using!).  "
        "Implies --strict-optional.  Has the undesirable side-effect of "
        "suppressing other errors in non-whitelisted files.")
    parser.add_argument('--junit-xml',
                        help="write junit.xml to the given file")
    parser.add_argument('--pdb',
                        action='store_true',
                        help="invoke pdb on fatal error")
    parser.add_argument('--show-traceback',
                        '--tb',
                        action='store_true',
                        help="show traceback on fatal error")
    parser.add_argument('--stats',
                        action='store_true',
                        dest='dump_type_stats',
                        help="dump stats")
    parser.add_argument('--inferstats',
                        action='store_true',
                        dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing',
                        metavar='MODULE',
                        dest='custom_typing_module',
                        help="use a custom typing module")
    parser.add_argument('--custom-typeshed-dir',
                        metavar='DIR',
                        help="use the custom typeshed in DIR")
    parser.add_argument('--scripts-are-modules',
                        action='store_true',
                        help="Script x becomes module x instead of __main__")
    parser.add_argument('--config-file',
                        help="Configuration file, must have a [mypy] section "
                        "(defaults to {})".format(defaults.CONFIG_FILE))
    add_invertible_flag('--show-column-numbers',
                        default=False,
                        help="Show column numbers in error messages")
    parser.add_argument(
        '--find-occurrences',
        metavar='CLASS.MEMBER',
        dest='special-opts:find_occurrences',
        help="print out all usages of a class member (experimental)")
    strict_help = "Strict mode. Enables the following flags: {}".format(
        ", ".join(strict_flag_names))
    parser.add_argument('--strict',
                        action='store_true',
                        dest='special-opts:strict',
                        help=strict_help)
    parser.add_argument('--shadow-file',
                        nargs=2,
                        metavar=('SOURCE_FILE', 'SHADOW_FILE'),
                        dest='shadow_file',
                        help='Typecheck SHADOW_FILE in place of SOURCE_FILE.')
    # hidden options
    # --debug-cache will disable any cache-related compressions/optimizations,
    # which will make the cache writing process output pretty-printed JSON (which
    # is easier to debug).
    parser.add_argument('--debug-cache',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # --dump-deps will dump all fine-grained dependencies to stdout
    parser.add_argument('--dump-deps',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # --dump-graph will dump the contents of the graph of SCCs and exit.
    parser.add_argument('--dump-graph',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # --semantic-analysis-only does exactly that.
    parser.add_argument('--semantic-analysis-only',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # --local-partial-types disallows partial types spanning module top level and a function
    # (implicitly defined in fine-grained incremental mode)
    parser.add_argument('--local-partial-types',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # deprecated options
    parser.add_argument('--disallow-any',
                        dest='special-opts:disallow_any',
                        help=argparse.SUPPRESS)
    add_invertible_flag('--strict-boolean',
                        default=False,
                        help=argparse.SUPPRESS)
    parser.add_argument('-f',
                        '--dirty-stubs',
                        action='store_true',
                        dest='special-opts:dirty_stubs',
                        help=argparse.SUPPRESS)
    parser.add_argument('--use-python-path',
                        action='store_true',
                        dest='special-opts:use_python_path',
                        help=argparse.SUPPRESS)
    parser.add_argument('-s',
                        '--silent-imports',
                        action='store_true',
                        dest='special-opts:silent_imports',
                        help=argparse.SUPPRESS)
    parser.add_argument('--almost-silent',
                        action='store_true',
                        dest='special-opts:almost_silent',
                        help=argparse.SUPPRESS)
    parser.add_argument('--fast-parser',
                        action='store_true',
                        dest='special-opts:fast_parser',
                        help=argparse.SUPPRESS)
    parser.add_argument('--no-fast-parser',
                        action='store_true',
                        dest='special-opts:no_fast_parser',
                        help=argparse.SUPPRESS)
    if server_options:
        parser.add_argument('--experimental',
                            action='store_true',
                            dest='fine_grained_incremental',
                            help="enable fine-grained incremental mode")
        parser.add_argument(
            '--use-fine-grained-cache',
            action='store_true',
            help="use the cache in fine-grained incremental mode")

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    for report_type in sorted(reporter_classes):
        report_group.add_argument('--%s-report' %
                                  report_type.replace('_', '-'),
                                  metavar='DIR',
                                  dest='special-opts:%s_report' % report_type)

    code_group = parser.add_argument_group(
        title='How to specify the code to type check')
    code_group.add_argument(
        '-m',
        '--module',
        action='append',
        metavar='MODULE',
        dest='special-opts:modules',
        help="type-check module; can repeat for more modules")
    # TODO: `mypy -p A -p B` currently silently ignores A
    # (last option wins).  Perhaps -c, -m and -p could just be
    # command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c',
                            '--command',
                            action='append',
                            metavar='PROGRAM_TEXT',
                            dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p',
                            '--package',
                            metavar='PACKAGE',
                            dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files',
                            nargs='*',
                            dest='special-opts:files',
                            help="type-check given files or directories")

    # Parse arguments once into a dummy namespace so we can get the
    # filename for the config file and know if the user requested all strict options.
    dummy = argparse.Namespace()
    parser.parse_args(args, dummy)
    config_file = dummy.config_file
    if config_file is not None and not os.path.exists(config_file):
        parser.error("Cannot find config file '%s'" % config_file)

    # Parse config file first, so command line can override.
    options = Options()
    parse_config_file(options, config_file)

    # Set strict flags before parsing (if strict mode enabled), so other command
    # line options can override.
    if getattr(dummy, 'special-opts:strict'):
        for dest, value in strict_flag_assignments:
            setattr(options, dest, value)

    # Parse command line for real, using a split namespace.
    special_opts = argparse.Namespace()
    parser.parse_args(args,
                      SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )

    # Process deprecated options
    if special_opts.disallow_any:
        print(
            "--disallow-any option was split up into multiple flags. "
            "See http://mypy.readthedocs.io/en/latest/command_line.html#disallow-any-flags"
        )
    if options.strict_boolean:
        print(
            "Warning: --strict-boolean is deprecated; "
            "see https://github.com/python/mypy/issues/3195",
            file=sys.stderr)
    if special_opts.almost_silent:
        print(
            "Warning: --almost-silent has been replaced by "
            "--follow-imports=errors",
            file=sys.stderr)
        if options.follow_imports == 'normal':
            options.follow_imports = 'errors'
    elif special_opts.silent_imports:
        print(
            "Warning: --silent-imports has been replaced by "
            "--ignore-missing-imports --follow-imports=skip",
            file=sys.stderr)
        options.ignore_missing_imports = True
        if options.follow_imports == 'normal':
            options.follow_imports = 'skip'
    if special_opts.dirty_stubs:
        print(
            "Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
            "checks the git status of stubs.",
            file=sys.stderr)
    if special_opts.fast_parser:
        print("Warning: --fast-parser is now the default (and only) parser.")
    if special_opts.no_fast_parser:
        print(
            "Warning: --no-fast-parser no longer has any effect.  The fast parser "
            "is now mypy's default and only parser.")

    # Check for invalid argument combinations.
    if require_targets:
        code_methods = sum(
            bool(c) for c in [
                special_opts.modules, special_opts.command,
                special_opts.package, special_opts.files
            ])
        if code_methods == 0:
            parser.error("Missing target module, package, files, or command.")
        elif code_methods > 1:
            parser.error(
                "May only specify one of: module, package, files, or command.")

    # Set build flags.
    if options.strict_optional_whitelist is not None:
        # TODO: Deprecate, then kill this flag
        options.strict_optional = True
    if special_opts.find_occurrences:
        experiments.find_occurrences = special_opts.find_occurrences.split('.')
        assert experiments.find_occurrences is not None
        if len(experiments.find_occurrences) < 2:
            parser.error("Can only find occurrences of class members.")
        if len(experiments.find_occurrences) != 2:
            parser.error(
                "Can only find occurrences of non-nested class members.")

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Let quick_and_dirty imply incremental.
    if options.quick_and_dirty:
        options.incremental = True

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it.".format(
                special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        targets = [BuildSource(None, None, '\n'.join(special_opts.command))]
        return targets, options
    else:
        targets = create_source_list(special_opts.files, options)
        return targets, options
Example #8
0
def process_options(
        args: List[str],
        require_targets: bool = True) -> Tuple[List[BuildSource], Options]:
    """Parse command line arguments."""

    # Make the help output a little less jarring.
    help_factory = (lambda prog: argparse.RawDescriptionHelpFormatter(
        prog=prog, max_help_position=28))
    parser = argparse.ArgumentParser(prog='mypy',
                                     epilog=FOOTER,
                                     fromfile_prefix_chars='@',
                                     formatter_class=help_factory)

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version',
                        type=parse_version,
                        metavar='x.y',
                        help='use Python x.y')
    parser.add_argument(
        '--platform',
        action='store',
        metavar='PLATFORM',
        help="typecheck special-cased code for the given OS platform "
        "(defaults to sys.platform).")
    parser.add_argument('-2',
                        '--py2',
                        dest='python_version',
                        action='store_const',
                        const=defaults.PYTHON2_VERSION,
                        help="use Python 2 mode")
    parser.add_argument('-s',
                        '--silent-imports',
                        action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument(
        '--almost-silent',
        action='store_true',
        help="like --silent-imports but reports the imports as errors")
    parser.add_argument(
        '--disallow-untyped-calls',
        action='store_true',
        help="disallow calling functions without type annotations"
        " from functions with type annotations")
    parser.add_argument(
        '--disallow-untyped-defs',
        action='store_true',
        help="disallow defining functions without type annotations"
        " or with incomplete type annotations")
    parser.add_argument(
        '--check-untyped-defs',
        action='store_true',
        help="type check the interior of functions without type annotations")
    parser.add_argument(
        '--disallow-subclassing-any',
        action='store_true',
        help="disallow subclassing values of type 'Any' when defining classes")
    parser.add_argument(
        '--warn-incomplete-stub',
        action='store_true',
        help="warn if missing type annotation in typeshed, only relevant with"
        " --check-untyped-defs enabled")
    parser.add_argument(
        '--warn-redundant-casts',
        action='store_true',
        help="warn about casting an expression to its inferred type")
    parser.add_argument('--warn-no-return',
                        action='store_true',
                        help="warn about functions that end without returning")
    parser.add_argument('--warn-unused-ignores',
                        action='store_true',
                        help="warn about unneeded '# type: ignore' comments")
    parser.add_argument('--hide-error-context',
                        action='store_true',
                        dest='hide_error_context',
                        help="Hide context notes before errors")
    parser.add_argument('--fast-parser',
                        action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i',
                        '--incremental',
                        action='store_true',
                        help="enable experimental module cache")
    parser.add_argument(
        '--cache-dir',
        action='store',
        metavar='DIR',
        help="store module cache info in the given folder in incremental mode "
        "(defaults to '{}')".format(defaults.CACHE_DIR))
    parser.add_argument('--strict-optional',
                        action='store_true',
                        dest='strict_optional',
                        help="enable experimental strict Optional checks")
    parser.add_argument(
        '--strict-optional-whitelist',
        metavar='GLOB',
        nargs='*',
        help="suppress strict Optional errors in all but the provided files "
        "(experimental -- read documentation before using!).  "
        "Implies --strict-optional.  Has the undesirable side-effect of "
        "suppressing other errors in non-whitelisted files.")
    parser.add_argument('--junit-xml',
                        help="write junit.xml to the given file")
    parser.add_argument('--pdb',
                        action='store_true',
                        help="invoke pdb on fatal error")
    parser.add_argument('--show-traceback',
                        '--tb',
                        action='store_true',
                        help="show traceback on fatal error")
    parser.add_argument('--stats',
                        action='store_true',
                        dest='dump_type_stats',
                        help="dump stats")
    parser.add_argument('--inferstats',
                        action='store_true',
                        dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing',
                        metavar='MODULE',
                        dest='custom_typing_module',
                        help="use a custom typing module")
    parser.add_argument('--custom-typeshed-dir',
                        metavar='DIR',
                        help="use the custom typeshed in DIR")
    parser.add_argument('--scripts-are-modules',
                        action='store_true',
                        help="Script x becomes module x instead of __main__")
    parser.add_argument('--config-file',
                        help="Configuration file, must have a [mypy] section "
                        "(defaults to {})".format(defaults.CONFIG_FILE))
    parser.add_argument('--show-column-numbers',
                        action='store_true',
                        dest='show_column_numbers',
                        help="Show column numbers in error messages")
    parser.add_argument(
        '--find-occurrences',
        metavar='CLASS.MEMBER',
        dest='special-opts:find_occurrences',
        help="print out all usages of a class member (experimental)")
    # hidden options
    # --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
    # Useful for tools to make transformations to a file to get more
    # information from a mypy run without having to change the file in-place
    # (e.g. by adding a call to reveal_type).
    parser.add_argument('--shadow-file',
                        metavar='PATH',
                        nargs=2,
                        dest='shadow_file',
                        help=argparse.SUPPRESS)
    # --debug-cache will disable any cache-related compressions/optimizations,
    # which will make the cache writing process output pretty-printed JSON (which
    # is easier to debug).
    parser.add_argument('--debug-cache',
                        action='store_true',
                        help=argparse.SUPPRESS)
    # deprecated options
    parser.add_argument('--silent',
                        action='store_true',
                        dest='special-opts:silent',
                        help=argparse.SUPPRESS)
    parser.add_argument('-f',
                        '--dirty-stubs',
                        action='store_true',
                        dest='special-opts:dirty_stubs',
                        help=argparse.SUPPRESS)
    parser.add_argument('--use-python-path',
                        action='store_true',
                        dest='special-opts:use_python_path',
                        help=argparse.SUPPRESS)

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    for report_type in sorted(reporter_classes):
        report_group.add_argument('--%s-report' %
                                  report_type.replace('_', '-'),
                                  metavar='DIR',
                                  dest='special-opts:%s_report' % report_type)

    code_group = parser.add_argument_group(
        title='How to specify the code to type check')
    code_group.add_argument(
        '-m',
        '--module',
        action='append',
        metavar='MODULE',
        dest='special-opts:modules',
        help="type-check module; can repeat for more modules")
    # TODO: `mypy -p A -p B` currently silently ignores ignores A
    # (last option wins).  Perhaps -c, -m and -p could just be
    # command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c',
                            '--command',
                            action='append',
                            metavar='PROGRAM_TEXT',
                            dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p',
                            '--package',
                            metavar='PACKAGE',
                            dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files',
                            nargs='*',
                            dest='special-opts:files',
                            help="type-check given files or directories")

    # Parse arguments once into a dummy namespace so we can get the
    # filename for the config file.
    dummy = argparse.Namespace()
    parser.parse_args(args, dummy)
    config_file = dummy.config_file or defaults.CONFIG_FILE

    # Parse config file first, so command line can override.
    options = Options()
    if config_file and os.path.exists(config_file):
        parse_config_file(options, config_file)

    # Parse command line for real, using a split namespace.
    special_opts = argparse.Namespace()
    parser.parse_args(args,
                      SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )

    # warn about deprecated options
    if special_opts.silent:
        print("Warning: --silent is deprecated; use --silent-imports",
              file=sys.stderr)
        options.silent_imports = True
    if special_opts.dirty_stubs:
        print(
            "Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
            "checks the git status of stubs.",
            file=sys.stderr)

    # Check for invalid argument combinations.
    if require_targets:
        code_methods = sum(
            bool(c) for c in [
                special_opts.modules, special_opts.command,
                special_opts.package, special_opts.files
            ])
        if code_methods == 0:
            parser.error("Missing target module, package, files, or command.")
        elif code_methods > 1:
            parser.error(
                "May only specify one of: module, package, files, or command.")

    # Set build flags.
    if options.strict_optional_whitelist is not None:
        # TODO: Deprecate, then kill this flag
        options.strict_optional = True
    if options.strict_optional:
        experiments.STRICT_OPTIONAL = True
    if special_opts.find_occurrences:
        experiments.find_occurrences = special_opts.find_occurrences.split('.')
        if len(experiments.find_occurrences) < 2:
            parser.error("Can only find occurrences of class members.")
        if len(experiments.find_occurrences) != 2:
            parser.error(
                "Can only find occurrences of non-nested class members.")

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it.".format(
                special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        return [BuildSource(None, None,
                            '\n'.join(special_opts.command))], options
    else:
        targets = []
        for f in special_opts.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'".format(
                        f))
                targets.extend(sub_targets)
            else:
                mod = os.path.basename(
                    f) if options.scripts_are_modules else None
                targets.append(BuildSource(f, mod, None))
        return targets, options
Example #9
0
File: main.py Project: nierob/mypy
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    options = Options()
    help = False
    ver = False
    while args and args[0].startswith("-"):
        if args[0] == "--verbose":
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == "--py2":
            # Use Python 2 mode.
            options.pyversion = defaults.PYTHON2_VERSION
            args = args[1:]
        elif args[0] == "--python-version":
            version_components = args[1].split(".")[0:2]
            if len(version_components) != 2:
                fail("Invalid python version {} (expected format: 'x.y')".format(repr(args[1])))
            if not all(item.isdigit() for item in version_components):
                fail("Found non-digit in python version: {}".format(args[1]))
            options.pyversion = (int(version_components[0]), int(version_components[1]))
            args = args[2:]
        elif args[0] == "-f" or args[0] == "--dirty-stubs":
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == "-m" and args[1:]:
            options.build_flags.append(build.MODULE)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == "--package" and args[1:]:
            options.build_flags.append(build.MODULE)
            return build.find_modules_recursive(args[1], [os.getcwd()]), options
        elif args[0] == "-c" and args[1:]:
            options.build_flags.append(build.PROGRAM_TEXT)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ("-h", "--help"):
            help = True
            args = args[1:]
        elif args[0] == "--stats":
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == "--inferstats":
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == "--custom-typing" and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == "--use-python-path":
            options.python_path = True
            args = args[1:]
        elif args[0] == "--version":
            ver = True
            args = args[1:]
        else:
            usage("Unknown option: {}".format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage("Missing target file or module")

    if args[1:]:
        usage("Extra argument: {}".format(args[1]))

    if options.python_path and options.pyversion[0] == 2:
        usage("Python version 2 (or --py2) specified, " "but --use-python-path will search in sys.path of Python 3")

    return [BuildSource(args[0], None, None)], options
Example #10
0
File: main.py Project: rra/mypy
def process_options(args: List[str],
                    require_targets: bool = True
                    ) -> Tuple[List[BuildSource], Options]:
    """Parse command line arguments."""

    # Make the help output a little less jarring.
    help_factory = (lambda prog:
                    argparse.RawDescriptionHelpFormatter(prog=prog,
                                                         max_help_position=28))  # type: Any
    parser = argparse.ArgumentParser(prog='mypy', epilog=FOOTER,
                                     fromfile_prefix_chars='@',
                                     formatter_class=help_factory)

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v', '--verbose', action='count', dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V', '--version', action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version', type=parse_version, metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--platform', action='store', metavar='PLATFORM',
                        help="typecheck special-cased code for the given OS platform "
                        "(defaults to sys.platform).")
    parser.add_argument('-2', '--py2', dest='python_version', action='store_const',
                        const=defaults.PYTHON2_VERSION, help="use Python 2 mode")
    parser.add_argument('--ignore-missing-imports', action='store_true',
                        help="silently ignore imports of missing modules")
    parser.add_argument('--follow-imports', choices=['normal', 'silent', 'skip', 'error'],
                        default='normal', help="how to treat imports (default normal)")
    parser.add_argument('--disallow-untyped-calls', action='store_true',
                        help="disallow calling functions without type annotations"
                        " from functions with type annotations")
    parser.add_argument('--disallow-untyped-defs', action='store_true',
                        help="disallow defining functions without type annotations"
                        " or with incomplete type annotations")
    parser.add_argument('--check-untyped-defs', action='store_true',
                        help="type check the interior of functions without type annotations")
    parser.add_argument('--disallow-subclassing-any', action='store_true',
                        help="disallow subclassing values of type 'Any' when defining classes")
    parser.add_argument('--warn-incomplete-stub', action='store_true',
                        help="warn if missing type annotation in typeshed, only relevant with"
                        " --check-untyped-defs enabled")
    parser.add_argument('--warn-redundant-casts', action='store_true',
                        help="warn about casting an expression to its inferred type")
    parser.add_argument('--warn-no-return', action='store_true',
                        help="warn about functions that end without returning")
    parser.add_argument('--warn-unused-ignores', action='store_true',
                        help="warn about unneeded '# type: ignore' comments")
    parser.add_argument('--show-error-context', action='store_false',
                        dest='hide_error_context',
                        help='Precede errors with "note:" messages explaining context')
    parser.add_argument('--fast-parser', action='store_true',
                        help="enable fast parser (recommended except on Windows)")
    parser.add_argument('-i', '--incremental', action='store_true',
                        help="enable experimental module cache")
    parser.add_argument('--cache-dir', action='store', metavar='DIR',
                        help="store module cache info in the given folder in incremental mode "
                        "(defaults to '{}')".format(defaults.CACHE_DIR))
    parser.add_argument('--strict-optional', action='store_true',
                        dest='strict_optional',
                        help="enable experimental strict Optional checks")
    parser.add_argument('--strict-optional-whitelist', metavar='GLOB', nargs='*',
                        help="suppress strict Optional errors in all but the provided files "
                        "(experimental -- read documentation before using!).  "
                        "Implies --strict-optional.  Has the undesirable side-effect of "
                        "suppressing other errors in non-whitelisted files.")
    parser.add_argument('--junit-xml', help="write junit.xml to the given file")
    parser.add_argument('--pdb', action='store_true', help="invoke pdb on fatal error")
    parser.add_argument('--show-traceback', '--tb', action='store_true',
                        help="show traceback on fatal error")
    parser.add_argument('--stats', action='store_true', dest='dump_type_stats', help="dump stats")
    parser.add_argument('--inferstats', action='store_true', dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
                        help="use a custom typing module")
    parser.add_argument('--custom-typeshed-dir', metavar='DIR',
                        help="use the custom typeshed in DIR")
    parser.add_argument('--scripts-are-modules', action='store_true',
                        help="Script x becomes module x instead of __main__")
    parser.add_argument('--config-file',
                        help="Configuration file, must have a [mypy] section "
                        "(defaults to {})".format(defaults.CONFIG_FILE))
    parser.add_argument('--show-column-numbers', action='store_true',
                        dest='show_column_numbers',
                        help="Show column numbers in error messages")
    parser.add_argument('--find-occurrences', metavar='CLASS.MEMBER',
                        dest='special-opts:find_occurrences',
                        help="print out all usages of a class member (experimental)")
    # hidden options
    # --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
    # Useful for tools to make transformations to a file to get more
    # information from a mypy run without having to change the file in-place
    # (e.g. by adding a call to reveal_type).
    parser.add_argument('--shadow-file', metavar='PATH', nargs=2, dest='shadow_file',
                        help=argparse.SUPPRESS)
    # --debug-cache will disable any cache-related compressions/optimizations,
    # which will make the cache writing process output pretty-printed JSON (which
    # is easier to debug).
    parser.add_argument('--debug-cache', action='store_true', help=argparse.SUPPRESS)
    # --dump-graph will dump the contents of the graph of SCCs and exit.
    parser.add_argument('--dump-graph', action='store_true', help=argparse.SUPPRESS)
    parser.add_argument('--hide-error-context', action='store_true',
                        dest='hide_error_context',
                        help=argparse.SUPPRESS)
    # deprecated options
    parser.add_argument('-f', '--dirty-stubs', action='store_true',
                        dest='special-opts:dirty_stubs',
                        help=argparse.SUPPRESS)
    parser.add_argument('--use-python-path', action='store_true',
                        dest='special-opts:use_python_path',
                        help=argparse.SUPPRESS)
    parser.add_argument('-s', '--silent-imports', action='store_true',
                        dest='special-opts:silent_imports',
                        help=argparse.SUPPRESS)
    parser.add_argument('--almost-silent', action='store_true',
                        dest='special-opts:almost_silent',
                        help=argparse.SUPPRESS)

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    for report_type in sorted(reporter_classes):
        report_group.add_argument('--%s-report' % report_type.replace('_', '-'),
                                  metavar='DIR',
                                  dest='special-opts:%s_report' % report_type)

    code_group = parser.add_argument_group(title='How to specify the code to type check')
    code_group.add_argument('-m', '--module', action='append', metavar='MODULE',
                            dest='special-opts:modules',
                            help="type-check module; can repeat for more modules")
    # TODO: `mypy -p A -p B` currently silently ignores ignores A
    # (last option wins).  Perhaps -c, -m and -p could just be
    # command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c', '--command', action='append', metavar='PROGRAM_TEXT',
                            dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p', '--package', metavar='PACKAGE', dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files', nargs='*', dest='special-opts:files',
                            help="type-check given files or directories")

    # Parse arguments once into a dummy namespace so we can get the
    # filename for the config file.
    dummy = argparse.Namespace()
    parser.parse_args(args, dummy)
    config_file = defaults.CONFIG_FILE
    if dummy.config_file:
        config_file = dummy.config_file
        if not os.path.exists(config_file):
            parser.error("Cannot file config file '%s'" % config_file)

    # Parse config file first, so command line can override.
    options = Options()
    if config_file and os.path.exists(config_file):
        parse_config_file(options, config_file)

    # Parse command line for real, using a split namespace.
    special_opts = argparse.Namespace()
    parser.parse_args(args, SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error("Sorry, --use-python-path is no longer supported.\n"
                     "If you are trying this because your code depends on a library module,\n"
                     "you should really investigate how to obtain stubs for that module.\n"
                     "See https://github.com/python/mypy/issues/1411 for more discussion."
                     )

    # Process deprecated options
    if special_opts.almost_silent:
        print("Warning: --almost-silent has been replaced by "
              "--follow-imports=errors", file=sys.stderr)
        if options.follow_imports == 'normal':
            options.follow_imports = 'errors'
    elif special_opts.silent_imports:
        print("Warning: --silent-imports has been replaced by "
              "--ignore-missing-imports --follow-imports=skip", file=sys.stderr)
        options.ignore_missing_imports = True
        if options.follow_imports == 'normal':
            options.follow_imports = 'skip'
    if special_opts.dirty_stubs:
        print("Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
              "checks the git status of stubs.",
              file=sys.stderr)

    # Check for invalid argument combinations.
    if require_targets:
        code_methods = sum(bool(c) for c in [special_opts.modules,
                                            special_opts.command,
                                            special_opts.package,
                                            special_opts.files])
        if code_methods == 0:
            parser.error("Missing target module, package, files, or command.")
        elif code_methods > 1:
            parser.error("May only specify one of: module, package, files, or command.")

    # Set build flags.
    if options.strict_optional_whitelist is not None:
        # TODO: Deprecate, then kill this flag
        options.strict_optional = True
    if options.strict_optional:
        experiments.STRICT_OPTIONAL = True
    if special_opts.find_occurrences:
        experiments.find_occurrences = special_opts.find_occurrences.split('.')
        if len(experiments.find_occurrences) < 2:
            parser.error("Can only find occurrences of class members.")
        if len(experiments.find_occurrences) != 2:
            parser.error("Can only find occurrences of non-nested class members.")

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it."
                 .format(special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        return [BuildSource(None, None, '\n'.join(special_opts.command))], options
    else:
        targets = []
        for f in special_opts.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'"
                         .format(f))
                targets.extend(sub_targets)
            else:
                mod = os.path.basename(f) if options.scripts_are_modules else None
                targets.append(BuildSource(f, mod, None))
        return targets, options
Example #11
0
File: main.py Project: gnprice/mypy
def process_options() -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    # Make the help output a little less jarring.
    help_factory = lambda prog: argparse.RawDescriptionHelpFormatter(prog=prog, max_help_position=28)
    parser = argparse.ArgumentParser(prog="mypy", epilog=FOOTER, formatter_class=help_factory)

    def parse_version(v):
        m = re.match(r"\A(\d)\.(\d+)\Z", v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError("Invalid python version '{}' (expected format: 'x.y')".format(v))

    parser.add_argument("-v", "--verbose", action="count", help="more verbose messages")
    parser.add_argument(
        "-V", "--version", action="version", version="%(prog)s " + __version__  # type: ignore # see typeshed#124
    )
    parser.add_argument("--python-version", type=parse_version, metavar="x.y", help="use Python x.y")
    parser.add_argument(
        "--py2", dest="python_version", action="store_const", const=defaults.PYTHON2_VERSION, help="use Python 2 mode"
    )
    parser.add_argument("-s", "--silent-imports", action="store_true", help="don't follow imports to .py files")
    parser.add_argument("--silent", action="store_true", help="deprecated name for --silent-imports")
    parser.add_argument(
        "--almost-silent", action="store_true", help="like --silent-imports but reports the imports as errors"
    )
    parser.add_argument(
        "--disallow-untyped-calls",
        action="store_true",
        help="disallow calling functions without type annotations" " from functions with type annotations",
    )
    parser.add_argument(
        "--disallow-untyped-defs",
        action="store_true",
        help="disallow defining functions without type annotations" " or with incomplete type annotations",
    )
    parser.add_argument(
        "--check-untyped-defs",
        action="store_true",
        help="type check the interior of functions without type annotations",
    )
    parser.add_argument(
        "--warn-incomplete-stub",
        action="store_true",
        help="warn if missing type annotation in typeshed, only relevant with" " --check-untyped-defs enabled",
    )
    parser.add_argument("--fast-parser", action="store_true", help="enable experimental fast parser")
    parser.add_argument("-i", "--incremental", action="store_true", help="enable experimental module cache")
    parser.add_argument("--strict-optional", action="store_true", help="enable experimental strict Optional checks")
    parser.add_argument("-f", "--dirty-stubs", action="store_true", help="don't warn if typeshed is out of sync")
    parser.add_argument("--pdb", action="store_true", help="invoke pdb on fatal error")
    parser.add_argument("--use-python-path", action="store_true", help="an anti-pattern")
    parser.add_argument("--stats", action="store_true", help="dump stats")
    parser.add_argument("--inferstats", action="store_true", help="dump type inference stats")
    parser.add_argument("--custom-typing", metavar="MODULE", help="use a custom typing module")

    report_group = parser.add_argument_group(
        title="report generation", description="Generate a report in the specified format."
    )
    report_group.add_argument("--html-report", metavar="DIR")
    report_group.add_argument("--old-html-report", metavar="DIR")
    report_group.add_argument("--xslt-html-report", metavar="DIR")
    report_group.add_argument("--xml-report", metavar="DIR")
    report_group.add_argument("--txt-report", metavar="DIR")
    report_group.add_argument("--xslt-txt-report", metavar="DIR")
    report_group.add_argument("--linecount-report", metavar="DIR")

    code_group = parser.add_argument_group(title="How to specify the code to type check")
    code_group.add_argument(
        "-m", "--module", action="append", dest="modules", help="type-check module; can repeat for more modules"
    )
    # TODO: `mypy -c A -c B` and `mypy -p A -p B` currently silently
    # ignore A (last option wins).  Perhaps -c, -m and -p could just
    # be command-line flags that modify how we interpret self.files?
    code_group.add_argument("-c", "--command", help="type-check program passed in as string")
    code_group.add_argument("-p", "--package", help="type-check all files in a directory")
    code_group.add_argument("files", nargs="*", help="type-check given files or directories")

    args = parser.parse_args()

    # --use-python-path is no longer supported; explain why.
    if args.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )
    # --silent is deprecated; warn about this.
    if args.silent:
        print("Warning: --silent is deprecated; use --silent-imports", file=sys.stderr)

    # Check for invalid argument combinations.
    code_methods = sum(bool(c) for c in [args.modules, args.command, args.package, args.files])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error("May only specify one of: module, package, files, or command.")

    if args.use_python_path and args.python_version and args.python_version[0] == 2:
        parser.error(
            "Python version 2 (or --py2) specified, " "but --use-python-path will search in sys.path of Python 3"
        )

    # Set options.
    options = Options()
    options.dirty_stubs = args.dirty_stubs
    options.python_path = args.use_python_path
    options.pdb = args.pdb
    options.custom_typing_module = args.custom_typing

    # Set build flags.
    if args.python_version is not None:
        options.pyversion = args.python_version

    if args.verbose:
        options.build_flags.extend(args.verbose * [build.VERBOSE])

    if args.stats:
        options.build_flags.append(build.DUMP_TYPE_STATS)

    if args.inferstats:
        options.build_flags.append(build.DUMP_INFER_STATS)

    if args.silent_imports or args.silent:
        options.build_flags.append(build.SILENT_IMPORTS)
    if args.almost_silent:
        options.build_flags.append(build.SILENT_IMPORTS)
        options.build_flags.append(build.ALMOST_SILENT)

    if args.disallow_untyped_calls:
        options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)

    if args.disallow_untyped_defs:
        options.build_flags.append(build.DISALLOW_UNTYPED_DEFS)

    if args.check_untyped_defs:
        options.build_flags.append(build.CHECK_UNTYPED_DEFS)

    if args.warn_incomplete_stub:
        options.build_flags.append(build.WARN_INCOMPLETE_STUB)

    # experimental
    if args.fast_parser:
        options.build_flags.append(build.FAST_PARSER)
    if args.incremental:
        options.build_flags.append(build.INCREMENTAL)
    if args.strict_optional:
        experiments.STRICT_OPTIONAL = True

    # Set reports.
    for flag, val in vars(args).items():
        if flag.endswith("_report") and val is not None:
            report_type = flag[:-7].replace("_", "-")
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if args.modules:
        options.build_flags.append(build.MODULE)
        targets = [BuildSource(None, m, None) for m in args.modules]
        return targets, options
    elif args.package:
        if os.sep in args.package or os.altsep and os.altsep in args.package:
            fail("Package name '{}' cannot have a slash in it.".format(args.package))
        options.build_flags.append(build.MODULE)
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(args.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(args.package))
        return targets, options
    elif args.command:
        options.build_flags.append(build.PROGRAM_TEXT)
        return [BuildSource(None, None, args.command)], options
    else:
        targets = []
        for f in args.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'".format(f))
                targets.extend(sub_targets)
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #12
0
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    # TODO: Rewrite using argparse.
    options = Options()
    help = False
    ver = False
    while args and args[0].startswith('-'):
        if args[0] in ('--verbose', '-v'):
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == '--py2':
            # Use Python 2 mode.
            options.pyversion = defaults.PYTHON2_VERSION
            args = args[1:]
        elif args[0] == '--python-version':
            version_components = args[1].split(".")[0:2]
            if len(version_components) != 2:
                fail("Invalid python version {} (expected format: 'x.y')".
                     format(repr(args[1])))
            if not all(item.isdigit() for item in version_components):
                fail("Found non-digit in python version: {}".format(args[1]))
            options.pyversion = (int(version_components[0]),
                                 int(version_components[1]))
            args = args[2:]
        elif args[0] == '-f' or args[0] == '--dirty-stubs':
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == '-m' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '-m mod'")
            options.build_flags.append(build.MODULE)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == '--package' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '--package dir'")
            options.build_flags.append(build.MODULE)
            lib_path = [os.getcwd()] + build.mypy_path()
            targets = build.find_modules_recursive(args[1], lib_path)
            if not targets:
                fail("Can't find package '{}'".format(args[1]))
            return targets, options
        elif args[0] == '-c' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '-c string'")
            options.build_flags.append(build.PROGRAM_TEXT)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ('-h', '--help'):
            help = True
            args = args[1:]
        elif args[0] == '--stats':
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == '--inferstats':
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == '--custom-typing' and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == '--use-python-path':
            options.python_path = True
            args = args[1:]
        elif args[0] in ('--silent-imports', '--silent', '-s'):
            options.build_flags.append(build.SILENT_IMPORTS)
            args = args[1:]
        elif args[0] == '--pdb':
            options.pdb = True
            args = args[1:]
        elif args[0] == '--implicit-any':
            options.implicit_any = True
            args = args[1:]
        elif args[0] == '--fast-parser':
            options.build_flags.append(build.FAST_PARSER)
            args = args[1:]
        elif args[0] == '--disallow-untyped-calls':
            options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)
            args = args[1:]
        elif args[0] in ('--version', '-V'):
            ver = True
            args = args[1:]
        else:
            usage('Unknown option: {}'.format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage('Missing target file or module')

    if options.python_path and options.pyversion[0] == 2:
        usage('Python version 2 (or --py2) specified, '
              'but --use-python-path will search in sys.path of Python 3')

    if build.FAST_PARSER in options.build_flags and options.pyversion[0] == 2:
        usage('The experimental fast parser is only compatible with Python 3, '
              'but Python 2 specified.')

    targets = []
    for arg in args:
        if arg.endswith(PY_EXTENSIONS):
            targets.append(BuildSource(arg, crawl_up(arg)[1], None))
        elif os.path.isdir(arg):
            targets.extend(expand_dir(arg))
        else:
            targets.append(BuildSource(arg, None, None))
    return targets, options
Example #13
0
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    options = Options()
    help = False
    ver = False
    while args and args[0].startswith('-'):
        if args[0] == '--verbose':
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == '--py2':
            # Use Python 2 mode.
            options.pyversion = defaults.PYTHON2_VERSION
            args = args[1:]
        elif args[0] == '--python-version':
            version_components = args[1].split(".")[0:2]
            if len(version_components) != 2:
                fail("Invalid python version {} (expected format: 'x.y')".format(
                    repr(args[1])))
            if not all(item.isdigit() for item in version_components):
                fail("Found non-digit in python version: {}".format(
                    args[1]))
            options.pyversion = (int(version_components[0]), int(version_components[1]))
            args = args[2:]
        elif args[0] == '-f' or args[0] == '--dirty-stubs':
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == '-m' and args[1:]:
            options.build_flags.append(build.MODULE)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == '--package' and args[1:]:
            options.build_flags.append(build.MODULE)
            return build.find_modules_recursive(args[1], [os.getcwd()]), options
        elif args[0] == '-c' and args[1:]:
            options.build_flags.append(build.PROGRAM_TEXT)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ('-h', '--help'):
            help = True
            args = args[1:]
        elif args[0] == '--stats':
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == '--inferstats':
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == '--custom-typing' and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == '--use-python-path':
            options.python_path = True
            args = args[1:]
        elif args[0] == '--version':
            ver = True
            args = args[1:]
        else:
            usage('Unknown option: {}'.format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage('Missing target file or module')

    if options.python_path and options.pyversion[0] == 2:
        usage('Python version 2 (or --py2) specified, '
              'but --use-python-path will search in sys.path of Python 3')

    #return [BuildSource(arg, arg.replace('/Users/yang/proj/sales/ts/rel/','').replace('/Users/yang/proj/sales/','').replace('.py','').replace('/','.')+'__main__', None) for arg in args], options
    return [BuildSource(arg, arg.replace('/Users/yang/proj/sales/ts/rel/','').replace('/Users/yang/proj/sales/','').replace('.py','').replace('/','.').replace('.__init__',''), None) for arg in args], options
Example #14
0
File: main.py Project: ivuk/mypy
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    # TODO: Rewrite using argparse.
    options = Options()
    help = False
    ver = False
    while args and args[0].startswith('-'):
        if args[0] in ('--verbose', '-v'):
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == '--py2':
            # Use Python 2 mode.
            options.pyversion = defaults.PYTHON2_VERSION
            args = args[1:]
        elif args[0] == '--python-version':
            version_components = args[1].split(".")[0:2]
            if len(version_components) != 2:
                fail("Invalid python version {} (expected format: 'x.y')".format(
                    repr(args[1])))
            if not all(item.isdigit() for item in version_components):
                fail("Found non-digit in python version: {}".format(
                    args[1]))
            options.pyversion = (int(version_components[0]), int(version_components[1]))
            args = args[2:]
        elif args[0] == '-f' or args[0] == '--dirty-stubs':
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == '-m' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '-m mod'")
            options.build_flags.append(build.MODULE)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == '--package' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '--package dir'")
            options.build_flags.append(build.MODULE)
            lib_path = [os.getcwd()] + build.mypy_path()
            targets = build.find_modules_recursive(args[1], lib_path)
            if not targets:
                fail("Can't find package '{}'".format(args[1]))
            return targets, options
        elif args[0] == '-c' and args[1:]:
            if args[2:]:
                fail("No extra argument should appear after '-c string'")
            options.build_flags.append(build.PROGRAM_TEXT)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ('-h', '--help'):
            help = True
            args = args[1:]
        elif args[0] == '--stats':
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == '--inferstats':
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == '--custom-typing' and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == '--use-python-path':
            options.python_path = True
            args = args[1:]
        elif args[0] in ('--silent-imports', '--silent', '-s'):
            options.build_flags.append(build.SILENT_IMPORTS)
            args = args[1:]
        elif args[0] == '--pdb':
            options.pdb = True
            args = args[1:]
        elif args[0] == '--implicit-any':
            options.implicit_any = True
            args = args[1:]
        elif args[0] == '--fast-parser':
            options.build_flags.append(build.FAST_PARSER)
            args = args[1:]
        elif args[0] == '--disallow-untyped-calls':
            options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)
            args = args[1:]
        elif args[0] in ('--version', '-V'):
            ver = True
            args = args[1:]
        else:
            usage('Unknown option: {}'.format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage('Missing target file or module')

    if options.python_path and options.pyversion[0] == 2:
        usage('Python version 2 (or --py2) specified, '
              'but --use-python-path will search in sys.path of Python 3')

    if build.FAST_PARSER in options.build_flags and options.pyversion[0] == 2:
        usage('The experimental fast parser is only compatible with Python 3, '
              'but Python 2 specified.')

    targets = []
    for arg in args:
        if arg.endswith(PY_EXTENSIONS):
            targets.append(BuildSource(arg, crawl_up(arg)[1], None))
        elif os.path.isdir(arg):
            targets.extend(expand_dir(arg))
        else:
            targets.append(BuildSource(arg, None, None))
    return targets, options
Example #15
0
def process_options(args: List[str],
                    require_targets: bool = True
                    ) -> Tuple[List[BuildSource], Options]:
    """Parse command line arguments."""

    parser = argparse.ArgumentParser(prog='mypy', epilog=FOOTER,
                                     fromfile_prefix_chars='@',
                                     formatter_class=AugmentedHelpFormatter)

    strict_flag_names = []  # type: List[str]
    strict_flag_assignments = []  # type: List[Tuple[str, bool]]

    def add_invertible_flag(flag: str,
                            *,
                            inverse: Optional[str] = None,
                            default: bool,
                            dest: Optional[str] = None,
                            help: str,
                            strict_flag: bool = False
                            ) -> None:
        if inverse is None:
            inverse = invert_flag_name(flag)

        if help is not argparse.SUPPRESS:
            help += " (inverse: {})".format(inverse)

        arg = parser.add_argument(flag,  # type: ignore  # incorrect stub for add_argument
                                  action='store_false' if default else 'store_true',
                                  dest=dest,
                                  help=help)
        dest = arg.dest
        arg = parser.add_argument(inverse,  # type: ignore  # incorrect stub for add_argument
                                  action='store_true' if default else 'store_false',
                                  dest=dest,
                                  help=argparse.SUPPRESS)
        if strict_flag:
            assert dest is not None
            strict_flag_names.append(flag)
            strict_flag_assignments.append((dest, not default))

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v', '--verbose', action='count', dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V', '--version', action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version', type=parse_version, metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--platform', action='store', metavar='PLATFORM',
                        help="typecheck special-cased code for the given OS platform "
                        "(defaults to sys.platform).")
    parser.add_argument('-2', '--py2', dest='python_version', action='store_const',
                        const=defaults.PYTHON2_VERSION, help="use Python 2 mode")
    parser.add_argument('--ignore-missing-imports', action='store_true',
                        help="silently ignore imports of missing modules")
    parser.add_argument('--follow-imports', choices=['normal', 'silent', 'skip', 'error'],
                        default='normal', help="how to treat imports (default normal)")
    parser.add_argument('--disallow-any-unimported', default=False, action='store_true',
                        help="disallow Any types resulting from unfollowed imports")
    parser.add_argument('--disallow-any-expr', default=False, action='store_true',
                        help='disallow all expressions that have type Any')
    parser.add_argument('--disallow-any-decorated', default=False, action='store_true',
                        help='disallow functions that have Any in their signature '
                             'after decorator transformation')
    parser.add_argument('--disallow-any-explicit', default=False, action='store_true',
                        help='disallow explicit Any in type positions')
    parser.add_argument('--disallow-any-generics', default=False, action='store_true',
                        help='disallow usage of generic types that do not specify explicit '
                             'type parameters')
    add_invertible_flag('--disallow-untyped-calls', default=False, strict_flag=True,
                        help="disallow calling functions without type annotations"
                        " from functions with type annotations")
    add_invertible_flag('--disallow-untyped-defs', default=False, strict_flag=True,
                        help="disallow defining functions without type annotations"
                        " or with incomplete type annotations")
    add_invertible_flag('--disallow-incomplete-defs', default=False, strict_flag=True,
                        help="disallow defining functions with incomplete type annotations")
    add_invertible_flag('--check-untyped-defs', default=False, strict_flag=True,
                        help="type check the interior of functions without type annotations")
    add_invertible_flag('--disallow-subclassing-any', default=False, strict_flag=True,
                        help="disallow subclassing values of type 'Any' when defining classes")
    add_invertible_flag('--warn-incomplete-stub', default=False,
                        help="warn if missing type annotation in typeshed, only relevant with"
                        " --check-untyped-defs enabled")
    add_invertible_flag('--disallow-untyped-decorators', default=False, strict_flag=True,
                        help="disallow decorating typed functions with untyped decorators")
    add_invertible_flag('--warn-redundant-casts', default=False, strict_flag=True,
                        help="warn about casting an expression to its inferred type")
    add_invertible_flag('--no-warn-no-return', dest='warn_no_return', default=True,
                        help="do not warn about functions that end without returning")
    add_invertible_flag('--warn-return-any', default=False, strict_flag=True,
                        help="warn about returning values of type Any"
                             " from non-Any typed functions")
    add_invertible_flag('--warn-unused-ignores', default=False, strict_flag=True,
                        help="warn about unneeded '# type: ignore' comments")
    add_invertible_flag('--warn-unused-configs', default=False, strict_flag=True,
                        help="warn about unnused '[mypy-<pattern>]' config sections")
    add_invertible_flag('--show-error-context', default=False,
                        dest='show_error_context',
                        help='Precede errors with "note:" messages explaining context')
    add_invertible_flag('--no-implicit-optional', default=False, strict_flag=True,
                        help="don't assume arguments with default values of None are Optional")
    parser.add_argument('-i', '--incremental', action='store_true',
                        help="enable module cache, (inverse: --no-incremental)")
    parser.add_argument('--no-incremental', action='store_false', dest='incremental',
                        help=argparse.SUPPRESS)
    parser.add_argument('--quick-and-dirty', action='store_true',
                        help="use cache even if dependencies out of date "
                        "(implies --incremental)")
    parser.add_argument('--cache-dir', action='store', metavar='DIR',
                        help="store module cache info in the given folder in incremental mode "
                        "(defaults to '{}')".format(defaults.CACHE_DIR))
    parser.add_argument('--skip-version-check', action='store_true',
                        help="allow using cache written by older mypy version")
    add_invertible_flag('--strict-optional', default=False, strict_flag=True,
                        help="enable experimental strict Optional checks")
    parser.add_argument('--strict-optional-whitelist', metavar='GLOB', nargs='*',
                        help="suppress strict Optional errors in all but the provided files "
                        "(experimental -- read documentation before using!).  "
                        "Implies --strict-optional.  Has the undesirable side-effect of "
                        "suppressing other errors in non-whitelisted files.")
    parser.add_argument('--junit-xml', help="write junit.xml to the given file")
    parser.add_argument('--pdb', action='store_true', help="invoke pdb on fatal error")
    parser.add_argument('--show-traceback', '--tb', action='store_true',
                        help="show traceback on fatal error")
    parser.add_argument('--stats', action='store_true', dest='dump_type_stats', help="dump stats")
    parser.add_argument('--inferstats', action='store_true', dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
                        help="use a custom typing module")
    parser.add_argument('--custom-typeshed-dir', metavar='DIR',
                        help="use the custom typeshed in DIR")
    parser.add_argument('--scripts-are-modules', action='store_true',
                        help="Script x becomes module x instead of __main__")
    parser.add_argument('--config-file',
                        help="Configuration file, must have a [mypy] section "
                        "(defaults to {})".format(defaults.CONFIG_FILE))
    add_invertible_flag('--show-column-numbers', default=False,
                        help="Show column numbers in error messages")
    parser.add_argument('--find-occurrences', metavar='CLASS.MEMBER',
                        dest='special-opts:find_occurrences',
                        help="print out all usages of a class member (experimental)")
    strict_help = "Strict mode. Enables the following flags: {}".format(
        ", ".join(strict_flag_names))
    parser.add_argument('--strict', action='store_true', dest='special-opts:strict',
                        help=strict_help)
    parser.add_argument('--shadow-file', nargs=2, metavar=('SOURCE_FILE', 'SHADOW_FILE'),
                        dest='shadow_file',
                        help='Typecheck SHADOW_FILE in place of SOURCE_FILE.')
    # hidden options
    # --debug-cache will disable any cache-related compressions/optimizations,
    # which will make the cache writing process output pretty-printed JSON (which
    # is easier to debug).
    parser.add_argument('--debug-cache', action='store_true', help=argparse.SUPPRESS)
    # --dump-deps will dump all fine-grained dependencies to stdout
    parser.add_argument('--dump-deps', action='store_true', help=argparse.SUPPRESS)
    # --dump-graph will dump the contents of the graph of SCCs and exit.
    parser.add_argument('--dump-graph', action='store_true', help=argparse.SUPPRESS)
    # --semantic-analysis-only does exactly that.
    parser.add_argument('--semantic-analysis-only', action='store_true', help=argparse.SUPPRESS)
    # deprecated options
    parser.add_argument('--disallow-any', dest='special-opts:disallow_any',
                        help=argparse.SUPPRESS)
    add_invertible_flag('--strict-boolean', default=False,
                        help=argparse.SUPPRESS)
    parser.add_argument('-f', '--dirty-stubs', action='store_true',
                        dest='special-opts:dirty_stubs',
                        help=argparse.SUPPRESS)
    parser.add_argument('--use-python-path', action='store_true',
                        dest='special-opts:use_python_path',
                        help=argparse.SUPPRESS)
    parser.add_argument('-s', '--silent-imports', action='store_true',
                        dest='special-opts:silent_imports',
                        help=argparse.SUPPRESS)
    parser.add_argument('--almost-silent', action='store_true',
                        dest='special-opts:almost_silent',
                        help=argparse.SUPPRESS)
    parser.add_argument('--fast-parser', action='store_true', dest='special-opts:fast_parser',
                        help=argparse.SUPPRESS)
    parser.add_argument('--no-fast-parser', action='store_true',
                        dest='special-opts:no_fast_parser',
                        help=argparse.SUPPRESS)

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    for report_type in sorted(reporter_classes):
        report_group.add_argument('--%s-report' % report_type.replace('_', '-'),
                                  metavar='DIR',
                                  dest='special-opts:%s_report' % report_type)

    code_group = parser.add_argument_group(title='How to specify the code to type check')
    code_group.add_argument('-m', '--module', action='append', metavar='MODULE',
                            dest='special-opts:modules',
                            help="type-check module; can repeat for more modules")
    # TODO: `mypy -p A -p B` currently silently ignores A
    # (last option wins).  Perhaps -c, -m and -p could just be
    # command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c', '--command', action='append', metavar='PROGRAM_TEXT',
                            dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p', '--package', metavar='PACKAGE', dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files', nargs='*', dest='special-opts:files',
                            help="type-check given files or directories")

    # Parse arguments once into a dummy namespace so we can get the
    # filename for the config file and know if the user requested all strict options.
    dummy = argparse.Namespace()
    parser.parse_args(args, dummy)
    config_file = dummy.config_file
    if config_file is not None and not os.path.exists(config_file):
        parser.error("Cannot find config file '%s'" % config_file)

    # Parse config file first, so command line can override.
    options = Options()
    parse_config_file(options, config_file)

    # Set strict flags before parsing (if strict mode enabled), so other command
    # line options can override.
    if getattr(dummy, 'special-opts:strict'):
        for dest, value in strict_flag_assignments:
            setattr(options, dest, value)

    # Parse command line for real, using a split namespace.
    special_opts = argparse.Namespace()
    parser.parse_args(args, SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error("Sorry, --use-python-path is no longer supported.\n"
                     "If you are trying this because your code depends on a library module,\n"
                     "you should really investigate how to obtain stubs for that module.\n"
                     "See https://github.com/python/mypy/issues/1411 for more discussion."
                     )

    # Process deprecated options
    if special_opts.disallow_any:
        print("--disallow-any option was split up into multiple flags. "
              "See http://mypy.readthedocs.io/en/latest/command_line.html#disallow-any-flags")
    if options.strict_boolean:
        print("Warning: --strict-boolean is deprecated; "
              "see https://github.com/python/mypy/issues/3195", file=sys.stderr)
    if special_opts.almost_silent:
        print("Warning: --almost-silent has been replaced by "
              "--follow-imports=errors", file=sys.stderr)
        if options.follow_imports == 'normal':
            options.follow_imports = 'errors'
    elif special_opts.silent_imports:
        print("Warning: --silent-imports has been replaced by "
              "--ignore-missing-imports --follow-imports=skip", file=sys.stderr)
        options.ignore_missing_imports = True
        if options.follow_imports == 'normal':
            options.follow_imports = 'skip'
    if special_opts.dirty_stubs:
        print("Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
              "checks the git status of stubs.",
              file=sys.stderr)
    if special_opts.fast_parser:
        print("Warning: --fast-parser is now the default (and only) parser.")
    if special_opts.no_fast_parser:
        print("Warning: --no-fast-parser no longer has any effect.  The fast parser "
              "is now mypy's default and only parser.")

    # Check for invalid argument combinations.
    if require_targets:
        code_methods = sum(bool(c) for c in [special_opts.modules,
                                             special_opts.command,
                                             special_opts.package,
                                             special_opts.files])
        if code_methods == 0:
            parser.error("Missing target module, package, files, or command.")
        elif code_methods > 1:
            parser.error("May only specify one of: module, package, files, or command.")

    # Set build flags.
    if options.strict_optional_whitelist is not None:
        # TODO: Deprecate, then kill this flag
        options.strict_optional = True
    if options.strict_optional:
        experiments.STRICT_OPTIONAL = True
    if special_opts.find_occurrences:
        experiments.find_occurrences = special_opts.find_occurrences.split('.')
        assert experiments.find_occurrences is not None
        if len(experiments.find_occurrences) < 2:
            parser.error("Can only find occurrences of class members.")
        if len(experiments.find_occurrences) != 2:
            parser.error("Can only find occurrences of non-nested class members.")

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Let quick_and_dirty imply incremental.
    if options.quick_and_dirty:
        options.incremental = True

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it."
                 .format(special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        targets = [BuildSource(None, None, '\n'.join(special_opts.command))]
        return targets, options
    else:
        targets = create_source_list(special_opts.files, options)
        return targets, options
Example #16
0
File: main.py Project: willywu/mypy
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    # Make the help output a little less jarring.
    help_factory = (lambda prog:
                    argparse.RawDescriptionHelpFormatter(prog=prog, max_help_position=28))
    parser = argparse.ArgumentParser(prog='mypy', epilog=FOOTER,
                                     formatter_class=help_factory)

    def parse_version(v: str) -> Tuple[int, int]:
        m = re.match(r'\A(\d)\.(\d+)\Z', v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError(
                "Invalid python version '{}' (expected format: 'x.y')".format(v))

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v', '--verbose', action='count', dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V', '--version', action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version', type=parse_version, metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--py2', dest='python_version', action='store_const',
                        const=defaults.PYTHON2_VERSION, help="use Python 2 mode")
    parser.add_argument('-s', '--silent-imports', action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument('--almost-silent', action='store_true',
                        help="like --silent-imports but reports the imports as errors")
    parser.add_argument('--disallow-untyped-calls', action='store_true',
                        help="disallow calling functions without type annotations"
                        " from functions with type annotations")
    parser.add_argument('--disallow-untyped-defs', action='store_true',
                        help="disallow defining functions without type annotations"
                        " or with incomplete type annotations")
    parser.add_argument('--check-untyped-defs', action='store_true',
                        help="type check the interior of functions without type annotations")
    parser.add_argument('--warn-incomplete-stub', action='store_true',
                        help="warn if missing type annotation in typeshed, only relevant with"
                        " --check-untyped-defs enabled")
    parser.add_argument('--warn-redundant-casts', action='store_true',
                        help="warn about casting an expression to its inferred type")
    parser.add_argument('--warn-unused-ignores', action='store_true',
                        help="warn about unneeded '# type: ignore' comments")
    parser.add_argument('--suppress-error-context', action='store_true',
                        dest='suppress_error_context',
                        help="Suppress context notes before errors")
    parser.add_argument('--fast-parser', action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i', '--incremental', action='store_true',
                        help="enable experimental module cache")
    parser.add_argument('--cache-dir', action='store', metavar='DIR',
                        help="store module cache info in the given folder in incremental mode "
                        "(defaults to '{}')".format(defaults.MYPY_CACHE))
    parser.add_argument('--strict-optional', action='store_true',
                        dest='special-opts:strict_optional',
                        help="enable experimental strict Optional checks")
    parser.add_argument('--pdb', action='store_true', help="invoke pdb on fatal error")
    parser.add_argument('--stats', action='store_true', dest='dump_type_stats', help="dump stats")
    parser.add_argument('--inferstats', action='store_true', dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
                        help="use a custom typing module")
    # deprecated options
    parser.add_argument('--silent', action='store_true', dest='special-opts:silent',
                        help=argparse.SUPPRESS)
    parser.add_argument('-f', '--dirty-stubs', action='store_true',
                        dest='special-opts:dirty_stubs',
                        help=argparse.SUPPRESS)
    parser.add_argument('--use-python-path', action='store_true',
                        dest='special-opts:use_python_path',
                        help=argparse.SUPPRESS)

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    report_group.add_argument('--html-report', metavar='DIR',
                              dest='special-opts:html_report')
    report_group.add_argument('--old-html-report', metavar='DIR',
                              dest='special-opts:old_html_report')
    report_group.add_argument('--xslt-html-report', metavar='DIR',
                              dest='special-opts:xslt_html_report')
    report_group.add_argument('--xml-report', metavar='DIR',
                              dest='special-opts:xml_report')
    report_group.add_argument('--txt-report', metavar='DIR',
                              dest='special-opts:txt_report')
    report_group.add_argument('--xslt-txt-report', metavar='DIR',
                              dest='special-opts:xslt_txt_report')
    report_group.add_argument('--linecount-report', metavar='DIR',
                              dest='special-opts:linecount_report')

    code_group = parser.add_argument_group(title='How to specify the code to type check')
    code_group.add_argument('-m', '--module', action='append', metavar='MODULE',
                            dest='special-opts:modules',
                            help="type-check module; can repeat for more modules")
    # TODO: `mypy -c A -c B` and `mypy -p A -p B` currently silently
    # ignore A (last option wins).  Perhaps -c, -m and -p could just
    # be command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c', '--command', metavar='PROGRAM_TEXT', dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p', '--package', metavar='PACKAGE', dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files', nargs='*', dest='special-opts:files',
                            help="type-check given files or directories")

    options = Options()
    special_opts = argparse.Namespace()
    parser.parse_args(args, SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error("Sorry, --use-python-path is no longer supported.\n"
                     "If you are trying this because your code depends on a library module,\n"
                     "you should really investigate how to obtain stubs for that module.\n"
                     "See https://github.com/python/mypy/issues/1411 for more discussion."
                     )

    # warn about deprecated options
    if special_opts.silent:
        print("Warning: --silent is deprecated; use --silent-imports",
              file=sys.stderr)
        options.silent_imports = True
    if special_opts.dirty_stubs:
        print("Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
              "checks the git status of stubs.",
              file=sys.stderr)

    # Check for invalid argument combinations.
    code_methods = sum(bool(c) for c in [special_opts.modules,
                                         special_opts.command,
                                         special_opts.package,
                                         special_opts.files])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error("May only specify one of: module, package, files, or command.")

    # Set build flags.
    if special_opts.strict_optional:
        experiments.STRICT_OPTIONAL = True

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it."
                 .format(special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        return [BuildSource(None, None, special_opts.command)], options
    else:
        targets = []
        for f in special_opts.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'"
                         .format(f))
                targets.extend(sub_targets)
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #17
0
File: main.py Project: o11c/mypy
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    options = Options()
    help = False
    ver = False
    python_executable = None  # type: str
    force_py2 = False

    while args and args[0].startswith('-'):
        if args[0] == '--verbose':
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == '--py2':
            # Use Python 2 mode.
            force_py2 = True
            args = args[1:]
        elif args[0] == '--python-executable':
            if len(args) < 2:
                fail('argument required')
            python_executable = args[1]
            args = args[2:]
        elif args[0] == '-f' or args[0] == '--dirty-stubs':
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == '-m' and args[1:]:
            options.build_flags.append(build.MODULE)
            options.implementation = get_implementation(python_executable, force_py2)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == '-p' and args[1:]:
            options.build_flags.append(build.MODULE)
            options.implementation = get_implementation(python_executable, force_py2)
            return build.find_modules_recursive(args[1], [os.getcwd()]), options
        elif args[0] == '-c' and args[1:]:
            options.build_flags.append(build.PROGRAM_TEXT)
            options.implementation = get_implementation(python_executable, force_py2)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ('-h', '--help'):
            help = True
            args = args[1:]
        elif args[0] == '--stats':
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == '--inferstats':
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == '--custom-typing' and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == '--use-python-path':
            options.python_path = True
            args = args[1:]
        elif args[0] == '--version':
            ver = True
            args = args[1:]
        else:
            usage('Unknown option: {}'.format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage('Missing target file or module')

    if args[1:]:
        usage('Extra argument: {}'.format(args[1]))

    options.implementation = get_implementation(python_executable, force_py2)
    return [BuildSource(args[0], None, None)], options
Example #18
0
def process_options() -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    # Make the help output a little less jarring.
    help_factory = (lambda prog: argparse.RawDescriptionHelpFormatter(
        prog=prog, max_help_position=28))
    parser = argparse.ArgumentParser(prog='mypy',
                                     epilog=FOOTER,
                                     formatter_class=help_factory)

    def parse_version(v):
        m = re.match(r'\A(\d)\.(\d+)\Z', v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError(
                "Invalid python version '{}' (expected format: 'x.y')".format(
                    v))

    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        help="more verbose messages")
    parser.add_argument(
        '-V',
        '--version',
        action='version',  # type: ignore # see typeshed#124
        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version',
                        type=parse_version,
                        metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--py2',
                        dest='python_version',
                        action='store_const',
                        const=defaults.PYTHON2_VERSION,
                        help="use Python 2 mode")
    parser.add_argument('-s',
                        '--silent-imports',
                        action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument('--silent',
                        action='store_true',
                        help="deprecated name for --silent-imports")
    parser.add_argument(
        '--almost-silent',
        action='store_true',
        help="like --silent-imports but reports the imports as errors")
    parser.add_argument(
        '--disallow-untyped-calls',
        action='store_true',
        help="disallow calling functions without type annotations"
        " from functions with type annotations")
    parser.add_argument(
        '--disallow-untyped-defs',
        action='store_true',
        help="disallow defining functions without type annotations"
        " or with incomplete type annotations")
    parser.add_argument(
        '--check-untyped-defs',
        action='store_true',
        help="type check the interior of functions without type annotations")
    parser.add_argument('--fast-parser',
                        action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i',
                        '--incremental',
                        action='store_true',
                        help="enable experimental module cache")
    parser.add_argument('-f',
                        '--dirty-stubs',
                        action='store_true',
                        help="don't warn if typeshed is out of sync")
    parser.add_argument('--pdb',
                        action='store_true',
                        help="invoke pdb on fatal error")
    parser.add_argument(
        '--use-python-path',
        action='store_true',
        help="search for modules in sys.path of running Python")
    parser.add_argument('--stats', action='store_true', help="dump stats")
    parser.add_argument('--inferstats',
                        action='store_true',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing',
                        metavar='MODULE',
                        help="use a custom typing module")

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    report_group.add_argument('--html-report', metavar='DIR')
    report_group.add_argument('--old-html-report', metavar='DIR')
    report_group.add_argument('--xslt-html-report', metavar='DIR')
    report_group.add_argument('--xml-report', metavar='DIR')
    report_group.add_argument('--txt-report', metavar='DIR')
    report_group.add_argument('--xslt-txt-report', metavar='DIR')
    report_group.add_argument('--linecount-report', metavar='DIR')

    code_group = parser.add_argument_group(
        title='How to specify the code to type check')
    code_group.add_argument(
        '-m',
        '--module',
        action='append',
        dest='modules',
        help="type-check module; can repeat for more modules")
    code_group.add_argument('-c',
                            '--command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p',
                            '--package',
                            help="type-check all files in a directory")
    code_group.add_argument('files',
                            nargs='*',
                            help="type-check given files or directories")

    args = parser.parse_args()

    # Check for invalid argument combinations.
    code_methods = sum(
        bool(c)
        for c in [args.modules, args.command, args.package, args.files])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error(
            "May only specify one of: module, package, files, or command.")

    if args.use_python_path and args.python_version and args.python_version[
            0] == 2:
        parser.error(
            'Python version 2 (or --py2) specified, '
            'but --use-python-path will search in sys.path of Python 3')

    if args.fast_parser and args.python_version and args.python_version[0] == 2:
        parser.error(
            'The experimental fast parser is only compatible with Python 3, '
            'but Python 2 specified.')

    # Set options.
    options = Options()
    options.dirty_stubs = args.dirty_stubs
    options.python_path = args.use_python_path
    options.pdb = args.pdb
    options.custom_typing_module = args.custom_typing

    # Set build flags.
    if args.python_version is not None:
        options.pyversion = args.python_version

    if args.verbose:
        options.build_flags.extend(args.verbose * [build.VERBOSE])

    if args.stats:
        options.build_flags.append(build.DUMP_TYPE_STATS)

    if args.inferstats:
        options.build_flags.append(build.DUMP_INFER_STATS)

    if args.silent_imports or args.silent:
        options.build_flags.append(build.SILENT_IMPORTS)
    if args.almost_silent:
        options.build_flags.append(build.SILENT_IMPORTS)
        options.build_flags.append(build.ALMOST_SILENT)

    if args.disallow_untyped_calls:
        options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)

    if args.disallow_untyped_defs:
        options.build_flags.append(build.DISALLOW_UNTYPED_DEFS)

    if args.check_untyped_defs:
        options.build_flags.append(build.CHECK_UNTYPED_DEFS)

    # experimental
    if args.fast_parser:
        options.build_flags.append(build.FAST_PARSER)
    if args.incremental:
        options.build_flags.append(build.INCREMENTAL)

    # Set reports.
    for flag, val in vars(args).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if args.modules:
        options.build_flags.append(build.MODULE)
        targets = [BuildSource(None, m, None) for m in args.modules]
        return targets, options
    elif args.package:
        options.build_flags.append(build.MODULE)
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(args.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(args.package))
        return targets, options
    elif args.command:
        options.build_flags.append(build.PROGRAM_TEXT)
        return [BuildSource(None, None, args.command)], options
    else:
        targets = []
        for f in args.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                targets.extend(expand_dir(f))
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #19
0
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    # Make the help output a little less jarring.
    help_factory = (lambda prog: argparse.RawDescriptionHelpFormatter(
        prog=prog, max_help_position=28))
    parser = argparse.ArgumentParser(prog='mypy',
                                     epilog=FOOTER,
                                     formatter_class=help_factory)

    def parse_version(v: str) -> Tuple[int, int]:
        m = re.match(r'\A(\d)\.(\d+)\Z', v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError(
                "Invalid python version '{}' (expected format: 'x.y')".format(
                    v))

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        dest='verbosity',
                        help="more verbose messages")
    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version',
                        type=parse_version,
                        metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--py2',
                        dest='python_version',
                        action='store_const',
                        const=defaults.PYTHON2_VERSION,
                        help="use Python 2 mode")
    parser.add_argument('-s',
                        '--silent-imports',
                        action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument('--silent',
                        action='store_true',
                        dest='special-opts:silent',
                        help="deprecated name for --silent-imports")
    parser.add_argument(
        '--almost-silent',
        action='store_true',
        help="like --silent-imports but reports the imports as errors")
    parser.add_argument(
        '--disallow-untyped-calls',
        action='store_true',
        help="disallow calling functions without type annotations"
        " from functions with type annotations")
    parser.add_argument(
        '--disallow-untyped-defs',
        action='store_true',
        help="disallow defining functions without type annotations"
        " or with incomplete type annotations")
    parser.add_argument(
        '--check-untyped-defs',
        action='store_true',
        help="type check the interior of functions without type annotations")
    parser.add_argument(
        '--warn-incomplete-stub',
        action='store_true',
        help="warn if missing type annotation in typeshed, only relevant with"
        " --check-untyped-defs enabled")
    parser.add_argument(
        '--warn-redundant-casts',
        action='store_true',
        help="warn about casting an expression to its inferred type")
    parser.add_argument('--warn-unused-ignores',
                        action='store_true',
                        help="warn about unneeded '# type: ignore' comments")
    parser.add_argument('--fast-parser',
                        action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i',
                        '--incremental',
                        action='store_true',
                        help="enable experimental module cache")
    parser.add_argument(
        '--cache-dir',
        action='store',
        metavar='DIR',
        help="store module cache info in the given folder in incremental mode "
        "(defaults to '{}')".format(defaults.MYPY_CACHE))
    parser.add_argument('--strict-optional',
                        action='store_true',
                        dest='special-opts:strict_optional',
                        help="enable experimental strict Optional checks")
    parser.add_argument('-f',
                        '--dirty-stubs',
                        action='store_true',
                        help="don't warn if typeshed is out of sync")
    parser.add_argument('--pdb',
                        action='store_true',
                        help="invoke pdb on fatal error")
    parser.add_argument('--use-python-path',
                        action='store_true',
                        dest='special-opts:use_python_path',
                        help="an anti-pattern")
    parser.add_argument('--stats',
                        action='store_true',
                        dest='dump_type_stats',
                        help="dump stats")
    parser.add_argument('--inferstats',
                        action='store_true',
                        dest='dump_inference_stats',
                        help="dump type inference stats")
    parser.add_argument('--custom-typing',
                        metavar='MODULE',
                        dest='custom_typing_module',
                        help="use a custom typing module")

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    report_group.add_argument('--html-report',
                              metavar='DIR',
                              dest='special-opts:html_report')
    report_group.add_argument('--old-html-report',
                              metavar='DIR',
                              dest='special-opts:old_html_report')
    report_group.add_argument('--xslt-html-report',
                              metavar='DIR',
                              dest='special-opts:xslt_html_report')
    report_group.add_argument('--xml-report',
                              metavar='DIR',
                              dest='special-opts:xml_report')
    report_group.add_argument('--txt-report',
                              metavar='DIR',
                              dest='special-opts:txt_report')
    report_group.add_argument('--xslt-txt-report',
                              metavar='DIR',
                              dest='special-opts:xslt_txt_report')
    report_group.add_argument('--linecount-report',
                              metavar='DIR',
                              dest='special-opts:linecount_report')

    code_group = parser.add_argument_group(
        title='How to specify the code to type check')
    code_group.add_argument(
        '-m',
        '--module',
        action='append',
        metavar='MODULE',
        dest='special-opts:modules',
        help="type-check module; can repeat for more modules")
    # TODO: `mypy -c A -c B` and `mypy -p A -p B` currently silently
    # ignore A (last option wins).  Perhaps -c, -m and -p could just
    # be command-line flags that modify how we interpret self.files?
    code_group.add_argument('-c',
                            '--command',
                            metavar='PROGRAM_TEXT',
                            dest='special-opts:command',
                            help="type-check program passed in as string")
    code_group.add_argument('-p',
                            '--package',
                            metavar='PACKAGE',
                            dest='special-opts:package',
                            help="type-check all files in a directory")
    code_group.add_argument(metavar='files',
                            nargs='*',
                            dest='special-opts:files',
                            help="type-check given files or directories")

    options = Options()
    special_opts = argparse.Namespace()
    parser.parse_args(args,
                      SplitNamespace(options, special_opts, 'special-opts:'))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )

    # --silent is deprecated; warn about this.
    if special_opts.silent:
        print("Warning: --silent is deprecated; use --silent-imports",
              file=sys.stderr)
        options.silent_imports = True

    # Check for invalid argument combinations.
    code_methods = sum(
        bool(c) for c in [
            special_opts.modules, special_opts.command, special_opts.package,
            special_opts.files
        ])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error(
            "May only specify one of: module, package, files, or command.")

    # Set build flags.
    if special_opts.strict_optional:
        experiments.STRICT_OPTIONAL = True

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it.".format(
                special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        return [BuildSource(None, None, special_opts.command)], options
    else:
        targets = []
        for f in special_opts.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'".format(
                        f))
                targets.extend(sub_targets)
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #20
0
File: main.py Project: jayvdb/mypy
def process_options() -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """

    parser = argparse.ArgumentParser(prog='mypy', epilog=FOOTER,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)

    def parse_version(v):
        m = re.match(r'\A(\d)\.(\d+)\Z', v)
        if m:
            return int(m.group(1)), int(m.group(2))
        else:
            raise argparse.ArgumentTypeError(
                "Invalid python version '{}' (expected format: 'x.y')".format(v))

    parser.add_argument('-v', '--verbose', action='count', help="more verbose messages")
    parser.add_argument('-V', '--version', action='version',  # type: ignore # see typeshed#124
                        version='%(prog)s ' + __version__)
    parser.add_argument('--python-version', type=parse_version, metavar='x.y',
                        help='use Python x.y')
    parser.add_argument('--py2', dest='python_version', action='store_const',
                        const=defaults.PYTHON2_VERSION, help="use Python 2 mode")
    parser.add_argument('-s', '--silent-imports', '--silent', action='store_true',
                        help="don't follow imports to .py files")
    parser.add_argument('--disallow-untyped-calls', action='store_true',
                        help="disallow calling functions without type annotations"
                        " from functions with type annotations")
    parser.add_argument('--disallow-untyped-defs', action='store_true',
                        help="disallow defining functions without type annotations"
                        " or with incomplete type annotations")
    parser.add_argument('--check-untyped-defs', action='store_true',
                        help="type check the interior of functions without type annotations")
    parser.add_argument('--fast-parser', action='store_true',
                        help="enable experimental fast parser")
    parser.add_argument('-i', '--incremental', action='store_true',
                        help="enable experimental module cache")
    parser.add_argument('-f', '--dirty-stubs', action='store_true',
                        help="don't warn if typeshed is out of sync")
    parser.add_argument('--pdb', action='store_true', help="invoke pdb on fatal error")
    parser.add_argument('--use-python-path', action='store_true',
                        help="search for modules in sys.path of running Python")
    parser.add_argument('--stats', action='store_true', help="dump stats")
    parser.add_argument('--inferstats', action='store_true', help="dump type inference stats")
    parser.add_argument('--custom-typing', metavar='MODULE', help="use a custom typing module")

    report_group = parser.add_argument_group(
        title='report generation',
        description='Generate a report in the specified format.')
    report_group.add_argument('--html-report', metavar='DIR')
    report_group.add_argument('--old-html-report', metavar='DIR')
    report_group.add_argument('--xslt-html-report', metavar='DIR')
    report_group.add_argument('--xml-report', metavar='DIR')
    report_group.add_argument('--txt-report', metavar='DIR')
    report_group.add_argument('--xslt-txt-report', metavar='DIR')
    report_group.add_argument('--linecount-report', metavar='DIR')

    code_group = parser.add_argument_group(title='How to specify the code to type check')
    code_group.add_argument('-m', '--module', action='append', dest='modules',
                            help="type-check module; can repeat for more modules")
    code_group.add_argument('-c', '--command', help="type-check program passed in as string")
    code_group.add_argument('-p', '--package', help="type-check all files in a directory")
    code_group.add_argument('files', nargs='*', help="type-check given files or directories")

    args = parser.parse_args()

    # Check for invalid argument combinations.
    code_methods = sum(bool(c) for c in [args.modules, args.command, args.package, args.files])
    if code_methods == 0:
        parser.error("Missing target module, package, files, or command.")
    elif code_methods > 1:
        parser.error("May only specify one of: module, package, files, or command.")

    if args.use_python_path and args.python_version and args.python_version[0] == 2:
        parser.error('Python version 2 (or --py2) specified, '
                     'but --use-python-path will search in sys.path of Python 3')

    if args.fast_parser and args.python_version and args.python_version[0] == 2:
        parser.error('The experimental fast parser is only compatible with Python 3, '
                     'but Python 2 specified.')

    # Set options.
    options = Options()
    options.dirty_stubs = args.dirty_stubs
    options.python_path = args.use_python_path
    options.pdb = args.pdb
    options.custom_typing_module = args.custom_typing

    # Set build flags.
    if args.python_version is not None:
        options.pyversion = args.python_version

    if args.verbose:
        options.build_flags.extend(args.verbose * [build.VERBOSE])

    if args.stats:
        options.build_flags.append(build.DUMP_TYPE_STATS)

    if args.inferstats:
        options.build_flags.append(build.DUMP_INFER_STATS)

    if args.silent_imports:
        options.build_flags.append(build.SILENT_IMPORTS)

    if args.disallow_untyped_calls:
        options.build_flags.append(build.DISALLOW_UNTYPED_CALLS)

    if args.disallow_untyped_defs:
        options.build_flags.append(build.DISALLOW_UNTYPED_DEFS)

    if args.check_untyped_defs:
        options.build_flags.append(build.CHECK_UNTYPED_DEFS)

    # experimental
    if args.fast_parser:
        options.build_flags.append(build.FAST_PARSER)
    if args.incremental:
        options.build_flags.append(build.INCREMENTAL)

    # Set reports.
    for flag, val in vars(args).items():
        if flag.endswith('_report') and val is not None:
            report_type = flag[:-7].replace('_', '-')
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if args.modules:
        options.build_flags.append(build.MODULE)
        targets = [BuildSource(None, m, None) for m in args.modules]
        return targets, options
    elif args.package:
        options.build_flags.append(build.MODULE)
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(args.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(args.package))
        return targets, options
    elif args.command:
        options.build_flags.append(build.PROGRAM_TEXT)
        return [BuildSource(None, None, args.command)], options
    else:
        targets = []
        for f in args.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                targets.extend(expand_dir(f))
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options
Example #21
0
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
    """Process command line arguments.

    Return (mypy program path (or None),
            module to run as script (or None),
            parsed flags)
    """
    options = Options()
    help = False
    ver = False
    while args and args[0].startswith('-'):
        if args[0] == '--verbose':
            options.build_flags.append(build.VERBOSE)
            args = args[1:]
        elif args[0] == '--py2':
            # Use Python 2 mode.
            options.pyversion = defaults.PYTHON2_VERSION
            args = args[1:]
        elif args[0] == '--python-version':
            version_components = args[1].split(".")[0:2]
            if len(version_components) != 2:
                fail("Invalid python version {} (expected format: 'x.y')".
                     format(repr(args[1])))
            if not all(item.isdigit() for item in version_components):
                fail("Found non-digit in python version: {}".format(args[1]))
            options.pyversion = (int(version_components[0]),
                                 int(version_components[1]))
            args = args[2:]
        elif args[0] == '-f' or args[0] == '--dirty-stubs':
            options.dirty_stubs = True
            args = args[1:]
        elif args[0] == '-m' and args[1:]:
            options.build_flags.append(build.MODULE)
            return [BuildSource(None, args[1], None)], options
        elif args[0] == '--package' and args[1:]:
            options.build_flags.append(build.MODULE)
            lib_path = [os.getcwd()] + build.mypy_path()
            targets = build.find_modules_recursive(args[1], lib_path)
            if not targets:
                fail("Can't find package '{}'".format(args[1]))
            return targets, options
        elif args[0] == '-c' and args[1:]:
            options.build_flags.append(build.PROGRAM_TEXT)
            return [BuildSource(None, None, args[1])], options
        elif args[0] in ('-h', '--help'):
            help = True
            args = args[1:]
        elif args[0] == '--stats':
            options.build_flags.append(build.DUMP_TYPE_STATS)
            args = args[1:]
        elif args[0] == '--inferstats':
            options.build_flags.append(build.DUMP_INFER_STATS)
            args = args[1:]
        elif args[0] == '--custom-typing' and args[1:]:
            options.custom_typing_module = args[1]
            args = args[2:]
        elif is_report(args[0]) and args[1:]:
            report_type = args[0][2:-7]
            report_dir = args[1]
            options.report_dirs[report_type] = report_dir
            args = args[2:]
        elif args[0] == '--use-python-path':
            options.python_path = True
            args = args[1:]
        elif args[0] == '--version':
            ver = True
            args = args[1:]
        else:
            usage('Unknown option: {}'.format(args[0]))

    if help:
        usage()

    if ver:
        version()

    if not args:
        usage('Missing target file or module')

    if args[1:]:
        usage('Extra argument: {}'.format(args[1]))

    if options.python_path and options.pyversion[0] == 2:
        usage('Python version 2 (or --py2) specified, '
              'but --use-python-path will search in sys.path of Python 3')

    return [BuildSource(args[0], None, None)], options
Example #22
0
def process_options(args: List[str], require_targets: bool = True) -> Tuple[List[BuildSource], Options]:
    """Parse command line arguments."""

    # Make the help output a little less jarring.
    help_factory = lambda prog: argparse.RawDescriptionHelpFormatter(prog=prog, max_help_position=28)
    parser = argparse.ArgumentParser(prog="mypy", epilog=FOOTER, formatter_class=help_factory)

    # Unless otherwise specified, arguments will be parsed directly onto an
    # Options object.  Options that require further processing should have
    # their `dest` prefixed with `special-opts:`, which will cause them to be
    # parsed into the separate special_opts namespace object.
    parser.add_argument("-v", "--verbose", action="count", dest="verbosity", help="more verbose messages")
    parser.add_argument("-V", "--version", action="version", version="%(prog)s " + __version__)
    parser.add_argument("--python-version", type=parse_version, metavar="x.y", help="use Python x.y")
    parser.add_argument(
        "--platform",
        action="store",
        metavar="PLATFORM",
        help="typecheck special-cased code for the given OS platform " "(defaults to sys.platform).",
    )
    parser.add_argument(
        "-2",
        "--py2",
        dest="python_version",
        action="store_const",
        const=defaults.PYTHON2_VERSION,
        help="use Python 2 mode",
    )
    parser.add_argument("-s", "--silent-imports", action="store_true", help="don't follow imports to .py files")
    parser.add_argument(
        "--almost-silent", action="store_true", help="like --silent-imports but reports the imports as errors"
    )
    parser.add_argument(
        "--disallow-untyped-calls",
        action="store_true",
        help="disallow calling functions without type annotations" " from functions with type annotations",
    )
    parser.add_argument(
        "--disallow-untyped-defs",
        action="store_true",
        help="disallow defining functions without type annotations" " or with incomplete type annotations",
    )
    parser.add_argument(
        "--check-untyped-defs",
        action="store_true",
        help="type check the interior of functions without type annotations",
    )
    parser.add_argument(
        "--disallow-subclassing-any",
        action="store_true",
        help="disallow subclassing values of type 'Any' when defining classes",
    )
    parser.add_argument(
        "--warn-incomplete-stub",
        action="store_true",
        help="warn if missing type annotation in typeshed, only relevant with" " --check-untyped-defs enabled",
    )
    parser.add_argument(
        "--warn-redundant-casts", action="store_true", help="warn about casting an expression to its inferred type"
    )
    parser.add_argument(
        "--warn-unused-ignores", action="store_true", help="warn about unneeded '# type: ignore' comments"
    )
    parser.add_argument(
        "--suppress-error-context",
        action="store_true",
        dest="suppress_error_context",
        help="Suppress context notes before errors",
    )
    parser.add_argument("--fast-parser", action="store_true", help="enable experimental fast parser")
    parser.add_argument("-i", "--incremental", action="store_true", help="enable experimental module cache")
    parser.add_argument(
        "--cache-dir",
        action="store",
        metavar="DIR",
        help="store module cache info in the given folder in incremental mode "
        "(defaults to '{}')".format(defaults.MYPY_CACHE),
    )
    parser.add_argument(
        "--strict-optional",
        action="store_true",
        dest="special-opts:strict_optional",
        help="enable experimental strict Optional checks",
    )
    parser.add_argument(
        "--strict-optional-whitelist",
        metavar="GLOB",
        nargs="*",
        help="suppress strict Optional errors in all but the provided files "
        "(experimental -- read documentation before using!).  "
        "Implies --strict-optional.  Has the undesirable side-effect of "
        "suppressing other errors in non-whitelisted files.",
    )
    parser.add_argument("--pdb", action="store_true", help="invoke pdb on fatal error")
    parser.add_argument("--show-traceback", "--tb", action="store_true", help="show traceback on fatal error")
    parser.add_argument("--stats", action="store_true", dest="dump_type_stats", help="dump stats")
    parser.add_argument(
        "--inferstats", action="store_true", dest="dump_inference_stats", help="dump type inference stats"
    )
    parser.add_argument(
        "--custom-typing", metavar="MODULE", dest="custom_typing_module", help="use a custom typing module"
    )
    # hidden options
    # --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
    # Useful for tools to make transformations to a file to get more
    # information from a mypy run without having to change the file in-place
    # (e.g. by adding a call to reveal_type).
    parser.add_argument("--shadow-file", metavar="PATH", nargs=2, dest="shadow_file", help=argparse.SUPPRESS)
    # --debug-cache will disable any cache-related compressions/optimizations,
    # which will make the cache writing process output pretty-printed JSON (which
    # is easier to debug).
    parser.add_argument("--debug-cache", action="store_true", help=argparse.SUPPRESS)
    # deprecated options
    parser.add_argument("--silent", action="store_true", dest="special-opts:silent", help=argparse.SUPPRESS)
    parser.add_argument(
        "-f", "--dirty-stubs", action="store_true", dest="special-opts:dirty_stubs", help=argparse.SUPPRESS
    )
    parser.add_argument(
        "--use-python-path", action="store_true", dest="special-opts:use_python_path", help=argparse.SUPPRESS
    )

    report_group = parser.add_argument_group(
        title="report generation", description="Generate a report in the specified format."
    )
    report_group.add_argument("--html-report", metavar="DIR", dest="special-opts:html_report")
    report_group.add_argument("--old-html-report", metavar="DIR", dest="special-opts:old_html_report")
    report_group.add_argument("--xslt-html-report", metavar="DIR", dest="special-opts:xslt_html_report")
    report_group.add_argument("--xml-report", metavar="DIR", dest="special-opts:xml_report")
    report_group.add_argument("--txt-report", metavar="DIR", dest="special-opts:txt_report")
    report_group.add_argument("--xslt-txt-report", metavar="DIR", dest="special-opts:xslt_txt_report")
    report_group.add_argument("--linecount-report", metavar="DIR", dest="special-opts:linecount_report")
    report_group.add_argument("--linecoverage-report", metavar="DIR", dest="special-opts:linecoverage_report")

    code_group = parser.add_argument_group(title="How to specify the code to type check")
    code_group.add_argument(
        "-m",
        "--module",
        action="append",
        metavar="MODULE",
        dest="special-opts:modules",
        help="type-check module; can repeat for more modules",
    )
    # TODO: `mypy -c A -c B` and `mypy -p A -p B` currently silently
    # ignore A (last option wins).  Perhaps -c, -m and -p could just
    # be command-line flags that modify how we interpret self.files?
    code_group.add_argument(
        "-c",
        "--command",
        action="append",
        metavar="PROGRAM_TEXT",
        dest="special-opts:command",
        help="type-check program passed in as string",
    )
    code_group.add_argument(
        "-p", "--package", metavar="PACKAGE", dest="special-opts:package", help="type-check all files in a directory"
    )
    code_group.add_argument(
        metavar="files", nargs="*", dest="special-opts:files", help="type-check given files or directories"
    )

    options = Options()
    special_opts = argparse.Namespace()
    parser.parse_args(args, SplitNamespace(options, special_opts, "special-opts:"))

    # --use-python-path is no longer supported; explain why.
    if special_opts.use_python_path:
        parser.error(
            "Sorry, --use-python-path is no longer supported.\n"
            "If you are trying this because your code depends on a library module,\n"
            "you should really investigate how to obtain stubs for that module.\n"
            "See https://github.com/python/mypy/issues/1411 for more discussion."
        )

    # warn about deprecated options
    if special_opts.silent:
        print("Warning: --silent is deprecated; use --silent-imports", file=sys.stderr)
        options.silent_imports = True
    if special_opts.dirty_stubs:
        print(
            "Warning: -f/--dirty-stubs is deprecated and no longer necessary. Mypy no longer "
            "checks the git status of stubs.",
            file=sys.stderr,
        )

    # Check for invalid argument combinations.
    if require_targets:
        code_methods = sum(
            bool(c) for c in [special_opts.modules, special_opts.command, special_opts.package, special_opts.files]
        )
        if code_methods == 0:
            parser.error("Missing target module, package, files, or command.")
        elif code_methods > 1:
            parser.error("May only specify one of: module, package, files, or command.")

    # Set build flags.
    if special_opts.strict_optional or options.strict_optional_whitelist is not None:
        experiments.STRICT_OPTIONAL = True

    # Set reports.
    for flag, val in vars(special_opts).items():
        if flag.endswith("_report") and val is not None:
            report_type = flag[:-7].replace("_", "-")
            report_dir = val
            options.report_dirs[report_type] = report_dir

    # Set target.
    if special_opts.modules:
        options.build_type = BuildType.MODULE
        targets = [BuildSource(None, m, None) for m in special_opts.modules]
        return targets, options
    elif special_opts.package:
        if os.sep in special_opts.package or os.altsep and os.altsep in special_opts.package:
            fail("Package name '{}' cannot have a slash in it.".format(special_opts.package))
        options.build_type = BuildType.MODULE
        lib_path = [os.getcwd()] + build.mypy_path()
        targets = build.find_modules_recursive(special_opts.package, lib_path)
        if not targets:
            fail("Can't find package '{}'".format(special_opts.package))
        return targets, options
    elif special_opts.command:
        options.build_type = BuildType.PROGRAM_TEXT
        return [BuildSource(None, None, "\n".join(special_opts.command))], options
    else:
        targets = []
        for f in special_opts.files:
            if f.endswith(PY_EXTENSIONS):
                targets.append(BuildSource(f, crawl_up(f)[1], None))
            elif os.path.isdir(f):
                sub_targets = expand_dir(f)
                if not sub_targets:
                    fail("There are no .py[i] files in directory '{}'".format(f))
                targets.extend(sub_targets)
            else:
                targets.append(BuildSource(f, None, None))
        return targets, options