MCPcopy Index your code
hub / github.com/python/cpython / test_node_replace

Method test_node_replace

Lib/test/test_ast/test_ast.py:3158–3183  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3156 self.assertASTTransformation(PrintToLog, code, expected)
3157
3158 def test_node_replace(self):
3159 code = """
3160 def func(arg):
3161 print(arg)
3162 """
3163 expected = """
3164 def func(arg):
3165 logger.log(arg, debug=True)
3166 """
3167
3168 class PrintToLog(ast.NodeTransformer):
3169 def visit_Call(self, node: ast.Call):
3170 self.generic_visit(node)
3171 if isinstance(node.func, ast.Name) and node.func.id == 'print':
3172 return ast.Call(
3173 func=ast.Attribute(
3174 ast.Name('logger', ctx=ast.Load()),
3175 attr='log',
3176 ctx=ast.Load(),
3177 ),
3178 args=node.args,
3179 keywords=[ast.keyword('debug', ast.Constant(True))],
3180 )
3181 return node
3182
3183 self.assertASTTransformation(PrintToLog, code, expected)
3184
3185
3186class ASTConstructorTests(unittest.TestCase):

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected