-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Disables immediately after an else clause do not work properly. #872
Comments
Investigating a little further, it seems like the culprit is BlockRangeMixIn._elsed_block_range: https://github.com/PyCQA/astroid/blob/master/astroid/mixins.py#L43 Specifically, the logic here is that when an else clause is present, anything before the first line of the first else statement is considered to have occurred before the else. Would it make more sense for this check to be based on the last line of the last statement of the body of the if? I suppose that has the problem that a disable at the end of the body would impact the else...
It would be great if we had the line number of the else keyword itself, though I suspect that's a limitation from the underlying python parser. I'm also not sure about the repercussions for chained if/elif/else statements or the other uses of BlockRangeMixIn (Try, While, etc). |
Indeed, this is definitely problematic, which is one of the reasons we will try to switch to other parsers in pylint-dev/astroid#329. Right now I think that trying to fine tune the numbering with the builtin ast module will still result in these kind of peculiar cases, where we don't have an exact state of source code. |
The issue is still here. |
See #3898 (comment) if you want to fix this:
|
Not sure if this is helpful, but class PolarPoint:
_private: int
x = PolarPoint()
y = PolarPoint()
# Fails
if x == y:
pass
else:
# pylint:disable=protected-access
print(x._private)
# Fails
if x == y:
pass
# pylint:disable=protected-access
else:
print(x._private)
# Success
# pylint:disable=protected-access
if x == y:
pass
else:
print(x._private) |
I have opened an |
The disable has no effect here and a warning is generated. If any statement is insert between the else and the disable then it works properly. I suspect the line numbering for the else portion of the ast starts at the first child of the else clause rather than the else keyword itself.
The text was updated successfully, but these errors were encountered: