From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Tue, 27 Jun 2023 00:47:55 +0000 (-0700) Subject: Decrease cost of ipynb code path when unneeded (#3748) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/31b3b6701d2cfae072900f9d45dc8f1737ab282b?ds=sidebyside;hp=e1036119f264a846bb049fad8404df318bc2f455 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. --- 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 = (