Allow transparent cell magics (#8911)

## Summary

This PR updates the logic for `is_magic_cell` to include certain cell
magics. These cell magics would contain Python code following the line
defining the command. The code could define a variable which can then be
referenced in other cells. Currently, we would ignore the cell
completely leading to undefined-name violation.

As discussed in
https://github.com/astral-sh/ruff/issues/8354#issuecomment-1832221009

## Test Plan

Add new test case to validate this scenario.
This commit is contained in:
Dhruv Manilawala 2023-12-07 14:15:43 -06:00 committed by GitHub
parent 04ec11a73d
commit 6bbabceead
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 4 deletions

View file

@ -4,5 +4,10 @@
"id": "1",
"metadata": {},
"outputs": [],
"source": ["%%timeit\n", "print('hello world')"]
"source": [
"%%script bash\n",
"for i in 1 2 3; do\n",
" echo $i\n",
"done"
]
}

View file

@ -0,0 +1,11 @@
{
"execution_count": null,
"cell_type": "code",
"id": "1",
"metadata": {},
"outputs": [],
"source": [
"%%timeit\n",
"print('hello world')"
]
}

View file

@ -26,6 +26,18 @@
"%%timeit\n",
"import sys"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36dedfd1-6c03-4894-bea6-6c1687b82b3c",
"metadata": {},
"outputs": [],
"source": [
"%%random\n",
"# This cell is ignored\n",
"import pathlib"
]
}
],
"metadata": {

View file

@ -22,8 +22,19 @@
"metadata": {},
"outputs": [],
"source": [
"%%timeit\n",
"import sys"
"%%timeit"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b6d7faa-72b3-4087-8670-fe6d35e41fb6",
"metadata": {},
"outputs": [],
"source": [
"%%random\n",
"# This cell is ignored\n",
"import pathlib"
]
}
],