8000 improve `files` and `exclude` parameter handling · python/mypy@99b62b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99b62b3

Browse files
committed
improve files and exclude parameter handling
1 parent 222029b commit 99b62b3

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

mypy/find_sources.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ def create_source_list(paths: Sequence[str], options: Options,
4444
else:
4545
mod = os.path.basename(path) if options.scripts_are_modules else None
4646
sources.append(BuildSource(path, mod, None))
47-
return sources
47+
48+
filtered_sources: List[BuildSource] = \
49+
list(filter(lambda source:
50+
source.path is None
51+
or (not matches_exclude(
52+
source.path, finder.exclude, finder.fscache, finder.verbosity >= 2)
53+
and source.path.endswith(PY_EXTENSIONS)),
54+
sources))
55+
return filtered_sources
4856

4957

5058
def keyfunc(name: str) -> Tuple[bool, int, str]:

mypy/test/test_find_sources.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import tempfile
55
import unittest
6+
import fnmatch
67
from typing import List, Optional, Set, Tuple
78

89
from mypy.find_sources import InvalidSourceList, SourceFinder, create_source_list
@@ -305,8 +306,9 @@ def test_find_sources_exclude(self) -> None:
305306
("a2.b.c.d.e", "/pkg"),
306307
("e", "/pkg/a1/b/c/d"),
307308
]
308-
assert find_sources(["/pkg/a1/b/f.py"], options, fscache) == [('f', '/pkg/a1/b')]
309-
assert find_sources(["/pkg/a2/b/f.py"], options, fscache) == [('a2.b.f', '/pkg')]
309+
310+
assert find_sources(["/pkg/a1/b/f.py"], options, fscache) == []
311+
assert find_sources(["/pkg/a2/b/f.py"], options, fscache) == []
310312

311313
# directory name
312314
options.exclude = ["/a1/"]
@@ -377,3 +379,51 @@ def test_find_sources_exclude(self) -> None:
377379
}
378380
fscache = FakeFSCache(files)
379381
assert len(find_sources(["."], options, fscache)) == len(files)
382+
383+
def test_find_sources_exclude_e2e(self) -> None:
384+
files_config = "/pkg/*, /src/test/"
385+
386+
files = {
387+
"/pkg/sample.json",
388+
"/pkg/test.json",
389+
"/pkg/a1/__init__.py",
390+
"/pkg/a1/f.py",
391+
"/pkg/a1/v.py",
392+
"/pkg/a2/__init__.py",
393+
"/pkg/a2/b.py",
394+
"/pkg/a2/a.py",
395+
"/src/test/a.py",
396+
"/src/test/b.py",
397+
"/src/test/a/a.py",
398+
}
399+
400+
def split_and_match_files(files: Set[str], paths: List[str]) -> List[str]:
401+
# mock split_and_match_files_list config_parser.py
402+
expanded_paths = []
403+
404+
for p in paths:
405+
p = p.strip()
406+
# glob uses fnmatch underneath
407+
matching = fnmatch.filter(files, p)
408+
print("PATH", p)
409+
if matching:
410+
expanded_paths.extend(matching)
411+
else:
412+
expanded_paths.append(p)
413+
return expanded_paths
414+
415+
all_files = split_and_match_files(files, files_config.split(','))
416+
del split_and_match_files
417+
options = Options()
418+
fscache = FakeFSCache(files)
419+
420+
assert find_sources(all_files, options, fscache) == [
421+
('a', '/src/test/a'),
422+
('a1', '/pkg'),
423+
('a1.f', '/pkg'),
424+
('a1.v', '/pkg'),
425+
('a2', '/pkg'),
426+
('a2.a', '/pkg'),
427+
('a2.b', '/pkg'),
428+
('b', '/src/test')
429+
]

0 commit comments

Comments
 (0)
0