Skip to content

Commit 6230e3f

Browse files
committed
Introducing --python-path flag to search running Python's sys.path for modules.
1 parent 53c1ec7 commit 6230e3f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

mypy/build.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def build(program_path: str,
9191
pyversion: int = 3,
9292
custom_typing_module: str = None,
9393
html_report_dir: str = None,
94-
flags: List[str] = None) -> BuildResult:
94+
flags: List[str] = None,
95+
python_path: bool = False) -> BuildResult:
9596
"""Build a mypy program.
9697
9798
A single call to build performs parsing, semantic analysis and optionally
@@ -122,7 +123,7 @@ def build(program_path: str,
122123
data_dir = default_data_dir(bin_dir)
123124

124125
# Determine the default module search path.
125-
lib_path = default_lib_path(data_dir, target, pyversion)
126+
lib_path = default_lib_path(data_dir, target, pyversion, python_path)
126127

127128
if TEST_BUILTINS in flags:
128129
# Use stub builtins (to speed up test cases and to make them easier to
@@ -191,7 +192,8 @@ def default_data_dir(bin_dir: str) -> str:
191192
raise RuntimeError("Broken installation: can't determine base dir")
192193

193194

194-
def default_lib_path(data_dir: str, target: int, pyversion: int) -> List[str]:
195+
def default_lib_path(data_dir: str, target: int, pyversion: int,
196+
python_path:bool) -> List[str]:
195197
"""Return default standard library search paths."""
196198
# IDEA: Make this more portable.
197199
path = List[str]()
@@ -218,6 +220,10 @@ def default_lib_path(data_dir: str, target: int, pyversion: int) -> List[str]:
218220
if sys.platform != 'win32':
219221
path.append('/usr/local/lib/mypy')
220222

223+
# Contents of Python's sys.path go last, to prefer the stubs
224+
if python_path:
225+
path.extend(sys.path)
226+
221227
return path
222228

223229

scripts/mypy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Options:
2323
self.pyversion = 3
2424
self.custom_typing_module = None # type: str
2525
self.html_report_dir = None # type: str
26+
self.python_path = False
2627

2728

2829
def main() -> None:
@@ -73,7 +74,8 @@ def type_check_only(path: str, module: str, bin_dir: str, options: Options) -> N
7374
pyversion=options.pyversion,
7475
custom_typing_module=options.custom_typing_module,
7576
html_report_dir=options.html_report_dir,
76-
flags=options.build_flags)
77+
flags=options.build_flags,
78+
python_path=options.python_path)
7779

7880

7981
def process_options(args: List[str]) -> Tuple[str, str, Options]:
@@ -112,6 +114,9 @@ def process_options(args: List[str]) -> Tuple[str, str, Options]:
112114
options.html_report_dir = args[1]
113115
options.build_flags.append('html-report')
114116
args = args[2:]
117+
elif args[0] == '--python-path':
118+
options.python_path = True
119+
args = args[1:]
115120
else:
116121
usage('Unknown option: {}'.format(args[0]))
117122

@@ -124,6 +129,10 @@ def process_options(args: List[str]) -> Tuple[str, str, Options]:
124129
if args[1:]:
125130
usage('Extra argument: {}'.format(args[1]))
126131

132+
if options.python_path and options.pyversion == 2:
133+
usage('--py2 specified, '
134+
'but --python_path will search in sys.path of Python 3')
135+
127136
return args[0], None, options
128137

129138

@@ -143,6 +152,7 @@ Optional arguments:
143152
--html-report dir generate a HTML report of type precision under dir/
144153
-m mod type check module
145154
--verbose more verbose messages
155+
--python_path search for modules in sys.path of running Python
146156
147157
Environment variables:
148158
MYPYPATH additional module search path

0 commit comments

Comments
 (0)