Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for invalid package names passed to mypy #3447

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -457,9 +461,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 InvalidPackageName as e:
fail(str(e))
elif os.path.isdir(f):
sub_targets = expand_dir(f)
try:
sub_targets = expand_dir(f)
except InvalidPackageName as e:
fail(str(e))
if not sub_targets:
fail("There are no .py[i] files in directory '{}'"
.format(f))
Expand Down Expand Up @@ -526,10 +536,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 InvalidPackageName('{} is not a valid Python package name'.format(base))
if mod == '__init__' or not mod:
mod = base
else:
mod = base + '.' + mod

return dir, mod


Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down