All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 """Functions to process IPython magics with."""
3 from functools import lru_cache
6 from typing import Dict, List, Tuple, Optional
12 if sys.version_info >= (3, 10):
13 from typing import TypeGuard
15 from typing_extensions import TypeGuard
17 from black.report import NothingChanged
18 from black.output import out
21 TRANSFORMED_MAGICS = frozenset(
23 "get_ipython().run_cell_magic",
24 "get_ipython().system",
25 "get_ipython().getoutput",
26 "get_ipython().run_line_magic",
29 TOKENS_TO_IGNORE = frozenset(
40 NON_PYTHON_CELL_MAGICS = frozenset(
56 TOKEN_HEX = secrets.token_hex
59 @dataclasses.dataclass(frozen=True)
66 def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool:
68 import IPython # noqa:F401
69 import tokenize_rt # noqa:F401
70 except ModuleNotFoundError:
71 if verbose or not quiet:
73 "Skipping .ipynb files as Jupyter dependencies are not installed.\n"
74 "You can fix this by running ``pip install black[jupyter]``"
82 def remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
83 """Remove trailing semicolon from Jupyter notebook cell.
87 fig, ax = plt.subplots()
88 ax.plot(x_data, y_data); # plot data
92 fig, ax = plt.subplots()
93 ax.plot(x_data, y_data) # plot data
95 Mirrors the logic in `quiet` from `IPython.core.displayhook`, but uses
96 ``tokenize_rt`` so that round-tripping works fine.
98 from tokenize_rt import (
104 tokens = src_to_tokens(src)
105 trailing_semicolon = False
106 for idx, token in reversed_enumerate(tokens):
107 if token.name in TOKENS_TO_IGNORE:
109 if token.name == "OP" and token.src == ";":
111 trailing_semicolon = True
113 if not trailing_semicolon:
115 return tokens_to_src(tokens), True
118 def put_trailing_semicolon_back(src: str, has_trailing_semicolon: bool) -> str:
119 """Put trailing semicolon back if cell originally had it.
121 Mirrors the logic in `quiet` from `IPython.core.displayhook`, but uses
122 ``tokenize_rt`` so that round-tripping works fine.
124 if not has_trailing_semicolon:
126 from tokenize_rt import src_to_tokens, tokens_to_src, reversed_enumerate
128 tokens = src_to_tokens(src)
129 for idx, token in reversed_enumerate(tokens):
130 if token.name in TOKENS_TO_IGNORE:
132 tokens[idx] = token._replace(src=token.src + ";")
134 else: # pragma: nocover
135 raise AssertionError(
136 "INTERNAL ERROR: Was not able to reinstate trailing semicolon. "
137 "Please report a bug on https://github.com/psf/black/issues. "
139 return str(tokens_to_src(tokens))
142 def mask_cell(src: str) -> Tuple[str, List[Replacement]]:
143 """Mask IPython magics so content becomes parseable Python code.
155 The replacements are returned, along with the transformed code.
157 replacements: List[Replacement] = []
161 # Might have IPython magics, will process below.
164 # Syntax is fine, nothing to mask, early return.
165 return src, replacements
167 from IPython.core.inputtransformer2 import TransformerManager
169 transformer_manager = TransformerManager()
170 transformed = transformer_manager.transform_cell(src)
171 transformed, cell_magic_replacements = replace_cell_magics(transformed)
172 replacements += cell_magic_replacements
173 transformed = transformer_manager.transform_cell(transformed)
174 transformed, magic_replacements = replace_magics(transformed)
175 if len(transformed.splitlines()) != len(src.splitlines()):
176 # Multi-line magic, not supported.
178 replacements += magic_replacements
179 return transformed, replacements
182 def get_token(src: str, magic: str) -> str:
183 """Return randomly generated token to mask IPython magic with.
185 For example, if 'magic' was `%matplotlib inline`, then a possible
186 token to mask it with would be `"43fdd17f7e5ddc83"`. The token
187 will be the same length as the magic, and we make sure that it was
188 not already present anywhere else in the cell.
191 nbytes = max(len(magic) // 2 - 1, 1)
192 token = TOKEN_HEX(nbytes)
195 token = TOKEN_HEX(nbytes)
198 raise AssertionError(
199 "INTERNAL ERROR: Black was not able to replace IPython magic. "
200 "Please report a bug on https://github.com/psf/black/issues. "
201 f"The magic might be helpful: {magic}"
203 if len(token) + 2 < len(magic):
208 def replace_cell_magics(src: str) -> Tuple[str, List[Replacement]]:
209 """Replace cell magic with token.
211 Note that 'src' will already have been processed by IPython's
212 TransformerManager().transform_cell.
216 get_ipython().run_cell_magic('t', '-n1', 'ls =!ls\\n')
223 The replacement, along with the transformed code, is returned.
225 replacements: List[Replacement] = []
227 tree = ast.parse(src)
229 cell_magic_finder = CellMagicFinder()
230 cell_magic_finder.visit(tree)
231 if cell_magic_finder.cell_magic is None:
232 return src, replacements
233 if cell_magic_finder.cell_magic.header.split()[0] in NON_PYTHON_CELL_MAGICS:
235 mask = get_token(src, cell_magic_finder.cell_magic.header)
236 replacements.append(Replacement(mask=mask, src=cell_magic_finder.cell_magic.header))
237 return f"{mask}\n{cell_magic_finder.cell_magic.body}", replacements
240 def replace_magics(src: str) -> Tuple[str, List[Replacement]]:
241 """Replace magics within body of cell.
243 Note that 'src' will already have been processed by IPython's
244 TransformerManager().transform_cell.
248 get_ipython().run_line_magic('matplotlib', 'inline')
256 The replacement, along with the transformed code, are returned.
259 magic_finder = MagicFinder()
260 magic_finder.visit(ast.parse(src))
262 for i, line in enumerate(src.splitlines(), start=1):
263 if i in magic_finder.magics:
264 offsets_and_magics = magic_finder.magics[i]
265 if len(offsets_and_magics) != 1: # pragma: nocover
266 raise AssertionError(
267 f"Expecting one magic per line, got: {offsets_and_magics}\n"
268 "Please report a bug on https://github.com/psf/black/issues."
270 col_offset, magic = (
271 offsets_and_magics[0].col_offset,
272 offsets_and_magics[0].magic,
274 mask = get_token(src, magic)
275 replacements.append(Replacement(mask=mask, src=magic))
276 line = line[:col_offset] + mask
277 new_srcs.append(line)
278 return "\n".join(new_srcs), replacements
281 def unmask_cell(src: str, replacements: List[Replacement]) -> str:
282 """Remove replacements from cell.
294 for replacement in replacements:
295 src = src.replace(replacement.mask, replacement.src)
299 def _is_ipython_magic(node: ast.expr) -> TypeGuard[ast.Attribute]:
300 """Check if attribute is IPython magic.
302 Note that the source of the abstract syntax tree
303 will already have been processed by IPython's
304 TransformerManager().transform_cell.
307 isinstance(node, ast.Attribute)
308 and isinstance(node.value, ast.Call)
309 and isinstance(node.value.func, ast.Name)
310 and node.value.func.id == "get_ipython"
314 @dataclasses.dataclass(frozen=True)
320 @dataclasses.dataclass
321 class CellMagicFinder(ast.NodeVisitor):
324 Note that the source of the abstract syntax tree
325 will already have been processed by IPython's
326 TransformerManager().transform_cell.
332 would have been transformed to
334 get_ipython().run_cell_magic('time', '', 'foo()\\n')
336 and we look for instances of the latter.
339 cell_magic: Optional[CellMagic] = None
341 def visit_Expr(self, node: ast.Expr) -> None:
342 """Find cell magic, extract header and body."""
344 isinstance(node.value, ast.Call)
345 and _is_ipython_magic(node.value.func)
346 and node.value.func.attr == "run_cell_magic"
349 for arg in node.value.args:
350 assert isinstance(arg, ast.Str)
352 header = f"%%{args[0]}"
354 header += f" {args[1]}"
355 self.cell_magic = CellMagic(header=header, body=args[2])
356 self.generic_visit(node)
359 @dataclasses.dataclass(frozen=True)
360 class OffsetAndMagic:
365 @dataclasses.dataclass
366 class MagicFinder(ast.NodeVisitor):
367 """Visit cell to look for get_ipython calls.
369 Note that the source of the abstract syntax tree
370 will already have been processed by IPython's
371 TransformerManager().transform_cell.
377 would have been transformed to
379 get_ipython().run_line_magic('matplotlib', 'inline')
381 and we look for instances of the latter (and likewise for other
385 magics: Dict[int, List[OffsetAndMagic]] = dataclasses.field(
386 default_factory=lambda: collections.defaultdict(list)
389 def visit_Assign(self, node: ast.Assign) -> None:
390 """Look for system assign magics.
394 black_version = !black --version
396 would have been transformed to
398 black_version = get_ipython().getoutput('black --version')
400 and we look for instances of the latter.
403 isinstance(node.value, ast.Call)
404 and _is_ipython_magic(node.value.func)
405 and node.value.func.attr == "getoutput"
408 for arg in node.value.args:
409 assert isinstance(arg, ast.Str)
413 self.magics[node.value.lineno].append(
414 OffsetAndMagic(node.value.col_offset, src)
416 self.generic_visit(node)
418 def visit_Expr(self, node: ast.Expr) -> None:
419 """Look for magics in body of cell.
428 would (respectively) get transformed to
430 get_ipython().system('ls')
431 get_ipython().getoutput('ls')
432 get_ipython().run_line_magic('pinfo', 'ls')
433 get_ipython().run_line_magic('pinfo2', 'ls')
435 and we look for instances of any of the latter.
437 if isinstance(node.value, ast.Call) and _is_ipython_magic(node.value.func):
439 for arg in node.value.args:
440 assert isinstance(arg, ast.Str)
443 if node.value.func.attr == "run_line_magic":
444 if args[0] == "pinfo":
446 elif args[0] == "pinfo2":
451 assert src is not None
453 elif node.value.func.attr == "system":
455 elif node.value.func.attr == "getoutput":
458 raise NothingChanged # unsupported magic.
459 self.magics[node.value.lineno].append(
460 OffsetAndMagic(node.value.col_offset, src)
462 self.generic_visit(node)