Skip to content
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

Merged
merged 1 commit into from
Dec 29, 2016

Conversation

atodorov
Copy link
Contributor

Detects things like

if X == '':
    pass

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.

@rowillia
Copy link
Contributor

rowillia commented Dec 1, 2016

@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 x is not None and isinstance(x, str), it's especially handy in parsing code.

If we go forward with this one I'd like to see us turn it off by default.

@atodorov
Copy link
Contributor Author

atodorov commented Dec 1, 2016

@rowillia - others to think about are if X != 0, where X is an int, X is None, X == None, X is not None, X != None

@rowillia
Copy link
Contributor

rowillia commented Dec 2, 2016

@atodorov The len change is fairly uncontroversial. The only False-y value len can return is 0.

In these cases where we don't know the type of X, it could be perfectly valid to compare to 0 if, say, X could be None or 0. While I think it's a terrible pattern, I've frequently seen code that does things like say None means no limit, 0 means the limit is 0.

@PCManticore
Copy link
Contributor

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).

@atodorov
Copy link
Contributor Author

@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?


You can activate this checker by adding the line::

load-plugins=pylint.extensions.docparams
Copy link
Contributor

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 == ''
Copy link
Contributor

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'
Copy link
Contributor

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']:
Copy link
Contributor

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.

@PCManticore
Copy link
Contributor

@atodorov In general, it looks good, apart of a couple of small nits.

@atodorov
Copy link
Contributor Author

@PCManticore should be good to go now.

@PCManticore
Copy link
Contributor

Thank you @atodorov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants