Look for system assign magics. For example, black_version = !black --version env = %env var would have been (respectively) transformed to black_version = get_ipython().getoutput('black --version') env = get_ipython().run_line_magic(
(self, node: ast.Assign)
| 441 | self.magics: dict[int, list[OffsetAndMagic]] = collections.defaultdict(list) |
| 442 | |
| 443 | def visit_Assign(self, node: ast.Assign) -> None: |
| 444 | """Look for system assign magics. |
| 445 | |
| 446 | For example, |
| 447 | |
| 448 | black_version = !black --version |
| 449 | env = %env var |
| 450 | |
| 451 | would have been (respectively) transformed to |
| 452 | |
| 453 | black_version = get_ipython().getoutput('black --version') |
| 454 | env = get_ipython().run_line_magic('env', 'var') |
| 455 | |
| 456 | and we look for instances of any of the latter. |
| 457 | """ |
| 458 | if isinstance(node.value, ast.Call) and _is_ipython_magic(node.value.func): |
| 459 | args = _get_str_args(node.value.args) |
| 460 | if node.value.func.attr == "getoutput": |
| 461 | src = f"!{args[0]}" |
| 462 | elif node.value.func.attr == "run_line_magic": |
| 463 | src = f"%{args[0]}" |
| 464 | if args[1]: |
| 465 | src += f" {args[1]}" |
| 466 | else: |
| 467 | raise AssertionError( |
| 468 | f"Unexpected IPython magic {node.value.func.attr!r} found. " |
| 469 | "Please report a bug on https://github.com/psf/black/issues." |
| 470 | ) from None |
| 471 | self.magics[node.value.lineno].append( |
| 472 | OffsetAndMagic(node.value.col_offset, src) |
| 473 | ) |
| 474 | self.generic_visit(node) |
| 475 | |
| 476 | def visit_Expr(self, node: ast.Expr) -> None: |
| 477 | """Look for magics in body of cell. |
nothing calls this directly
no test coverage detected