Skip to content

Commit 1f40020

Browse files
authored
Properly lazily import shutil (#3023)
2 parents 4f936ac + 6f6cbbb commit 1f40020

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/click/_termui_impl.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from gettext import gettext as _
1818
from io import StringIO
1919
from pathlib import Path
20-
from shutil import which
2120
from types import TracebackType
2221

2322
from ._compat import _default_text_stdout
@@ -235,8 +234,6 @@ def format_progress_line(self) -> str:
235234
).rstrip()
236235

237236
def render_progress(self) -> None:
238-
import shutil
239-
240237
if self.hidden:
241238
return
242239

@@ -250,6 +247,8 @@ def render_progress(self) -> None:
250247
buf = []
251248
# Update width in case the terminal has been resized
252249
if self.autowidth:
250+
import shutil
251+
253252
old_width = self.width
254253
self.width = 0
255254
clutter_length = term_len(self.format_progress_line())
@@ -421,10 +420,13 @@ def _pipepager(
421420
# Split the command into the invoked CLI and its parameters.
422421
if not cmd_parts:
423422
return False
423+
424+
import shutil
425+
424426
cmd = cmd_parts[0]
425427
cmd_params = cmd_parts[1:]
426428

427-
cmd_filepath = which(cmd)
429+
cmd_filepath = shutil.which(cmd)
428430
if not cmd_filepath:
429431
return False
430432
# Resolves symlinks and produces a normalized absolute path string.
@@ -510,9 +512,12 @@ def _tempfilepager(
510512
# Split the command into the invoked CLI and its parameters.
511513
if not cmd_parts:
512514
return False
515+
516+
import shutil
517+
513518
cmd = cmd_parts[0]
514519

515-
cmd_filepath = which(cmd)
520+
cmd_filepath = shutil.which(cmd)
516521
if not cmd_filepath:
517522
return False
518523
# Resolves symlinks and produces a normalized absolute path string.
@@ -573,6 +578,9 @@ def get_editor(self) -> str:
573578
return rv
574579
if WIN:
575580
return "notepad"
581+
582+
from shutil import which
583+
576584
for editor in "sensible-editor", "vim", "nano":
577585
if which(editor) is not None:
578586
return editor

src/click/formatting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ def __init__(
119119
width: int | None = None,
120120
max_width: int | None = None,
121121
) -> None:
122-
import shutil
123-
124122
self.indent_increment = indent_increment
125123
if max_width is None:
126124
max_width = 80
127125
if width is None:
126+
import shutil
127+
128128
width = FORCED_WIDTH
129129
if width is None:
130130
width = max(min(shutil.get_terminal_size().columns, max_width) - 2, 50)

src/click/testing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io
66
import os
77
import shlex
8-
import shutil
98
import sys
109
import tempfile
1110
import typing as t
@@ -571,6 +570,8 @@ def isolated_filesystem(
571570
os.chdir(cwd)
572571

573572
if temp_dir is None:
573+
import shutil
574+
574575
try:
575576
shutil.rmtree(dt)
576577
except OSError:

tests/test_imports.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def tracking_import(module, locals=None, globals=None, fromlist=None,
4343
"itertools",
4444
"os",
4545
"re",
46-
"shutil",
4746
"stat",
4847
"struct",
4948
"sys",

0 commit comments

Comments
 (0)