Skip to content

Commit b8aa01b

Browse files
ddfishergvanrossum
authored andcommitted
Allow user to specify custom typeshed directory (#1588)
1 parent d0ea126 commit b8aa01b

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

docs/source/command_line.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ flag (or its long form ``--help``)::
1717
[--strict-optional]
1818
[--strict-optional-whitelist [GLOB [GLOB ...]]] [--pdb]
1919
[--show-traceback] [--stats] [--inferstats]
20-
[--custom-typing MODULE] [--scripts-are-modules]
21-
[--config-file CONFIG_FILE] [--show-column-numbers]
22-
[--html-report DIR] [--linecount-report DIR]
23-
[--linecoverage-report DIR] [--memory-xml-report DIR]
24-
[--old-html-report DIR] [--txt-report DIR] [--xml-report DIR]
25-
[--xslt-html-report DIR] [--xslt-txt-report DIR]
26-
[-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE]
20+
[--custom-typing MODULE] [--custom-typeshed-dir DIR]
21+
[--scripts-are-modules] [--config-file CONFIG_FILE]
22+
[--show-column-numbers] [--html-report DIR]
23+
[--linecount-report DIR] [--linecoverage-report DIR]
24+
[--memory-xml-report DIR] [--old-html-report DIR]
25+
[--txt-report DIR] [--xml-report DIR] [--xslt-html-report DIR]
26+
[--xslt-txt-report DIR] [-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE]
2727
[files [files ...]]
2828

2929
(etc., too long to show everything here)
@@ -326,6 +326,12 @@ Here are some more useful flags:
326326
package, the behavior enabled by this flag is often more
327327
convenient.)
328328

329+
- ``--custom-typeshed-dir DIR`` specifies the directory where mypy looks for
330+
typeshed stubs, instead of the typeshed that ships with mypy. This is
331+
primarily intended to make it easier to test typeshed changes before
332+
submitting them upstream, but also allows you to use a forked version of
333+
typeshed.
334+
329335
.. _config-file-flag:
330336

331337
- ``--config-file CONFIG_FILE`` causes configuration settings to be

docs/source/config_file.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ The following global flags may only be set in the global section
4747
alternative module which is to be considered equivalent to the
4848
``typing`` module.
4949

50+
- ``custom_typeshed_dir`` (string) specifies the name of an
51+
alternative directory which is used to look for stubs instead of the
52+
default ``typeshed`` directory.
53+
5054
- ``warn_incomplete_stub`` (Boolean, default False) warns for missing
5155
type annotation in typeshed. This is only relevant in combination
5256
with ``check_untyped_defs``.

mypy/build.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def build(sources: List[BuildSource],
135135
find_module_clear_caches()
136136

137137
# Determine the default module search path.
138-
lib_path = default_lib_path(data_dir, options.python_version)
138+
lib_path = default_lib_path(data_dir,
139+
options.python_version,
140+
custom_typeshed_dir=options.custom_typeshed_dir)
139141

140142
if options.use_builtins_fixtures:
141143
# Use stub builtins (to speed up test cases and to make them easier to
@@ -252,15 +254,20 @@ def mypy_path() -> List[str]:
252254
return path_env.split(os.pathsep)
253255

254256

255-
def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
257+
def default_lib_path(data_dir: str,
258+
pyversion: Tuple[int, int],
259+
custom_typeshed_dir: Optional[str]) -> List[str]:
256260
"""Return default standard library search paths."""
257261
# IDEA: Make this more portable.
258262
path = [] # type: List[str]
259263

260-
auto = os.path.join(data_dir, 'stubs-auto')
261-
if os.path.isdir(auto):
262-
data_dir = auto
263-
264+
if custom_typeshed_dir:
265+
typeshed_dir = custom_typeshed_dir
266+
else:
267+
auto = os.path.join(data_dir, 'stubs-auto')
268+
if os.path.isdir(auto):
269+
data_dir = auto
270+
typeshed_dir = os.path.join(data_dir, "typeshed")
264271
# We allow a module for e.g. version 3.5 to be in 3.4/. The assumption
265272
# is that a module added with 3.4 will still be present in Python 3.5.
266273
versions = ["%d.%d" % (pyversion[0], minor)
@@ -269,7 +276,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
269276
# (Note that 3.1 and 3.0 aren't really supported, but we don't care.)
270277
for v in versions + [str(pyversion[0]), '2and3']:
271278
for lib_type in ['stdlib', 'third_party']:
272-
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
279+
stubdir = os.path.join(typeshed_dir, lib_type, v)
273280
if os.path.isdir(stubdir):
274281
path.append(stubdir)
275282

mypy/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def process_options(args: List[str],
198198
help="dump type inference stats")
199199
parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
200200
help="use a custom typing module")
201+
parser.add_argument('--custom-typeshed-dir', metavar='DIR',
202+
help="use the custom typeshed in DIR")
201203
parser.add_argument('--scripts-are-modules', action='store_true',
202204
help="Script x becomes module x instead of __main__")
203205
parser.add_argument('--config-file',
@@ -451,6 +453,7 @@ def get_init_file(dir: str) -> Optional[str]:
451453
'python_version': lambda s: tuple(map(int, s.split('.'))),
452454
'strict_optional_whitelist': lambda s: s.split(),
453455
'custom_typing_module': str,
456+
'custom_typeshed_dir': str,
454457
'junit_xml': str,
455458
}
456459

mypy/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def __init__(self) -> None:
3434
self.build_type = BuildType.STANDARD
3535
self.python_version = defaults.PYTHON3_VERSION
3636
self.platform = sys.platform
37-
self.custom_typing_module = None # type: str
37+
self.custom_typing_module = None # type: Optional[str]
38+
self.custom_typeshed_dir = None # type: Optional[str]
3839
self.report_dirs = {} # type: Dict[str, str]
3940
self.silent_imports = False
4041
self.almost_silent = False

0 commit comments

Comments
 (0)