4343 ast .MatMult : "__matmul__" ,
4444}
4545
46+ OP2SYMBOL = {
47+ # BinOp
48+ ast .Add : "+" ,
49+ ast .Sub : "-" ,
50+ ast .Mult : "*" ,
51+ ast .Div : "/" ,
52+ ast .FloorDiv : "//" ,
53+ ast .Mod : "%" ,
54+ ast .Pow : "**" ,
55+ ast .LShift : "<<" ,
56+ ast .RShift : ">>" ,
57+ ast .BitOr : "|" ,
58+ ast .BitXor : "^" ,
59+ ast .BitAnd : "&" ,
60+ ast .MatMult : "@" ,
61+ # BoolOp
62+ ast .And : "and" ,
63+ ast .Or : "or" ,
64+ # UnaryOp
65+ ast .UAdd : "+" ,
66+ ast .USub : "-" ,
67+ ast .Invert : "~" ,
68+ ast .Not : "not " ,
69+ # Compare
70+ ast .Eq : "==" ,
71+ ast .NotEq : "!=" ,
72+ ast .Lt : "<" ,
73+ ast .LtE : "<=" ,
74+ ast .Gt : ">" ,
75+ ast .GtE : ">=" ,
76+ ast .Is : "is" ,
77+ ast .IsNot : "is not" ,
78+ ast .In : "in" ,
79+ ast .NotIn : "not in" ,
80+ }
81+
4682CMP2MAGIC = {
4783 ast .Eq : "__eq__" ,
4884 ast .NotEq : "__ne__" ,
@@ -229,6 +265,25 @@ def node_name(
229265 if node .upper is not None
230266 else ":"
231267 )
268+ if isinstance (node , ast .BinOp ):
269+ return (
270+ f"{ node_name (node .left )} { OP2SYMBOL [type (node .op )]} { node_name (node .right )} "
271+ )
272+ if isinstance (node , ast .BoolOp ):
273+ return f" { OP2SYMBOL [type (node .op )]} " .join (
274+ node_name (value ) for value in node .values
275+ )
276+ 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+ )
285+ if isinstance (node , ast .UnaryOp ):
286+ return f"{ OP2SYMBOL [type (node .op )]} { node_name (node .operand )} "
232287
233288 name = type (node ).__name__
234289 if isinstance (node , ast .Subscript ):
@@ -245,7 +300,9 @@ def node_name(
245300 " - ast.List (e.g. [x, y, z])\n "
246301 " - ast.Tuple (e.g. (x, y, z))\n "
247302 " - ast.Starred (e.g. *x)\n "
248- " - ast.Subscript with slice of the above nodes (e.g. x[y])"
303+ " - ast.Subscript with slice of the above nodes (e.g. x[y])\n "
304+ " - ast.Subscript with ast.BinOp/ast.BoolOp/ast.Compare/ast.UnaryOp "
305+ "(e.g. x[y + 1], x[y and z])"
249306 )
250307
251308
0 commit comments