Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve filter_python_files() #798

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/darker/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,21 @@ def filter_python_files(
``black_config``, relative to ``root``.

"""
absolute_paths = {p.resolve() for p in paths}
directories = {p for p in absolute_paths if p.is_dir()}
files = {p for p in absolute_paths if p not in directories}
# Split input paths into directories (which need recursion) and direct files
directories, files = set(), set()
for p in paths:
# Convert all input paths to absolute paths for consistent handling
path = p.resolve()
if path.is_dir():
directories.add(path)
else:
files.add(path)

# Recursively walk directories to find Python files, applying exclusion patterns.
# Get all Python files from directories that match our criteria:
# - Pass formatter's exclude/extend-exclude/force-exclude patterns
# - Match Python file extensions (.py, .pyi, .ipynb)
# - Aren't symlinks pointing outside the root
files_from_directories = set(
_gen_python_files(
directories,
Expand All @@ -155,4 +167,7 @@ def filter_python_files(
formatter.get_force_exclude(),
)
)

# Combine directly specified files with those found in directories.
# Convert all paths to be relative to the root directory.
return {p.resolve().relative_to(root) for p in files_from_directories | files}
Loading