diff --git a/black.py b/black.py index 957e51a939f..320abd3ab72 100644 --- a/black.py +++ b/black.py @@ -441,8 +441,10 @@ def main( gen_python_files_in_dir(p, root, include_regex, exclude_regex, report) ) elif p.is_file() or s == "-": - # if a file was explicitly given, we don't care about its extension - sources.add(p) + # if a file was explicitly given, we bypass include + sources.update( + gen_python_files([p], root, include=None, exclude=exclude_regex, report=report) + ) else: err(f"invalid path: {s}") if len(sources) == 0: @@ -3406,10 +3408,10 @@ def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]: return imports -def gen_python_files_in_dir( - path: Path, +def gen_python_files( + paths: [Path], root: Path, - include: Pattern[str], + include: Optional[Pattern[str]], exclude: Pattern[str], report: "Report", ) -> Iterator[Path]: @@ -3421,7 +3423,7 @@ def gen_python_files_in_dir( `report` is where output about exclusions goes. """ assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}" - for child in path.iterdir(): + for child in paths: try: normalized_path = "/" + child.resolve().relative_to(root).as_posix() except ValueError: @@ -3444,11 +3446,22 @@ def gen_python_files_in_dir( yield from gen_python_files_in_dir(child, root, include, exclude, report) elif child.is_file(): - include_match = include.search(normalized_path) - if include_match: + # if an include regex is not provided, the file is included + if not include or include.search(normalized_path): yield child +def gen_python_files_in_dir( + path: Path, + root: Path, + include: Pattern[str], + exclude: Pattern[str], + report: "Report", +) -> Iterator[Path]: + return gen_python_files(path.iterdir()) + + + @lru_cache() def find_project_root(srcs: Iterable[str]) -> Path: """Return a directory containing .git, .hg, or pyproject.toml.