Fix pylint disable-next comments detaching from imports at function-body start#2564
Closed
Labib-Bin-Salam wants to merge 1 commit into
Closed
Fix pylint disable-next comments detaching from imports at function-body start#2564Labib-Bin-Salam wants to merge 1 commit into
Labib-Bin-Salam wants to merge 1 commit into
Conversation
…ody index positions When a function begins at the very start of a file (index 0), the first comment inside the function (at index 1 or 2) was incorrectly being flagged by the `in_top_comment` mechanism, causing it to be written to the output stream before the import section was sorted. This silently broke `# pylint: disable-next=...` annotations: both comments would end up above all imports rather than each traveling with the specific import it annotated. Root cause: `in_top_comment` fired at index 1-2 whenever no imports had been seen yet, regardless of whether the preceding non-blank line was a file header comment or actual code. Fix: add a `top_comment_started` flag that is set only when a comment at index 0 genuinely opens a file-level header block. For index 1-2, `in_top_comment` now requires either `top_comment_started` (file-level header continuation) or the comment to be an isort directive (`# isort:` prefix, e.g. `dont-add-import`). Additionally, when a function definition appears at the file start and a comment follows it inside the function body (at index < 3), the indent is inferred from the comment so that the comment and its following import land in the same section and can be parsed and sorted together. Fixes PyCQA#2054
Member
|
Closing this and your other PRs until you engage with humans, see #2559 Creating PRs without engaging takes away valuable resources from maintainers. |
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.
What's the issue?
Closes #2054.
When a function definition appears at the very start of a file (index 0), the first
comment inside the function body (at file-line index 1 or 2) was incorrectly flagged
by the
in_top_commentmechanism. This caused it to be written directly to the outputstream before the import section was sorted, silently breaking
# pylint: disable-next=...annotations:Root cause
in_top_commentfired at indices 1–2 whenever no imports had been seen yet,regardless of whether the preceding non-blank line was a genuine file header comment
(shebang / coding declaration) or actual code like
def foo():.Fix
Two targeted changes to
isort/core.py:top_comment_startedflag – set only when a comment at index 0 opens afile-level header block. At indices 1–2,
in_top_commentnow additionallyrequires either
top_comment_started(header continuation) or a# isort:directive prefix. Non-isort comments (e.g.
# pylint:) inside a function bodyare no longer mistakenly promoted to top comments.
Indent inference from comment – when a non-top comment at index < 3 enters the
import_sectionbuffer (instead of being written directly), the currentindentisinferred from its leading whitespace. This prevents a spurious indent-transition
flush that would otherwise separate the comment from its following import before
parse.file_contentscan associate them.Existing behaviour preserved
# isort: dont-add-import:and other# isort:directives at the top of a filecontinue to be treated as file-level control lines.
functions deeper in the file) retain existing behaviour – the indent-inference applies
only to the first three file lines.
Tests
A new regression test
test_pylint_disable_next_stays_with_import_issue_2054covers:# pylint: disable-nextannotations, each before a different importat index 2)