Skip to content

Commit cbdd1f3

Browse files
author
nightcityblade
committed
Allow valid targets alongside empty directories
1 parent e3003dd commit cbdd1f3

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎mypy/find_sources.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def create_source_list(
3838
finder = SourceFinder(fscache, options)
3939

4040
sources = []
41+
empty_dirs = []
4142
for path in paths:
4243
path = os.path.normpath(path)
4344
if path.endswith(PY_EXTENSIONS):
@@ -47,11 +48,13 @@ def create_source_list(
4748
elif fscache.isdir(path):
4849
sub_sources = finder.find_sources_in_dir(path)
4950
if not sub_sources and not allow_empty_dir:
50-
raise InvalidSourceList(f"There are no .py[i] files in directory '{path}'")
51+
empty_dirs.append(path)
5152
sources.extend(sub_sources)
5253
else:
5354
mod = os.path.basename(path) if options.scripts_are_modules else None
5455
sources.append(BuildSource(path, mod, None))
56+
if not sources and empty_dirs:
57+
raise InvalidSourceList(f"There are no .py[i] files in directory '{empty_dirs[0]}'")
5558
return sources
5659

5760

‎mypy/test/test_find_sources.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import unittest
77

88
import pytest
9-
109
from mypy.find_sources import InvalidSourceList, SourceFinder, create_source_list
1110
from mypy.fscache import FileSystemCache
1211
from mypy.modulefinder import BuildSource
@@ -261,6 +260,14 @@ def test_find_sources_in_dir_namespace_multi_dir(self) -> None:
261260
finder = SourceFinder(FakeFSCache({"/a/pkg/a.py", "/b/pkg/b.py"}), options)
262261
assert find_sources_in_dir(finder, "/") == [("pkg.a", "/a"), ("pkg.b", "/b")]
263262

263+
def test_find_sources_with_empty_directory(self) -> None:
264+
options = Options()
265+
fscache = FakeFSCache({"/empty/README", "/src/a.py"})
266+
267+
assert find_sources(["/empty", "/src"], options, fscache) == [("a", "/src")]
268+
with pytest.raises(InvalidSourceList, match=r"directory '.*[\\/]empty'"):
269+
find_sources(["/empty"], options, fscache)
270+
264271
def test_find_sources_exclude(self) -> None:
265272
options = Options()
266273
options.namespace_packages = True

0 commit comments

Comments
 (0)