wemake-python-styleguide: Forbid slow `in` inside comprehensions
Rule request
Thesis
len with comprehensions
Iterators have no len, and sometimes I forgetting it.
Bad:
len(1 for el in a if el in b)
Better:
len([1 for el in a if el in b])
in inside comprehensions
Good:
sum(1 for el in a if el in b)
Twice slower, but also ok:
sum(el in b for el in a)
Reasoning
Detect runtime TypeError in advance. We could also detect len from yield-like iterators, but resolving symbols in python always is a non-trivial thing, unfortunately.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 16 (14 by maintainers)
Would like to take this! Can u plz assign this to me
@ManishAradwad you need to create a new violation in
best_practices, then we can create a new visitor here: https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/visitors/ast/loops.pyWe need to visit:
ast.ListCompast.SetCompast.DictCompast.GeneratorExpYou can add a new method in the visitor:
self._check_slow_in_expression(node)Here’s how our bad node (
(a > 0 for a in elements)) looks like (just one example):Then you write the required logic, test it, and submit a PR. I am here to help.