From c09a816aff21ff8018c28348e9fad430db9dfbdc Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Tue, 8 Nov 2022 10:02:36 -0300 Subject: [PATCH] Make nested function a top-level function The function to match a given path with every discovered .gitignore file does not need to be a nested function and can be a top-level function. The arguments did not change, but the naming of local variables was improved for readability. Signed-off-by: Antonio Ossa Guerra --- src/black/files.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/black/files.py b/src/black/files.py index c58970e53e3..ea517f4ece9 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -182,6 +182,19 @@ def normalize_path_maybe_ignore( 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]], @@ -212,18 +225,6 @@ def gen_python_files( `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 - assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}" for child in paths: normalized_path = normalize_path_maybe_ignore(child, root, report) @@ -231,7 +232,7 @@ def is_ignored( continue # First ignore files matching .gitignore, if passed - if gitignore_dict is not None and is_ignored(gitignore_dict, child, report): + if gitignore_dict and path_is_ignored(child, gitignore_dict, report): continue # Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.