From 98304e0732f3a8215d479f18e139e986303b3f33 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 15:03:40 -0700 Subject: [PATCH 1/6] Decrease cost of ipynb code path when unneeded 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. --- src/black/handle_ipynb_magics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 9e1af757c32..451bfc803b9 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -58,8 +58,8 @@ class Replacement: @lru_cache() def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool: try: - import IPython # noqa:F401 import tokenize_rt # noqa:F401 + import IPython # noqa:F401 except ModuleNotFoundError: if verbose or not quiet: msg = ( From 2c270a4ae3cdc1c8424d806769997a31d017c4e5 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 15:14:41 -0700 Subject: [PATCH 2/6] changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 460f9c95114..223a07e8ef9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -45,6 +45,8 @@ +- Avoid importing `IPython` in a case where we wouldn't need it (#3748) + ### Output From 9d866944fa9383382d7fd673780c168166969a20 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 15:15:17 -0700 Subject: [PATCH 3/6] isort --- src/black/handle_ipynb_magics.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 451bfc803b9..63799fa3513 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -58,8 +58,10 @@ class Replacement: @lru_cache() def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool: try: + # isort: off import tokenize_rt # noqa:F401 import IPython # noqa:F401 + # isort: on except ModuleNotFoundError: if verbose or not quiet: msg = ( From 0297c9c1d538f4d712f61b93966454ce0b58f0c5 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 15:16:33 -0700 Subject: [PATCH 4/6] format --- src/black/handle_ipynb_magics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 63799fa3513..51c5675032a 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -61,6 +61,7 @@ def jupyter_dependencies_are_installed(*, verbose: bool, quiet: bool) -> bool: # isort: off import tokenize_rt # noqa:F401 import IPython # noqa:F401 + # isort: on except ModuleNotFoundError: if verbose or not quiet: From 6a0b46d674457bda2279d758680c3bd977715f59 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 16:39:06 -0700 Subject: [PATCH 5/6] comment --- src/black/handle_ipynb_magics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 51c5675032a..2f57fd1c01e 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -59,6 +59,7 @@ class Replacement: 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 From 467a2c9caa8bc28a395396d2f8b9e15abb1c4ce3 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 24 Jun 2023 17:39:47 -0700 Subject: [PATCH 6/6] lint --- src/black/handle_ipynb_magics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/black/handle_ipynb_magics.py b/src/black/handle_ipynb_magics.py index 2f57fd1c01e..2b6b9209211 100644 --- a/src/black/handle_ipynb_magics.py +++ b/src/black/handle_ipynb_magics.py @@ -59,7 +59,8 @@ class Replacement: 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 + # tokenize_rt is less commonly installed than IPython + # and IPython is expensive to import import tokenize_rt # noqa:F401 import IPython # noqa:F401