Hi! While working on #3593 I found this case
value = -~-7 # -> -6
value = ~-~7 # -> -9
Why is it marked as unnecessary operator (WPS330)?
I think this code should definitely be banned, but WPS330 is not applicable here.
Maybe only for ast.IAdd operator, because it doesn't affect the value.
Can we use something like the following instead of count_unary_operator() to fix it?
# SingleUnaryOperator: TypeAlias = type[ast.UAdd] | type[ast.USub] | type[ast.Invert] | type[ast.Not]
def count_consecutive_unary_operator(
node: ast.AST,
operator: type[ast.unaryop], # I have doubts about the correctness of this annotation, but can imagine that someday we will need to count i.e. UAdd and USub together
# operator: SingleUnaryOperator, # alternative
) -> int:
"""Counts the maximum number of consecutive identical operators"""
counter = 0
max_counter = 0
parent = node
while True:
parent = get_parent(parent)
if not isinstance(parent, ast.UnaryOp):
break
if isinstance(parent.op, operator):
counter += 1
elif counter > max_counter:
max_counter = counter
counter = 0
else:
counter = 0
return max(max_counter, counter)
or in a recursive form (which one is preferred?).
What do you think about this?
Hi! While working on #3593 I found this case
Why is it marked as unnecessary operator (WPS330)?
I think this code should definitely be banned, but WPS330 is not applicable here.
Maybe only for
ast.IAddoperator, because it doesn't affect the value.Can we use something like the following instead of
count_unary_operator()to fix it?or in a recursive form (which one is preferred?).
What do you think about this?