From 5afdb9d30ca1d789768cab0d7dc9838e99ce4826 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Thu, 26 May 2016 16:47:33 -0700 Subject: [PATCH 1/4] Allow user to specify custom typeshed directory --- docs/source/command_line.rst | 20 +++++++++++++------- mypy/build.py | 19 +++++++++++++++---- mypy/main.py | 2 ++ mypy/options.py | 3 ++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 050222d1f7ea8..961823ed94708 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/mypy/build.py b/mypy/build.py index 679c331f0ddba..3bded932fa7f8 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -127,12 +127,20 @@ def build(sources: List[BuildSource], directories; if omitted, use '.' as the data directory """ - data_dir = default_data_dir(bin_dir) + if options.custom_typeshed_dir: + custom_data_dir, custom_typeshed_name = os.path.split( + options.custom_typeshed_dir.rstrip(os.sep)) + data_dir = custom_data_dir + else: + custom_typeshed_name = None + data_dir = default_data_dir(bin_dir) 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_name=custom_typeshed_name) if options.use_builtins_fixtures: # Use stub builtins (to speed up test cases and to make them easier to @@ -249,10 +257,13 @@ 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_name: str = None) -> List[str]: """Return default standard library search paths.""" # IDEA: Make this more portable. path = [] # type: List[str] + typeshed_name = custom_typeshed_name or 'typeshed' auto = os.path.join(data_dir, 'stubs-auto') if os.path.isdir(auto): @@ -266,7 +277,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(data_dir, typeshed_name, lib_type, v) if os.path.isdir(stubdir): path.append(stubdir) diff --git a/mypy/main.py b/mypy/main.py index bbc30028639d7..3399d3ca80b48 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -188,6 +188,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', diff --git a/mypy/options.py b/mypy/options.py index 1d0945645a830..56f1e7e58bdfb 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 From a00b4f666b9997edbbf8063be5152c3c6e84fa4b Mon Sep 17 00:00:00 2001 From: David Fisher Date: Thu, 20 Oct 2016 13:31:30 -0700 Subject: [PATCH 2/4] Make work with config file and add description --- docs/source/config_file.rst | 4 ++++ mypy/main.py | 1 + 2 files changed, 5 insertions(+) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index f3698063e7792..c036c8963ccb3 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/main.py b/mypy/main.py index 3399d3ca80b48..ff8c57862b048 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -434,6 +434,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, } From 00f33231ea2d92fe421a4cbc3a081db9665987f2 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Thu, 20 Oct 2016 13:48:12 -0700 Subject: [PATCH 3/4] refactor to fix issues pointed out in review --- mypy/build.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 3bded932fa7f8..2d212a7fb917a 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -127,20 +127,14 @@ def build(sources: List[BuildSource], directories; if omitted, use '.' as the data directory """ - if options.custom_typeshed_dir: - custom_data_dir, custom_typeshed_name = os.path.split( - options.custom_typeshed_dir.rstrip(os.sep)) - data_dir = custom_data_dir - else: - custom_typeshed_name = None - data_dir = default_data_dir(bin_dir) + data_dir = default_data_dir(bin_dir) find_module_clear_caches() # Determine the default module search path. lib_path = default_lib_path(data_dir, options.python_version, - custom_typeshed_name=custom_typeshed_name) + 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 @@ -259,16 +253,18 @@ def mypy_path() -> List[str]: def default_lib_path(data_dir: str, pyversion: Tuple[int, int], - custom_typeshed_name: str = None) -> List[str]: + custom_typeshed_dir: Optional[str]) -> List[str]: """Return default standard library search paths.""" # IDEA: Make this more portable. path = [] # type: List[str] - typeshed_name = custom_typeshed_name or 'typeshed' - - 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) @@ -277,7 +273,7 @@ def default_lib_path(data_dir: 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_name, lib_type, v) + stubdir = os.path.join(typeshed_dir, lib_type, v) if os.path.isdir(stubdir): path.append(stubdir) From 3ca925393d0617a5eac4e9b505299fcc5bab6b29 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Tue, 25 Oct 2016 15:09:20 -0700 Subject: [PATCH 4/4] properly merge submodule --- typeshed | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeshed b/typeshed index 1db6a12ead69d..f6bbc46c35b4c 160000 --- a/typeshed +++ b/typeshed @@ -1 +1 @@ -Subproject commit 1db6a12ead69d2d08652b4552e5e1fc1a077d970 +Subproject commit f6bbc46c35b4c54cc6dd3268062df447ac81e718