(self, node)
| 446 | return '<could not convert AST to source>' |
| 447 | |
| 448 | def visit(self, node): |
| 449 | if not isinstance(node, gast.AST): |
| 450 | # This is not that uncommon a mistake: various node bodies are lists, for |
| 451 | # example, posing a land mine for transformers that need to recursively |
| 452 | # call `visit`. The error needs to be raised before the exception handler |
| 453 | # below is installed, because said handler will mess up if `node` is not, |
| 454 | # in fact, a node. |
| 455 | msg = ('invalid value for "node": expected "ast.AST", got "{}"; to' |
| 456 | ' visit lists of nodes, use "visit_block" instead').format( |
| 457 | type(node)) |
| 458 | raise ValueError(msg) |
| 459 | |
| 460 | did_enter_function = False |
| 461 | local_scope_size_at_entry = len(self._local_scope_state) |
| 462 | processing_expr_node = False |
| 463 | |
| 464 | parent_origin = self.ctx.current_origin |
| 465 | if isinstance(node, (gast.FunctionDef, gast.ClassDef, gast.Lambda)): |
| 466 | did_enter_function = True |
| 467 | elif isinstance(node, gast.Expr): |
| 468 | processing_expr_node = True |
| 469 | |
| 470 | if did_enter_function: |
| 471 | self._enclosing_entities.append(node) |
| 472 | |
| 473 | if anno.hasanno(node, anno.Basic.ORIGIN): |
| 474 | self.ctx.current_origin = anno.getanno(node, anno.Basic.ORIGIN) |
| 475 | |
| 476 | if processing_expr_node: |
| 477 | entry_expr_value = node.value |
| 478 | |
| 479 | if not anno.hasanno(node, anno.Basic.SKIP_PROCESSING): |
| 480 | result = super(Base, self).visit(node) |
| 481 | self.ctx.current_origin = parent_origin |
| 482 | |
| 483 | # Adjust for consistency: replacing the value of an Expr with |
| 484 | # an Assign node removes the need for the Expr node. |
| 485 | if processing_expr_node: |
| 486 | if isinstance(result, gast.Expr) and result.value != entry_expr_value: |
| 487 | # When the replacement is a list, it is assumed that the list came |
| 488 | # from a template that contained a number of statements, which |
| 489 | # themselves are standalone and don't require an enclosing Expr. |
| 490 | if isinstance(result.value, |
| 491 | (list, tuple, gast.Assign, gast.AugAssign)): |
| 492 | result = result.value |
| 493 | |
| 494 | # By default, all replacements receive the origin info of the replaced node. |
| 495 | if result is not node and result is not None: |
| 496 | nodes_to_adjust = result |
| 497 | if isinstance(result, (list, tuple)): |
| 498 | nodes_to_adjust = result |
| 499 | else: |
| 500 | nodes_to_adjust = (result,) |
| 501 | for n in nodes_to_adjust: |
| 502 | if not anno.hasanno(n, anno.Basic.ORIGIN): |
| 503 | inherited_origin = anno.getanno( |
| 504 | node, anno.Basic.ORIGIN, default=parent_origin) |
| 505 | if inherited_origin is not None: |
no test coverage detected