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
Add use-set-for-membership
check
#4841
Conversation
Pull Request Test Coverage Report for Build 1181150804
💛 - Coveralls |
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.
We could make this a default checker I think. I'm not merging immediately so it can be discussed. It's going in before we release 2.10.0 either way.
The numerous warning in pylint's code are an opportunity to check the performance improvement that this checker really bring :D |
d674e60
to
6e0dd4a
Compare
While fixing the existing code, I came across something we haven't yet considered. Here, pylint would currently print a warning for L1419. However, replacing the tuple with a
-- Suggestions going forwardWe can only be certain if all items in a list / tuple are in-place defined constants ( As the performance benefit from this change would be minimal, I would suggest not to move forward with it. Furthermore, we could add a new refactoring check |
Maybe we can check that the |
True, but it is possible. Python is a dynamic language after all.
I don't think even a very few false positives are worth it considering that the change provides only a minimal performance benefit in practice (if any at all). |
Implementing it for collections containing string literals (probably a very common case) would bring a notable benefit as string hashes are cached in the string structure, especially in successive if in/elif in/... constructs.
|
As pylint doesn't support control flow inference, we can't reliably use infer for it. Thus this would be limited to in-place strings, ints, and so on. Still believe the effort wouldn't be worth it here, unfortunately. -- |
I moved that to 2.11 so we do not delay 2.10 while we discuss this. |
I opened #4853 which extends |
6e0dd4a
to
ab6dd5d
Compare
Perhaps I am mistaken but isn't this impossible?
Although the order might need to be switched, as I think performance-wise it will be better to perform check 3 as the first step. This would mean that checking for pre-defined variables such as on line 4 of the tests ( |
Of course at runtime no new items will be added. That doesn't however consider if you later change the code itself. |
Hmm, I would argue that that is no reason not to add this check. |
Of course that would be an option. I believe I even suggested it somewhere already. |
fce9190
to
a504e1a
Compare
a504e1a
to
aba9473
Compare
ffb4d16
to
f959999
Compare
I've moved the code to a new extension: |
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.
Nice 👌 Just waiting for the hot fix for 2.10 to dry before merging.
@Pierre-Sassoulas Should we create a new label for it? Something like |
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.
Do you think we should handle the following ?
class Hashable:
pass
class NotHashable:
def __eq__(self, other):
return self == other
class HashableAgain:
def __eq__(self, other):
return self == other
def __hash__(self):
return id(self)
x in (1, Hashable, HashableAgain) # [use-set-for-membership]
x in (1, NotHashable) # List is not hashable
If someone likes to contribute it, that would certainly be an option. I think this should go in another PR though. This one only adds a basic check for constants. |
Type of Changes
Description
Add new
use-set-for-membership
check following the discussion in #4776.For the moment I've chosen to add it to the
CodeStyleChecker
, thus not enabling it by default.However this is certainly something that might be worth having a discussion about.
Closes #4776
--
/CC: @Pierre-Sassoulas, @samueltardieu