Skip to content

Commit 0a6f9ab

Browse files
authored
Improve error message for invalid Python package (#20482)
Resolves #17108
1 parent 1c866bf commit 0a6f9ab

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

mypy/find_sources.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ def _crawl_up_helper(self, dir: str) -> tuple[str, str] | None:
195195
if not name.isidentifier():
196196
# in most cases the directory name is invalid, we'll just stop crawling upwards
197197
# but if there's an __init__.py in the directory, something is messed up
198-
raise InvalidSourceList(f"{name} is not a valid Python package name")
198+
raise InvalidSourceList(
199+
f"{name} contains {os.path.basename(init_file)} "
200+
"but is not a valid Python package name"
201+
)
199202
# we're definitely a package, so we always return a non-None value
200203
mod_prefix, base_dir = self.crawl_up_dir(parent)
201204
return module_join(mod_prefix, name), base_dir

test-data/unit/cmdline.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ dir/a.py:1: error: Name "undef" is not defined
106106
[file dir/sub.pkg/a.py]
107107
undef
108108
[out]
109-
sub.pkg is not a valid Python package name
109+
sub.pkg contains __init__.py but is not a valid Python package name
110110
== Return code: 2
111111

112112
[case testBadFileEncoding]

0 commit comments

Comments
 (0)