Repository: psf/black
File: src/black/files.py (Python)

Code before:

    except OSError as e:
        if report:
            report.path_ignored(path, f"cannot be read because {e}")
        return None

    return root_relative_path


def path_is_excluded(
    normalized_path: str,
    pattern: Optional[Pattern[str]],
) -> bool:
    match = pattern.search(normalized_path) if pattern else None
    return bool(match and match.group(0))


def gen_python_files(
    paths: Iterable[Path],
    root: Path,
    include: Pattern[str],
    exclude: Pattern[str],
    extend_exclude: Optional[Pattern[str]],
    force_exclude: Optional[Pattern[str]],
    report: Report,
    gitignore: Optional[Dict[Path, PathSpec]],
    *,
    verbose: bool,
    quiet: bool,
) -> Iterator[Path]:
    """Generate all files under `path` whose paths are not excluded by the
    `exclude_regex`, `extend_exclude`, or `force_exclude` regexes,
    but are included by the `include` regex.

    Symbolic links pointing outside of the `root` directory are ignored.

    `report` is where output about exclusions goes.
    """

    def is_ignored(
        gitignore_dict: Dict[Path, PathSpec], child: Path, report: Report
    ) -> bool:
        for _dir, _gitignore in gitignore_dict.items():
            relative_path = normalize_path_maybe_ignore(child, _dir, report)
            if relative_path is None:
                break
            if _gitignore is not None and _gitignore.match_file(relative_path):
                report.path_ignored(child, "matches the .gitignore file content")
                return True
        return False


Review comment (suggestion):
Would suggest renaming this to `gitignore_dict` for clarity. Also, no reason to keep this Optional, we can just use an empty dict if there's no gitignores.

Code after:

    except OSError as e:
        if report:
            report.path_ignored(path, f"cannot be read because {e}")
        return None

    return root_relative_path


def path_is_ignored(
    path: Path, gitignore_dict: Dict[Path, PathSpec], report: Report
) -> bool:
    for gitignore_path, pattern in gitignore_dict.items():
        relative_path = normalize_path_maybe_ignore(path, gitignore_path, report)
        if relative_path is None:
            break
        if pattern.match_file(relative_path):
            report.path_ignored(path, "matches a .gitignore file content")
            return True
    return False


def path_is_excluded(
    normalized_path: str,
    pattern: Optional[Pattern[str]],
) -> bool:
    match = pattern.search(normalized_path) if pattern else None
    return bool(match and match.group(0))


def gen_python_files(
    paths: Iterable[Path],
    root: Path,
    include: Pattern[str],
    exclude: Pattern[str],
    extend_exclude: Optional[Pattern[str]],
    force_exclude: Optional[Pattern[str]],
    report: Report,
    gitignore_dict: Optional[Dict[Path, PathSpec]],
    *,
    verbose: bool,
    quiet: bool,
) -> Iterator[Path]:
    """Generate all files under `path` whose paths are not excluded by the
    `exclude_regex`, `extend_exclude`, or `force_exclude` regexes,
    but are included by the `include` regex.

    Symbolic links pointing outside of the `root` directory are ignored.

    `report` is where output about exclusions goes.
    """