-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: support block-level wrong-import-position pragma suppression #10590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emmanuel-ferdman
wants to merge
6
commits into
pylint-dev:main
Choose a base branch
from
emmanuel-ferdman:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−29
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cace41
fix: scope wrong-import-position pragma to specific line only
emmanuel-ferdman a7e937a
fix: scope wrong-import-position pragma to specific line only
emmanuel-ferdman f957d35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1015d06
Support block-level wrong-import-position pragma suppression
emmanuel-ferdman 4c56438
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7fb7b18
fix: support block-level wrong-import-position pragma suppression
emmanuel-ferdman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Allow ``wrong-import-position`` pragma on non-import lines to suppress following imports until the next non-import statement. | ||
|
|
||
| Closes #10589 |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| """Checks that disabling 'wrong-import-position' on a statement prevents it from | ||
| invalidating subsequent imports.""" | ||
| """Test wrong-import-position pragma on non-import statement.""" | ||
| # pylint: disable=unused-import | ||
|
|
||
| CONSTANT = True # pylint: disable=wrong-import-position | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| CONSTANT_A = False # pylint: disable=wrong-import-position | ||
| import time | ||
|
|
||
| CONSTANT_B = True | ||
| import logging # [wrong-import-position] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| wrong-import-position:11:0:11:14::"Import ""import logging"" should be placed at the top of the module":UNDEFINED |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """Test wrong-import-position pragma scoping.""" | ||
| # pylint: disable=unused-import | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| # Pragma on non-import suppresses following imports until next non-import | ||
| CONSTANT_A = False # pylint: disable=wrong-import-position | ||
| import time | ||
|
|
||
| CONSTANT_B = True | ||
| import logging # [wrong-import-position] | ||
|
|
||
| # Inline pragma on import line | ||
| CONSTANT_C = 42 | ||
| import json # pylint: disable=wrong-import-position | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| wrong-import-position:12:0:12:14::"Import ""import logging"" should be placed at the top of the module":UNDEFINED |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test case where this
importis indented under a try or an if? I'm showing that this is not currently handled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobtylerwalls You are correct - there is inconsistent behavior and the question is what is the expected behaviour?
For the following example:
The currently PR code produce the following behaviour:
import timeflagged?Could you please help here define the expected behavior here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to just define what a consecutive import is and then stick to the letter of the rule. (We disable until the consecutive imports stop.)
For me, these two are consecutive imports:
These two are not:
Same with
if/with/for/while:defandclasscan't be empty, they must at least have a.../passDoes that seem sane?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobtylerwalls provides 4 examples of what he expects from the wrong-import-position rule itself.
I think you guys are discussing CHANGING the behavior, so these examples are not expected to match pylint 4.0.3. Nevertheless, it seems useful to state what existing 4.0.3 behavior is.
Your Expected 1 agrees with pylint 4.0.3.
Your Expected 2 disagrees. It seems like 4.0.3 treats the whole try-block as an import (a “wrong-import-position import”?, a WIPI?), because the try-clause smells like an import. The contents of the except-clause and else-clause are ignored. See
tests/functional/w/wrong_import_position2.pyAlso note that the try clause could contain non-imports before and after the “import black”, but those would not matter to the wrong-import-position algorithm. The try-block would STILL be assessed as an import for the purpose of this rule.
However if the try-clause DIDN'T contain an import, then yeah, the try block WOULDN'T smell like a WIPI.
Your Expected 3 -- disagrees with 4.0.3. ANY if-block is a non-import (non-WIPI) see
tests/functional/w/wrong_import_position14.pyYour Expected 4 -- agrees, but still, function def in the content of the if-block doesn't matter.
If I was king, I think I wouldn't even try to parse the contents of try/if/while/for/with. I'd treat them (their block, that is) as non-import regardless of content -- that is, just like the if-statement seems to be treated now.
And report any following module-level imports as wrong-import-position.
And a disable-pragma on the try/if/while/for/with statement, or a disable-next before, should cause the whole block to be treated as if it was an import.
So, I'm not challenging the sanity of your expectations, but I'm offering a different (sane?) POV.
This too would be behavior changing because existing
wrong_import_position2.pywould be broken :-/Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, another POV -- I could see an argument for flattening try-blocks and ignoring its exception paths.
Replace
with
because you're representing the success path. And you'd want to recurse to handle the try inside try.
Meh, seems too clever by half. If I had a vote, I'd oppose descending below the first level of the ast nodes. Treat a 'try' node as a non-import.
And I'm still not seeing a good rationale for flattening
withstatements to be anything other than a non-import.