Skip to content

Commit 6f338df

Browse files
committed
Fixes pypa#4390 by allowing explicitly provided --prefix and --target options to overrule implicitly discovered --user.
1 parent ebfa663 commit 6f338df

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/pip/_internal/cli/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ def expand_default(self, option):
108108
return optparse.IndentedHelpFormatter.expand_default(self, option)
109109

110110

111+
class CustomOption(optparse.Option):
112+
113+
def process(self, opt, value, values, parser):
114+
parser.provided_options.add(opt)
115+
return super(CustomOption, self).process(opt, value, values, parser)
116+
117+
111118
class CustomOptionParser(optparse.OptionParser):
112119

113120
def insert_option_group(self, idx, *args, **kwargs):
@@ -138,6 +145,9 @@ def __init__(self, *args, **kwargs):
138145

139146
isolated = kwargs.pop("isolated", False)
140147
self.config = Configuration(isolated)
148+
self.provided_options = set()
149+
150+
kwargs.setdefault("option_class", CustomOption)
141151

142152
assert self.name
143153
optparse.OptionParser.__init__(self, *args, **kwargs)

src/pip/_internal/commands/install.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,33 @@ def run(self, options, args):
226226
options.src_dir = os.path.abspath(options.src_dir)
227227
install_options = options.install_options or []
228228
if options.use_user_site:
229+
explicit_opt = self.parser.provided_options
230+
if "--user" not in explicit_opt:
231+
# user option was implicit, so allow it to be disabled
232+
# by explicit options
233+
if options.prefix_path and "--prefix" in explicit_opt:
234+
options.use_user_site = False
235+
if options.target_dir and "--target" in explicit_opt:
236+
options.use_user_site = False
237+
if options.use_user_site:
238+
if options.prefix_path:
239+
raise CommandError(
240+
"Can not combine '--user' and '--prefix' as they imply "
241+
"different installation locations"
242+
)
243+
if options.target_dir:
244+
raise CommandError(
245+
"Can not combine '--user' and '--target' as they imply "
246+
"different installation locations"
247+
)
248+
if virtualenv_no_global():
249+
raise InstallationError(
250+
"Can not perform a '--user' install. User site-packages "
251+
"are not visible in this virtualenv."
252+
)
253+
install_options.append('--user')
229254
if options.prefix_path:
230-
raise CommandError(
231-
"Can not combine '--user' and '--prefix' as they imply "
232-
"different installation locations"
233-
)
234-
if virtualenv_no_global():
235-
raise InstallationError(
236-
"Can not perform a '--user' install. User site-packages "
237-
"are not visible in this virtualenv."
238-
)
239-
install_options.append('--user')
240-
install_options.append('--prefix=')
255+
install_options.append('--prefix=')
241256

242257
target_temp_dir = TempDirectory(kind="target")
243258
if options.target_dir:

0 commit comments

Comments
 (0)