MCPcopy
hub / github.com/psf/black / validate_cell

Function validate_cell

src/black/handle_ipynb_magics.py:64–89  ·  view source on GitHub ↗

r"""Check that cell does not already contain TransformerManager transformations, or non-Python cell magics, which might cause tokenizer_rt to break because of indentations. If a cell contains ``!ls``, then it'll be transformed to ``get_ipython().system('ls')``. However, if the cell

(src: str, mode: Mode)

Source from the content-addressed store, hash-verified

62
63
64def validate_cell(src: str, mode: Mode) -> None:
65 r"""Check that cell does not already contain TransformerManager transformations,
66 or non-Python cell magics, which might cause tokenizer_rt to break because of
67 indentations.
68
69 If a cell contains ``!ls``, then it'll be transformed to
70 ``get_ipython().system('ls')``. However, if the cell originally contained
71 ``get_ipython().system('ls')``, then it would get transformed in the same way:
72
73 >>> TransformerManager().transform_cell("get_ipython().system('ls')")
74 "get_ipython().system('ls')\n"
75 >>> TransformerManager().transform_cell("!ls")
76 "get_ipython().system('ls')\n"
77
78 Due to the impossibility of safely roundtripping in such situations, cells
79 containing transformed magics will be ignored.
80 """
81 if any(transformed_magic in src for transformed_magic in TRANSFORMED_MAGICS):
82 raise NothingChanged
83
84 line = _get_code_start(src)
85 if line.startswith("%%") and (
86 line.split(maxsplit=1)[0][2:]
87 not in PYTHON_CELL_MAGICS | mode.python_cell_magics
88 ):
89 raise NothingChanged
90
91
92def remove_trailing_semicolon(src: str) -> tuple[str, bool]:

Callers 1

format_cellFunction · 0.90

Calls 1

_get_code_startFunction · 0.85

Tested by

no test coverage detected