-
-
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
New checker: comparison to empty string constant #1183
New checker: comparison to empty string constant #1183
Conversation
@atodorov Thanks for the PR! I've got to think this one over a bit. I've definitely had code where I compare to empty string as shorthand for If we go forward with this one I'd like to see us turn it off by default. |
@rowillia - others to think about are |
@atodorov The In these cases where we don't know the type of |
I agree with @rowillia that this might make sense only in some situations. I think this can become an extension, since we don't have full support for disabled checks (only checker classes). |
f5e71d5
to
9c77e2a
Compare
@PCManticore @rowillia refactored into an extension so that people can enable it if they so desire. (I certainly have a need for these checkers). Can you comment on the code/docs, etc so I can proceed with the other examples I've listed in the comment above? |
9c77e2a
to
e0a38ee
Compare
|
||
You can activate this checker by adding the line:: | ||
|
||
load-plugins=pylint.extensions.docparams |
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.
Probably you mean emptystring
here?
|
||
|
||
def _is_constant_empty_str(node): | ||
return isinstance(node, astroid.Const) and isinstance(node.value, str) and node.value == '' |
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.
You can drop the middle check, since the last one makes it redundant.
__implements__ = (interfaces.IAstroidChecker,) | ||
|
||
# configuration section name | ||
name = 'cmp-empty-str' |
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.
compare-to-empty-string
? Longer, but more intuitive.
error_detected = False | ||
|
||
# x ?? "" | ||
if _is_constant_empty_str(op_1) and op_2 in ['!=', '==', 'is not', 'is']: |
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.
Let's reuse this list, move it into a separate variable.
@atodorov In general, it looks good, apart of a couple of small nits. |
e0a38ee
to
fde76e2
Compare
@PCManticore should be good to go now. |
Thank you @atodorov |
Detects things like
Note that in some cases this is what the author needs but my experience tells me most of the times empty string value doesn't have a special meaning.
Note2: this probably belongs to the ComparisonChecker in base.py. This checker however only supports binary comparisons so I will have to fix his limitation first. Let me know how would you like to proceed about that.