def visit_Compare(self, node):
if not self.config.constant_folding:
return
if len(node.ops) != 1:
# FIXME: implement 1 < 2 < 3
return
if len(node.comparators) != 1:
# FIXME: support this case? What's the syntax of this case?
return
new_node = self.compare_cst(node)
if new_node is not None:
return new_node
# replace 'None is None' with True
if (isinstance(node.ops[0], (ast.Is, ast.IsNot))
and isinstance(node.left, ast.Constant)
and node.left.value is None
and isinstance(node.comparators[0], ast.Constant)
and node.comparators[0].value is None):
result = isinstance(node.ops[0], ast.Is)
return self.new_constant(node, result)
# replace 'x in {1, 2}' with 'x in frozenset({1, 2})'
# replace 'x in [1, 2]' with 'x in frozenset((1, 2))'
if isinstance(node.ops[0], ast.In):
new_node = self.compare_contains(node)
if new_node is not None:
return new_node
评论列表
文章目录