-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix a crash on psycopg2 for elif used #5369
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
Changes from all commits
a80d00b
d5dec5c
82d7b80
1f78cb9
23e74e7
500351f
ee84cab
4b1a3c5
c0ec435
b2b9545
e1fe167
29cf454
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
|
||
from pylint.checkers import BaseTokenChecker | ||
from pylint.checkers.utils import check_messages | ||
from pylint.interfaces import IAstroidChecker, ITokenChecker | ||
from pylint.interfaces import HIGH, IAstroidChecker, ITokenChecker | ||
|
||
|
||
class ElseifUsedChecker(BaseTokenChecker): | ||
|
@@ -38,37 +38,27 @@ def __init__(self, linter=None): | |
self._init() | ||
|
||
def _init(self): | ||
self._elifs = [] | ||
self._if_counter = 0 | ||
self._elifs = {} | ||
|
||
def process_tokens(self, tokens): | ||
# Process tokens and look for 'if' or 'elif' | ||
for _, token, _, _, _ in tokens: | ||
if token == "elif": | ||
self._elifs.append(True) | ||
elif token == "if": | ||
self._elifs.append(False) | ||
"""Process tokens and look for 'if' or 'elif'""" | ||
self._elifs = { | ||
begin: token for _, token, begin, _, _ in tokens if token in {"elif", "if"} | ||
} | ||
|
||
def leave_module(self, _: nodes.Module) -> None: | ||
self._init() | ||
|
||
def visit_ifexp(self, node: nodes.IfExp) -> None: | ||
if isinstance(node.parent, nodes.FormattedValue): | ||
return | ||
self._if_counter += 1 | ||
|
||
def visit_comprehension(self, node: nodes.Comprehension) -> None: | ||
self._if_counter += len(node.ifs) | ||
|
||
@check_messages("else-if-used") | ||
def visit_if(self, node: nodes.If) -> None: | ||
if isinstance(node.parent, nodes.If): | ||
orelse = node.parent.orelse | ||
# current if node must directly follow an "else" | ||
if orelse and orelse == [node]: | ||
if not self._elifs[self._if_counter]: | ||
self.add_message("else-if-used", node=node) | ||
self._if_counter += 1 | ||
"""Current if node must directly follow an 'else'""" | ||
if ( | ||
isinstance(node.parent, nodes.If) | ||
and node.parent.orelse == [node] | ||
and (node.lineno, node.col_offset) in self._elifs | ||
and self._elifs[(node.lineno, node.col_offset)] == "if" | ||
): | ||
self.add_message("else-if-used", node=node, confidence=HIGH) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not having an off by one error when we miscount an If increased the confidence π But really the other option are: Confidence = namedtuple("Confidence", ["name", "description"])
# Warning Certainties
HIGH = Confidence("HIGH", "No false positive possible.")
INFERENCE = Confidence("INFERENCE", "Warning based on inference result.")
INFERENCE_FAILURE = Confidence(
"INFERENCE_FAILURE", "Warning based on inference with failures."
)
UNDEFINED = Confidence("UNDEFINED", "Warning without any associated confidence level.")
CONFIDENCE_LEVELS = [HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED] There's no inference here, so it should probably by HIGH. But also, we should probably refactor this (?) It was not used before so we might as well make it make sense. I'm not confident enough to say there will never be any false positive just because there was no inference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like we could use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated #5317 |
||
|
||
|
||
def register(linter): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
else-if-used:10:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH | ||
else-if-used:22:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH | ||
else-if-used:13:8:my_function:"Consider using ""elif"" instead of ""else if""":HIGH | ||
else-if-used:25:20:my_function:"Consider using ""elif"" instead of ""else if""":HIGH | ||
else-if-used:44:8:_if_in_fstring_comprehension_with_elif:"Consider using ""elif"" instead of ""else if""":HIGH | ||
else-if-used:47:12:_if_in_fstring_comprehension_with_elif:"Consider using ""elif"" instead of ""else if""":HIGH |
Uh oh!
There was an error while loading. Please reload this page.