diff --git a/mypy/find_sources.py b/mypy/find_sources.py index e1edebe90985..af4f7101038c 100644 --- a/mypy/find_sources.py +++ b/mypy/find_sources.py @@ -38,6 +38,7 @@ def create_source_list( finder = SourceFinder(fscache, options) sources = [] + empty_dirs = [] for path in paths: path = os.path.normpath(path) if path.endswith(PY_EXTENSIONS): @@ -47,11 +48,14 @@ def create_source_list( elif fscache.isdir(path): sub_sources = finder.find_sources_in_dir(path) if not sub_sources and not allow_empty_dir: - raise InvalidSourceList(f"There are no .py[i] files in directory '{path}'") + empty_dirs.append(path) sources.extend(sub_sources) else: mod = os.path.basename(path) if options.scripts_are_modules else None sources.append(BuildSource(path, mod, None)) + if not sources and empty_dirs: + empty_dirs_str = ", ".join(map(repr, empty_dirs)) + raise InvalidSourceList(f"There are no .py[i] files in directories: {empty_dirs_str}") return sources diff --git a/mypy/test/test_find_sources.py b/mypy/test/test_find_sources.py index 321f3405e999..2236efc985ca 100644 --- a/mypy/test/test_find_sources.py +++ b/mypy/test/test_find_sources.py @@ -261,6 +261,16 @@ def test_find_sources_in_dir_namespace_multi_dir(self) -> None: finder = SourceFinder(FakeFSCache({"/a/pkg/a.py", "/b/pkg/b.py"}), options) assert find_sources_in_dir(finder, "/") == [("pkg.a", "/a"), ("pkg.b", "/b")] + def test_find_sources_with_empty_directory(self) -> None: + options = Options() + fscache = FakeFSCache({"/empty/README", "/other'empty/README", "/src/a.py"}) + + assert find_sources(["/empty", "/src"], options, fscache) == [("a", "/src")] + with pytest.raises(InvalidSourceList, match="directories") as exc_info: + find_sources(["/empty", "/other'empty"], options, fscache) + assert repr(os.path.abspath("/empty")) in str(exc_info.value) + assert repr(os.path.abspath("/other'empty")) in str(exc_info.value) + def test_find_sources_exclude(self) -> None: options = Options() options.namespace_packages = True