Skip to content

Commit 5afdb9d

Browse files
committed
Allow user to specify custom typeshed directory
1 parent 99904e0 commit 5afdb9d

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
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

mypy/build.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,20 @@ def build(sources: List[BuildSource],
127127
directories; if omitted, use '.' as the data directory
128128
"""
129129

130-
data_dir = default_data_dir(bin_dir)
130+
if options.custom_typeshed_dir:
131+
custom_data_dir, custom_typeshed_name = os.path.split(
132+
options.custom_typeshed_dir.rstrip(os.sep))
133+
data_dir = custom_data_dir
134+
else:
135+
custom_typeshed_name = None
136+
data_dir = default_data_dir(bin_dir)
131137

132138
find_module_clear_caches()
133139

134140
# Determine the default module search path.
135-
lib_path = default_lib_path(data_dir, options.python_version)
141+
lib_path = default_lib_path(data_dir,
142+
options.python_version,
143+
custom_typeshed_name=custom_typeshed_name)
136144

137145
if options.use_builtins_fixtures:
138146
# Use stub builtins (to speed up test cases and to make them easier to
@@ -249,10 +257,13 @@ def mypy_path() -> List[str]:
249257
return path_env.split(os.pathsep)
250258

251259

252-
def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
260+
def default_lib_path(data_dir: str,
261+
pyversion: Tuple[int, int],
262+
custom_typeshed_name: str = None) -> List[str]:
253263
"""Return default standard library search paths."""
254264
# IDEA: Make this more portable.
255265
path = [] # type: List[str]
266+
typeshed_name = custom_typeshed_name or 'typeshed'
256267

257268
auto = os.path.join(data_dir, 'stubs-auto')
258269
if os.path.isdir(auto):
@@ -266,7 +277,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int]) -> List[str]:
266277
# (Note that 3.1 and 3.0 aren't really supported, but we don't care.)
267278
for v in versions + [str(pyversion[0]), '2and3']:
268279
for lib_type in ['stdlib', 'third_party']:
269-
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
280+
stubdir = os.path.join(data_dir, typeshed_name, lib_type, v)
270281
if os.path.isdir(stubdir):
271282
path.append(stubdir)
272283

mypy/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def process_options(args: List[str],
188188
help="dump type inference stats")
189189
parser.add_argument('--custom-typing', metavar='MODULE', dest='custom_typing_module',
190190
help="use a custom typing module")
191+
parser.add_argument('--custom-typeshed-dir', metavar='DIR',
192+
help="use the custom typeshed in DIR")
191193
parser.add_argument('--scripts-are-modules', action='store_true',
192194
help="Script x becomes module x instead of __main__")
193195
parser.add_argument('--config-file',

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)