Skip to content

Commit c316ca6

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

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

mypy/find_sources.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,30 @@ 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+
sources))
54+
55+
unsupported_files: List[BuildSource] = \
56+
list(filter(lambda source:
57+
source.path is not None
58+
and not source.path.endswith(PY_EXTENSIONS),
59+
sources))
60+
61+
if unsupported_files:
62+
unsupported_text = "Unsupported file type(s) where found in the source tree. "
63+
unsupported_text += "This can lead to an error (only .py and .pyi are supported):"
64+
print(unsupported_text)
65+
for file in unsupported_files:
66+
print(file.path)
67+
print("Use --exclude or add exclude to the config file. More information is here: "
68+
"https://mypy.readthedocs.io/en/stable/config_file.html?highlight=exclude#confval-exclude")
69+
70+
return filtered_sources
4871

4972

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

mypy/test/test_find_sources.py

Lines changed: 53 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,52 @@ 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+
options.exclude = [r'(?x)(\.json$)']
419+
fscache = FakeFSCache(files)
420+
421+
assert find_sources(all_files, options, fscache) == [
422+
('a', '/src/test/a'),
423+
('a1', '/pkg'),
424+
('a1.f', '/pkg'),
425+
('a1.v', '/pkg'),
426+
('a2', '/pkg'),
427+
('a2.a', '/pkg'),
428+
('a2.b', '/pkg'),
429+
('b', '/src/test')
430+
]

test-data/unit/cmdline.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,9 @@ tabs.py:2: error: Incompatible return value type (got "None", expected "str")
11301130
# coding: uft-8
11311131
[out]
11321132
mypy: stubgen does not support .pyd files: 'a.pyd'
1133+
Unsupported file type(s) where found in the source tree. This can lead to an error (only .py and .pyi are supported):
1134+
a.pyd
1135+
Use --exclude or add exclude to the config file. More information is here: https://mypy.readthedocs.io/en/stable/config_file.html?highlight=exclude#confval-exclude
11331136
== Return code: 2
11341137

11351138
[case testDuplicateModules]

0 commit comments

Comments
 (0)