Skip to content

Commit

Permalink
Decrease cost of ipynb code path when unneeded (psf#3748)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hauntsaninja authored and SWHL committed Jun 27, 2023
1 parent 18b9262 commit c66c04b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -47,6 +47,8 @@

<!-- Changes that improve Black's performance. -->

- Avoid importing `IPython` in a case where we wouldn't need it (#3748)

### Output

<!-- Changes to Black's terminal output and error messages -->
Expand Down
7 changes: 6 additions & 1 deletion src/black/handle_ipynb_magics.py
Expand Up @@ -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 = (
Expand Down

0 comments on commit c66c04b

Please sign in to comment.