Skip to content

Commit a197a66

Browse files
committed
Allow user to specify custom typeshed directory
1 parent 9624b54 commit a197a66

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

mypy/build.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def build(sources: List[BuildSource],
133133
bin_dir: str = None,
134134
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
135135
custom_typing_module: str = None,
136+
custom_typeshed_module: str = None,
136137
report_dirs: Dict[str, str] = None,
137138
flags: List[str] = None,
138139
python_path: bool = False) -> BuildResult:
@@ -158,12 +159,20 @@ def build(sources: List[BuildSource],
158159
report_dirs = report_dirs or {}
159160
flags = flags or []
160161

161-
data_dir = default_data_dir(bin_dir)
162+
custom_typeshed_name = None
163+
if custom_typeshed_module:
164+
custom_data_dir, custom_typeshed_name = os.path.split(custom_typeshed_module.rstrip(os.sep))
165+
data_dir = custom_data_dir
166+
else:
167+
data_dir = default_data_dir(bin_dir)
162168

163169
find_module_clear_caches()
164170

165171
# Determine the default module search path.
166-
lib_path = default_lib_path(data_dir, pyversion, python_path)
172+
lib_path = default_lib_path(data_dir,
173+
pyversion,
174+
python_path,
175+
custom_typeshed_name=custom_typeshed_name)
167176

168177
if TEST_BUILTINS in flags:
169178
# Use stub builtins (to speed up test cases and to make them easier to
@@ -276,11 +285,14 @@ def mypy_path() -> List[str]:
276285
return path_env.split(os.pathsep)
277286

278287

279-
def default_lib_path(data_dir: str, pyversion: Tuple[int, int],
280-
python_path: bool) -> List[str]:
288+
def default_lib_path(data_dir: str,
289+
pyversion: Tuple[int, int],
290+
python_path: bool,
291+
custom_typeshed_name: str = None) -> List[str]:
281292
"""Return default standard library search paths."""
282293
# IDEA: Make this more portable.
283294
path = [] # type: List[str]
295+
typeshed_name = custom_typeshed_name or 'typeshed'
284296

285297
auto = os.path.join(data_dir, 'stubs-auto')
286298
if os.path.isdir(auto):
@@ -294,7 +306,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int],
294306
# (Note that 3.1 and 3.0 aren't really supported, but we don't care.)
295307
for v in versions + [str(pyversion[0]), '2and3']:
296308
for lib_type in ['stdlib', 'third_party']:
297-
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
309+
stubdir = os.path.join(data_dir, typeshed_name, lib_type, v)
298310
if os.path.isdir(stubdir):
299311
path.append(stubdir)
300312

mypy/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self) -> None:
2727
self.build_flags = [] # type: List[str]
2828
self.pyversion = defaults.PYTHON3_VERSION
2929
self.custom_typing_module = None # type: str
30+
self.custom_typeshed_module = None # type: str
3031
self.report_dirs = {} # type: Dict[str, str]
3132
self.python_path = False
3233
self.dirty_stubs = False
@@ -97,6 +98,7 @@ def type_check_only(sources: List[BuildSource],
9798
bin_dir=bin_dir,
9899
pyversion=options.pyversion,
99100
custom_typing_module=options.custom_typing_module,
101+
custom_typeshed_module=options.custom_typeshed_module,
100102
report_dirs=options.report_dirs,
101103
flags=options.build_flags,
102104
python_path=options.python_path)
@@ -160,6 +162,8 @@ def parse_version(v):
160162
help="an anti-pattern")
161163
parser.add_argument('--stats', action='store_true', help="dump stats")
162164
parser.add_argument('--inferstats', action='store_true', help="dump type inference stats")
165+
parser.add_argument('--custom-typeshed', metavar='PATH',
166+
help="use the custom typeshed module at PATH")
163167
parser.add_argument('--custom-typing', metavar='MODULE', help="use a custom typing module")
164168

165169
report_group = parser.add_argument_group(
@@ -214,6 +218,10 @@ def parse_version(v):
214218
options.python_path = args.use_python_path
215219
options.pdb = args.pdb
216220
options.custom_typing_module = args.custom_typing
221+
options.custom_typeshed_module = args.custom_typeshed
222+
if args.custom_typeshed:
223+
# Don't check if the standard stubs are clean if we're using custom ones anyway.
224+
options.dirty_stubs = True
217225

218226
# Set build flags.
219227
if args.python_version is not None:

0 commit comments

Comments
 (0)