From 31b3b6701d2cfae072900f9d45dc8f1737ab282b Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 26 Jun 2023 17:47:55 -0700 Subject: [PATCH] Decrease cost of ipynb code path when unneeded (#3748) IPython is a very expensive import, like, at least 300ms. I'd also venture that it's much more common than tokenize-rt, which is like 30ms. I work in a repo where I use black, have IPython installed and there happen to be a couple notebooks (that we don't want formatted). I know I can force exclude ipynb, but this change doesn't really have a cost. --- CHANGES.md | 2 ++ src/black/handle_ipynb_magics.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a9d4d9d..6fa0e4b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -47,6 +47,8 @@ +- Avoid importing `IPython` in a case where we wouldn't need it (#3748) + ### Output diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 9e1af75..2b6b920 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -58,8 +58,13 @@ class Replacement: @lru_cache() def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool: try: - import IPython # noqa:F401 + # 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 = ( -- 2.39.5