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.
summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
0e26ada)
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
<!-- Changes that improve Black's performance. -->
<!-- Changes that improve Black's performance. -->
+- Avoid importing `IPython` if notebook cells do not contain magics (#3782)
+
### Output
<!-- Changes to Black's terminal output and error messages -->
### Output
<!-- Changes to Black's terminal output and error messages -->
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
- verbose=verbose, quiet=quiet
+ warn=verbose or not quiet
elif child.is_file():
if child.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
elif child.is_file():
if child.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
- verbose=verbose, quiet=quiet
+ warn=verbose or not quiet
):
continue
include_match = include.search(normalized_path) if include else True
):
continue
include_match = include.search(normalized_path) if include else True
import secrets
import sys
from functools import lru_cache
import secrets
import sys
from functools import lru_cache
+from importlib.util import find_spec
from typing import Dict, List, Optional, Tuple
if sys.version_info >= (3, 10):
from typing import Dict, List, Optional, Tuple
if sys.version_info >= (3, 10):
-def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool:
- try:
- # isort: off
- # tokenize_rt is less commonly installed than IPython
- # and IPython is expensive to import
- import tokenize_rt # noqa:F401
- import IPython # noqa:F401
-
- # isort: on
- except ModuleNotFoundError:
- if verbose or not quiet:
- msg = (
- "Skipping .ipynb files as Jupyter dependencies are not installed.\n"
- 'You can fix this by running ``pip install "black[jupyter]"``'
- )
- out(msg)
- return False
- else:
- return True
+def jupyter_dependencies_are_installed(*, warn: bool) -> bool:
+ installed = (
+ find_spec("tokenize_rt") is not None and find_spec("IPython") is not None
+ )
+ if not installed and warn:
+ msg = (
+ "Skipping .ipynb files as Jupyter dependencies are not installed.\n"
+ 'You can fix this by running ``pip install "black[jupyter]"``'
+ )
+ out(msg)
+ return installed
def remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
def remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
tmp_nb = tmp_path / "notebook.ipynb"
tmp_nb.write_bytes(nb.read_bytes())
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
tmp_nb = tmp_path / "notebook.ipynb"
tmp_nb.write_bytes(nb.read_bytes())
- monkeypatch.setattr(
- "black.jupyter_dependencies_are_installed", lambda verbose, quiet: False
- )
+ monkeypatch.setattr("black.jupyter_dependencies_are_installed", lambda warn: False)
result = runner.invoke(
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
)
assert "No Python files are present to be formatted. Nothing to do" in result.output
jupyter_dependencies_are_installed.cache_clear()
result = runner.invoke(
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
)
assert "No Python files are present to be formatted. Nothing to do" in result.output
jupyter_dependencies_are_installed.cache_clear()
- monkeypatch.setattr(
- "black.jupyter_dependencies_are_installed", lambda verbose, quiet: True
- )
+ monkeypatch.setattr("black.jupyter_dependencies_are_installed", lambda warn: True)
result = runner.invoke(
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
)
result = runner.invoke(
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
)
tmp_nb = tmp_path / "notebook.ipynb"
tmp_nb.write_bytes(nb.read_bytes())
monkeypatch.setattr(
tmp_nb = tmp_path / "notebook.ipynb"
tmp_nb.write_bytes(nb.read_bytes())
monkeypatch.setattr(
- "black.files.jupyter_dependencies_are_installed", lambda verbose, quiet: False
+ "black.files.jupyter_dependencies_are_installed", lambda warn: False
)
result = runner.invoke(main, [str(tmp_path), f"--config={EMPTY_CONFIG}"])
assert "No Python files are present to be formatted. Nothing to do" in result.output
jupyter_dependencies_are_installed.cache_clear()
monkeypatch.setattr(
)
result = runner.invoke(main, [str(tmp_path), f"--config={EMPTY_CONFIG}"])
assert "No Python files are present to be formatted. Nothing to do" in result.output
jupyter_dependencies_are_installed.cache_clear()
monkeypatch.setattr(
- "black.files.jupyter_dependencies_are_installed", lambda verbose, quiet: True
+ "black.files.jupyter_dependencies_are_installed", lambda warn: True
)
result = runner.invoke(main, [str(tmp_path), f"--config={EMPTY_CONFIG}"])
assert "reformatted" in result.output
)
result = runner.invoke(main, [str(tmp_path), f"--config={EMPTY_CONFIG}"])
assert "reformatted" in result.output