Repository navigation
fix: allow removal of unused imports with trailing comments - #373
Open
AdamMagued wants to merge 1 commit into
Open
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
Unused import lines with trailing inline comments were previously skipped from removal because autoflake bypassed all lines containing "#" before evaluating unused imports. - Restrict inline comment bypass to "# noqa" directives. - Update multiline check and extract_package_name to ignore characters in comments. - Preserve trailing comments when only a subset of imported symbols on a line are removed. - Allow full removal of unused import lines with trailing comments while maintaining block structure. - Add regression tests covering import lines with trailing comments. Fixes PyCQA#106
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When an unused import line had a trailing comment (e.g.
import unused_module # comment), autoflake previously skipped removal entirely because line iteration checkedelif "#" in line: result = linebefore checkingline_number in marked_import_line_numbers.This PR addresses the issue by:
line_number in marked_import_line_numbersbefore generic#comment checks, while continuing to respect# noqadirectives via a dedicatedis_noqahelper._valid_char_in_lineis used inmultiline_importandmultiline_statementso characters like(,),:, and;within inline comments do not incorrectly mark single-line imports as multiline.extract_package_nameandfilter_unused_importto operate on the code portion of the line when parsing package names and commas.filter_from_importandbreak_up_importwhen only a subset of imports on the line are removed.test_autoflake.pycovering single and multiple imports with trailing comments.Fixes #106