From b563e3ddf08a57c23a950e9a74c835ff0c8accbc Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Thu, 2 Jan 2025 22:56:36 +0200 Subject: [PATCH] feat: improve `filter_python_files()` Thanks @clintonsteiner! --- src/darker/files.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/darker/files.py b/src/darker/files.py index beb92ae35..e88cf85fc 100644 --- a/src/darker/files.py +++ b/src/darker/files.py @@ -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, @@ -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}