Fix #2528: Handle isort: off/on comments with and without spaces#2544
Closed
harshith-coder wants to merge 1 commit into
Closed
Fix #2528: Handle isort: off/on comments with and without spaces#2544harshith-coder wants to merge 1 commit into
harshith-coder wants to merge 1 commit into
Conversation
This fix addresses issue PyCQA#2528 where isort: off was not properly recognized when written without spaces (e.g., '#isort: off' or '# isort:off'). Changes: - Updated core.py to use flexible string matching for 'isort: off' and 'isort: on' - Now handles: '# isort: off', '#isort: off', and '# isort:off' variations - Added comprehensive regression tests for both import and from import statements - Verified that isort: off honors both plain imports and from imports equally Fixes: PyCQA#2528
68a97eb to
d4131a5
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2544 +/- ##
=======================================
Coverage 99.15% 99.15%
=======================================
Files 41 41
Lines 3094 3094
Branches 669 669
=======================================
Hits 3068 3068
Misses 14 14
Partials 12 12 🚀 New features to boost your workflow:
|
Member
|
This does not the fix the linked issue. |
Author
|
Hi @DanielNoord, Thank you for reviewing the PR. I understand you mentioned this doesn't fix the linked issue #2528. I'd appreciate clarification on what aspect of issue #2528 is not being addressed. Based on my investigation, the fix I submitted handles the case where I tested the fix with: test_code = """#isort: off
import z
import a
import b
"""
result = isort.code(test_code)
# Result: imports remain unsorted ✓ |
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.
Description
Fixes #2528: Handle
isort: offandisort: oncomments with and without spacesRoot Cause
The comment detection in
core.pywas using exact string matching (e.g.,stripped_line == "# isort: off"), which only worked with specific spacing. Comments like#isort: offor# isort:offwere not recognized, causing imports to be sorted despite the directive.Changes Made
isort/core.pyto use flexible string matching for comment detectiontest_action_comments.pyFiles Changed
isort/core.py(lines 93, 99, 181, 236)tests/unit/test_action_comments.py(new test function with 6 test cases)Testing
isort: offisort: offisort: off#isort: off,# isort:off,# isort: offisort: onVerification