Skip to content

Commit 0555178

Browse files
Guido van RossumJukkaL
Guido van Rossum
authored andcommitted
Accept multiple files on command line. Fixes issue #935.
1 parent fb09f7d commit 0555178

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

mypy/build.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int],
269269

270270

271271
def lookup_program(module: str, lib_path: List[str]) -> str:
272-
# Modules are .py and not .pyi
272+
# Modules are .py or .pyi
273273
path = find_module(module, lib_path)
274274
if path:
275275
return path
@@ -544,12 +544,20 @@ def log(self, message: str) -> None:
544544
def remove_cwd_prefix_from_path(p: str) -> str:
545545
"""Remove current working directory prefix from p, if present.
546546
547+
Also crawl up until a directory without __init__.py is found.
548+
547549
If the result would be empty, return '.' instead.
548550
"""
549551
cur = os.getcwd()
550552
# Add separator to the end of the path, unless one is already present.
551553
if basename(cur) != '':
552554
cur += os.sep
555+
# Compute root path.
556+
while p and os.path.isfile(os.path.join(p, '__init__.py')):
557+
dir, base = os.path.split(p)
558+
if not base:
559+
break
560+
p = dir
553561
# Remove current directory prefix from the path, if present.
554562
if p.startswith(cur):
555563
p = p[len(cur):]

mypy/main.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
9999
module to run as script (or None),
100100
parsed flags)
101101
"""
102+
# TODO: Rewrite using argparse.
102103
options = Options()
103104
help = False
104105
ver = False
@@ -171,14 +172,30 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
171172
if not args:
172173
usage('Missing target file or module')
173174

174-
if args[1:]:
175-
usage('Extra argument: {}'.format(args[1]))
176-
177175
if options.python_path and options.pyversion[0] == 2:
178176
usage('Python version 2 (or --py2) specified, '
179177
'but --use-python-path will search in sys.path of Python 3')
180178

181-
return [BuildSource(args[0], None, None)], options
179+
return [BuildSource(arg, file_to_mod(arg), None) for arg in args], options
180+
181+
182+
def file_to_mod(arg: str) -> str:
183+
"""Convert a .py filename to a module name.
184+
185+
We crawl up the path until we find a directory without __init__.py.
186+
"""
187+
if not arg.endswith('.py'):
188+
return '__main__' # Special case for entry scripts.
189+
dir, mod = os.path.split(arg)
190+
if mod.endswith('.py'):
191+
mod = mod[:-3]
192+
assert '.' not in mod
193+
while dir and os.path.isfile(os.path.join(dir, '__init__.py')):
194+
dir, base = os.path.split(dir)
195+
if not base:
196+
break
197+
mod = base + '.' + mod
198+
return mod
182199

183200

184201
# Don't generate this from mypy.reports, not all are meant to be public.
@@ -200,15 +217,16 @@ def is_report(arg: str) -> bool:
200217

201218

202219
def usage(msg: str = None) -> None:
220+
# TODO: Add other supported options (--package, -f/--dirty-stubs, ...)
203221
if msg:
204222
sys.stderr.write('%s\n' % msg)
205223
sys.stderr.write("""\
206-
usage: mypy [option ...] [-c cmd | -m mod | file]
224+
usage: mypy [option ...] [-c cmd | -m mod | file ...]
207225
Try 'mypy -h' for more information.
208226
""")
209227
else:
210228
sys.stderr.write("""\
211-
usage: mypy [option ...] [-m mod | file]
229+
usage: mypy [option ...] [-c cmd | -m mod | file ...]
212230
213231
Optional arguments:
214232
-h, --help print this help message and exit

0 commit comments

Comments
 (0)