Skip to content

Commit 9e0ccf0

Browse files
committed
feat: enhance comparisons in subscript to support multiple comparators
1 parent 394fbc7 commit 9e0ccf0

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

tests/test_varname.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ def func():
273273
assert c.value == "c[b < 1 and b > 0]"
274274
c[b < 1 or b > 0] = func()
275275
assert c.value == "c[b < 1 or b > 0]"
276+
c[0 < b < 2] = func()
277+
assert c.value == "c[0 < b < 2]"
276278

277279

278280
def test_unusual():

varname/utils.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,11 @@ def node_name(
274274
str(node_name(value)) for value in node.values
275275
)
276276
if isinstance(node, ast.Compare):
277-
# When the node is identified by executing, len(ops) is always 1.
278-
# Otherwise, the node cannot be identified.
279-
assert len(node.ops) == 1
280-
return (
281-
f"{node_name(node.left)} "
282-
f"{OP2SYMBOL[type(node.ops[0])]} "
283-
f"{node_name(node.comparators[0])}"
284-
)
277+
parts = [str(node_name(node.left))]
278+
for op, comparator in zip(node.ops, node.comparators):
279+
parts.append(OP2SYMBOL[type(op)])
280+
parts.append(str(node_name(comparator)))
281+
return " ".join(parts)
285282
if isinstance(node, ast.UnaryOp):
286283
return f"{OP2SYMBOL[type(node.op)]}{node_name(node.operand)}"
287284

0 commit comments

Comments
 (0)