Filter out Python files with invalid module names during directory scanning #403
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Vulture was processing temporary and backup files created by text editors when scanning directories. For example, Emacs creates temporary files like
.#filename.pyfor unsaved buffers, and these were being analyzed by vulture even though they're not valid Python modules according to PEP 8.Solution
This PR adds filtering to only process Python files with valid module names when scanning directories. According to PEP 8, module names should be valid Python identifiers.
What's changed:
_is_valid_module_name()helper function that checks if a filename is a valid Python identifier (must start with a letter or underscore, followed by letters, digits, or underscores)get_modules()to filter out files with invalid names when usingrglob()on directoriesExamples of filtered files:
.#filename.py- Emacs temporary files.dotfile.py- Hidden files starting with dots2module.py- Files starting with numbersmy-module.py- Files containing dashes~backup.py- Backup filesExamples of files still processed:
module.py- Valid lowercase names_private.py- Names starting with underscore__init__.py- Double underscore namesmodule_123.py- Names containing numbers (but not starting with them)café.py- Unicode identifiers (valid per PEP 3131)Testing:
Added comprehensive test coverage including:
Fixes the issue where users had to manually exclude editor temporary files using patterns like
"*/.#*.py"in their configuration.Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.