Skip to content

Commit e26052f

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

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

mypy/build.py

Lines changed: 18 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,21 @@ 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(
165+
custom_typeshed_module.rstrip(os.sep))
166+
data_dir = custom_data_dir
167+
else:
168+
data_dir = default_data_dir(bin_dir)
162169

163170
find_module_clear_caches()
164171

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

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

278288

279-
def default_lib_path(data_dir: str, pyversion: Tuple[int, int],
280-
python_path: bool) -> List[str]:
289+
def default_lib_path(data_dir: str,
290+
pyversion: Tuple[int, int],
291+
python_path: bool,
292+
custom_typeshed_name: str = None) -> List[str]:
281293
"""Return default standard library search paths."""
282294
# IDEA: Make this more portable.
283295
path = [] # type: List[str]
296+
typeshed_name = custom_typeshed_name or 'typeshed'
284297

285298
auto = os.path.join(data_dir, 'stubs-auto')
286299
if os.path.isdir(auto):
@@ -294,7 +307,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int],
294307
# (Note that 3.1 and 3.0 aren't really supported, but we don't care.)
295308
for v in versions + [str(pyversion[0]), '2and3']:
296309
for lib_type in ['stdlib', 'third_party']:
297-
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
310+
stubdir = os.path.join(data_dir, typeshed_name, lib_type, v)
298311
if os.path.isdir(stubdir):
299312
path.append(stubdir)
300313

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)