From a5f2a2f6b7c63afe00739248eb40448f5040d3a4 Mon Sep 17 00:00:00 2001 From: Rick Chern Date: Wed, 24 May 2017 18:58:27 -0700 Subject: [PATCH 1/2] Better error message for invalid package names passed to mypy Revert accidental change --- mypy/main.py | 14 ++++++++++++-- test-data/unit/cmdline.test | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index a5511671c966..53ec3d06f973 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -457,9 +457,15 @@ def add_invertible_flag(flag: str, targets = [] for f in special_opts.files: if f.endswith(PY_EXTENSIONS): - targets.append(BuildSource(f, crawl_up(f)[1], None)) + try: + targets.append(BuildSource(f, crawl_up(f)[1], None)) + except ValueError as e: + fail(str(e)) elif os.path.isdir(f): - sub_targets = expand_dir(f) + try: + sub_targets = expand_dir(f) + except ValueError as e: + fail(str(e)) if not sub_targets: fail("There are no .py[i] files in directory '{}'" .format(f)) @@ -526,10 +532,14 @@ def crawl_up(arg: str) -> Tuple[str, str]: dir, base = os.path.split(dir) if not base: break + # Ensure that base is a valid python module name + if not base.isidentifier(): + raise ValueError('{} is not a valid Python package name'.format(base)) if mod == '__init__' or not mod: mod = base else: mod = base + '.' + mod + return dir, mod diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index d0648844daaa..a9ba3c28950b 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -75,6 +75,14 @@ undef dir/subpkg/a.py:1: error: Name 'undef' is not defined dir/a.py:1: error: Name 'undef' is not defined +[case testCmdlineInvalidPackageName] +# cmd: mypy dir/sub.pkg/a.py +[file dir/sub.pkg/__init__.py] +[file dir/sub.pkg/a.py] +undef +[out] +sub.pkg is not a valid Python package name + [case testBadFileEncoding] # cmd: mypy a.py [file a.py] From 5810fc1d3c41b2766c41f2a951162e99b6da298c Mon Sep 17 00:00:00 2001 From: Rick Chern Date: Wed, 24 May 2017 23:21:45 -0700 Subject: [PATCH 2/2] Add InvalidPackageName exception --- mypy/main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 53ec3d06f973..7edfff02e119 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -24,6 +24,10 @@ PY_EXTENSIONS = tuple(PYTHON_EXTENSIONS) +class InvalidPackageName(Exception): + """Exception indicating that a package name was invalid.""" + + def main(script_path: str, args: List[str] = None) -> None: """Main entry point to the type checker. @@ -459,12 +463,12 @@ def add_invertible_flag(flag: str, if f.endswith(PY_EXTENSIONS): try: targets.append(BuildSource(f, crawl_up(f)[1], None)) - except ValueError as e: + except InvalidPackageName as e: fail(str(e)) elif os.path.isdir(f): try: sub_targets = expand_dir(f) - except ValueError as e: + except InvalidPackageName as e: fail(str(e)) if not sub_targets: fail("There are no .py[i] files in directory '{}'" @@ -534,7 +538,7 @@ def crawl_up(arg: str) -> Tuple[str, str]: break # Ensure that base is a valid python module name if not base.isidentifier(): - raise ValueError('{} is not a valid Python package name'.format(base)) + raise InvalidPackageName('{} is not a valid Python package name'.format(base)) if mod == '__init__' or not mod: mod = base else: