Closed tekumara closed 8 months ago
Could also suggest calling set on an iterable before checking membership? e.g.
for foo in iterable_of_foos:
if foo in bar_iterable:
do_something(foo)
->
bar_set = set(bar_iterable)
for foo in iterable_of_foos:
if foo in bar_set:
do_something(foo)
@tekumara that’s already an optional checker ini pylint: Set Membership checker.
Oh right thank you, I don't use pylint so wasn't aware it had this check. If possible, could perflint include this too so all the performance related checks are together?
@tekumara have you also looked at ruff
’s performance lint implementation here?
oh yes! I'm using ruff rather than pylint or perflint directly 🙂
I thought to raise the issue here, as this is the upstream. Once implemented here, I'd hope ruff would implement it too.
Well, I think @tonybaloney can find inspiration in pylint
’s set_membership.py module.
Although, looking at the inactivity of this repo over the past months I do worry that this package is getting stale… 🤔
Well, I think @tonybaloney can find inspiration in
pylint
’s set_membership.py module.Although, looking at the inactivity of this repo over the past months I do worry that this package is getting stale… 🤔
Sorry. I'm on a long overdue break. This is on my list for next week
@tonybaloney oh that’s great to know 👍🏼 Enjoy your break!
perflint is a plugin to pylint so you can run both together. I don't want to duplicate rules that already exist in pylint
https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/use-set-for-membership.html closing. thanks for the idea
Suggest a set when checking membership of a list/tuple of hashable values.
eg:
rather than
or
See also https://docs.sourcery.ai/Reference/Python/Default-Rules/collection-into-set/