diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 050222d1f7ea..961823ed9470 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -17,13 +17,13 @@ flag (or its long form ``--help``):: [--strict-optional] [--strict-optional-whitelist [GLOB [GLOB ...]]] [--pdb] [--show-traceback] [--stats] [--inferstats] - [--custom-typing MODULE] [--scripts-are-modules] - [--config-file CONFIG_FILE] [--show-column-numbers] - [--html-report DIR] [--linecount-report DIR] - [--linecoverage-report DIR] [--memory-xml-report DIR] - [--old-html-report DIR] [--txt-report DIR] [--xml-report DIR] - [--xslt-html-report DIR] [--xslt-txt-report DIR] - [-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE] + [--custom-typing MODULE] [--custom-typeshed-dir DIR] + [--scripts-are-modules] [--config-file CONFIG_FILE] + [--show-column-numbers] [--html-report DIR] + [--linecount-report DIR] [--linecoverage-report DIR] + [--memory-xml-report DIR] [--old-html-report DIR] + [--txt-report DIR] [--xml-report DIR] [--xslt-html-report DIR] + [--xslt-txt-report DIR] [-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE] [files [files ...]] (etc., too long to show everything here) @@ -326,6 +326,12 @@ Here are some more useful flags: package, the behavior enabled by this flag is often more convenient.) +- ``--custom-typeshed-dir DIR`` specifies the directory where mypy looks for + typeshed stubs, instead of the typeshed that ships with mypy. This is + primarily intended to make it easier to test typeshed changes before + submitting them upstream, but also allows you to use a forked version of + typeshed. + .. _config-file-flag: - ``--config-file CONFIG_FILE`` causes configuration settings to be diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index ae8dc48cbac8..58264532aec9 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -47,6 +47,10 @@ The following global flags may only be set in the global section alternative module which is to be considered equivalent to the ``typing`` module. +- ``custom_typeshed_dir`` (string) specifies the name of an + alternative directory which is used to look for stubs instead of the + default ``typeshed`` directory. + - ``warn_incomplete_stub`` (Boolean, default False) warns for missing type annotation in typeshed. This is only relevant in combination with ``check_untyped_defs``. diff --git a/mypy/build.py b/mypy/build.py index 289a22b499bb..c29d48e96dc7 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -135,7 +135,9 @@ def build(sources: List[BuildSource], find_module_clear_caches() # Determine the default module search path. - lib_path = default_lib_path(data_dir, options.python_version) + lib_path = default_lib_path(data_dir, + options.python_version, + custom_typeshed_dir=options.custom_typeshed_dir) if options.use_builtins_fixtures: # Use stub builtins (to speed up test cases and to make them easier to @@ -252,15 +254,20 @@ def mypy_path() -> List[str]: return path_env.split(os.pathsep) -def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]: +def default_lib_path(data_dir: str, + pyversion: Tuple[int, int], + custom_typeshed_dir: Optional[str]) -> List[str]: """Return default standard library search paths.""" # IDEA: Make this more portable. path = [] # type: List[str] - auto = os.path.join(data_dir, 'stubs-auto') - if os.path.isdir(auto): - data_dir = auto - + if custom_typeshed_dir: + typeshed_dir = custom_typeshed_dir + else: + auto = os.path.join(data_dir, 'stubs-auto') + if os.path.isdir(auto): + data_dir = auto + typeshed_dir = os.path.join(data_dir, "typeshed") # We allow a module for e.g. version 3.5 to be in 3.4/. The assumption # is that a module added with 3.4 will still be present in Python 3.5. versions = ["%d.%d" % (pyversion[0], minor) @@ -269,7 +276,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]: # (Note that 3.1 and 3.0 aren't really supported, but we don't care.) for v in versions + [str(pyversion[0]), '2and3']: for lib_type in ['stdlib', 'third_party']: - stubdir = os.path.join(data_dir, 'typeshed', lib_type, v) + stubdir = os.path.join(typeshed_dir, lib_type, v) if os.path.isdir(stubdir): path.append(stubdir) diff --git a/mypy/main.py b/mypy/main.py index a8200defce8f..3acb16134006 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -198,6 +198,8 @@ def process_options(args: List[str], 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', @@ -451,6 +453,7 @@ def get_init_file(dir: str) -> Optional[str]: 'python_version': lambda s: tuple(map(int, s.split('.'))), 'strict_optional_whitelist': lambda s: s.split(), 'custom_typing_module': str, + 'custom_typeshed_dir': str, 'junit_xml': str, } diff --git a/mypy/options.py b/mypy/options.py index c78a775f1b4a..68be56163731 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -34,7 +34,8 @@ def __init__(self) -> None: self.build_type = BuildType.STANDARD self.python_version = defaults.PYTHON3_VERSION self.platform = sys.platform - self.custom_typing_module = None # type: str + self.custom_typing_module = None # type: Optional[str] + self.custom_typeshed_dir = None # type: Optional[str] self.report_dirs = {} # type: Dict[str, str] self.silent_imports = False self.almost_silent = False