Avoid importing IPython
if notebook cells do not contain magics (#3782)
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
parent
0e26ada66d
commit
92e0f5b965
@ -30,6 +30,8 @@
|
|||||||
|
|
||||||
<!-- Changes that improve Black's performance. -->
|
<!-- Changes that improve Black's performance. -->
|
||||||
|
|
||||||
|
- Avoid importing `IPython` if notebook cells do not contain magics (#3782)
|
||||||
|
|
||||||
### Output
|
### Output
|
||||||
|
|
||||||
<!-- Changes to Black's terminal output and error messages -->
|
<!-- Changes to Black's terminal output and error messages -->
|
||||||
|
@ -668,7 +668,7 @@ def get_sources(
|
|||||||
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
|
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
|
||||||
|
|
||||||
if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
|
if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
|
||||||
verbose=verbose, quiet=quiet
|
warn=verbose or not quiet
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -384,7 +384,7 @@ def gen_python_files(
|
|||||||
|
|
||||||
elif child.is_file():
|
elif child.is_file():
|
||||||
if child.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
|
if child.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
|
||||||
verbose=verbose, quiet=quiet
|
warn=verbose or not quiet
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
include_match = include.search(normalized_path) if include else True
|
include_match = include.search(normalized_path) if include else True
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
import secrets
|
import secrets
|
||||||
import sys
|
import sys
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
from importlib.util import find_spec
|
||||||
from typing import Dict, List, Optional, Tuple
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
if sys.version_info >= (3, 10):
|
if sys.version_info >= (3, 10):
|
||||||
@ -56,25 +57,17 @@ class Replacement:
|
|||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool:
|
def jupyter_dependencies_are_installed(*, warn: bool) -> bool:
|
||||||
try:
|
installed = (
|
||||||
# isort: off
|
find_spec("tokenize_rt") is not None and find_spec("IPython") is not None
|
||||||
# tokenize_rt is less commonly installed than IPython
|
)
|
||||||
# and IPython is expensive to import
|
if not installed and warn:
|
||||||
import tokenize_rt # noqa:F401
|
msg = (
|
||||||
import IPython # noqa:F401
|
"Skipping .ipynb files as Jupyter dependencies are not installed.\n"
|
||||||
|
'You can fix this by running ``pip install "black[jupyter]"``'
|
||||||
# isort: on
|
)
|
||||||
except ModuleNotFoundError:
|
out(msg)
|
||||||
if verbose or not quiet:
|
return installed
|
||||||
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 remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
|
def remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
|
||||||
|
@ -440,17 +440,13 @@ def test_cache_isnt_written_if_no_jupyter_deps_single(
|
|||||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||||
tmp_nb = tmp_path / "notebook.ipynb"
|
tmp_nb = tmp_path / "notebook.ipynb"
|
||||||
tmp_nb.write_bytes(nb.read_bytes())
|
tmp_nb.write_bytes(nb.read_bytes())
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr("black.jupyter_dependencies_are_installed", lambda warn: False)
|
||||||
"black.jupyter_dependencies_are_installed", lambda verbose, quiet: False
|
|
||||||
)
|
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
|
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
|
assert "No Python files are present to be formatted. Nothing to do" in result.output
|
||||||
jupyter_dependencies_are_installed.cache_clear()
|
jupyter_dependencies_are_installed.cache_clear()
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr("black.jupyter_dependencies_are_installed", lambda warn: True)
|
||||||
"black.jupyter_dependencies_are_installed", lambda verbose, quiet: True
|
|
||||||
)
|
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
|
main, [str(tmp_path / "notebook.ipynb"), f"--config={EMPTY_CONFIG}"]
|
||||||
)
|
)
|
||||||
@ -466,13 +462,13 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
|
|||||||
tmp_nb = tmp_path / "notebook.ipynb"
|
tmp_nb = tmp_path / "notebook.ipynb"
|
||||||
tmp_nb.write_bytes(nb.read_bytes())
|
tmp_nb.write_bytes(nb.read_bytes())
|
||||||
monkeypatch.setattr(
|
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}"])
|
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
|
assert "No Python files are present to be formatted. Nothing to do" in result.output
|
||||||
jupyter_dependencies_are_installed.cache_clear()
|
jupyter_dependencies_are_installed.cache_clear()
|
||||||
monkeypatch.setattr(
|
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}"])
|
result = runner.invoke(main, [str(tmp_path), f"--config={EMPTY_CONFIG}"])
|
||||||
assert "reformatted" in result.output
|
assert "reformatted" in result.output
|
||||||
|
Loading…
Reference in New Issue
Block a user