Skip to content

Commit 8d3d26b

Browse files
authored
Allow package names with env markers with pip binary options (#2853)
Fixes #2814 (comment)
1 parent b8e47ce commit 8d3d26b

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

docs/changelog/2814.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow using package names with env markers for pip's ``--no-binary`` and ``--only-binary`` options - by :user:`q0w`.

src/tox/tox_env/python/pip/req/args.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from argparse import Action, ArgumentParser, ArgumentTypeError, Namespace
66
from typing import IO, Any, NoReturn, Sequence
77

8+
from tox.tox_env.python.pip.req.util import handle_binary_option
9+
810

911
class _OurArgumentParser(ArgumentParser):
1012
def print_usage(self, file: IO[str] | None = None) -> None: # noqa: U100
@@ -33,8 +35,8 @@ def _global_options(parser: ArgumentParser) -> None:
3335
parser.add_argument("-r", "--requirement", action=AddUniqueAction, dest="requirements")
3436
parser.add_argument("-e", "--editable", action=AddUniqueAction, dest="editables")
3537
parser.add_argument("-f", "--find-links", action=AddUniqueAction)
36-
parser.add_argument("--no-binary")
37-
parser.add_argument("--only-binary")
38+
parser.add_argument("--no-binary", action=BinaryAction, nargs="+")
39+
parser.add_argument("--only-binary", action=BinaryAction, nargs="+")
3840
parser.add_argument("--prefer-binary", action="store_true", default=False)
3941
parser.add_argument("--require-hashes", action="store_true", default=False)
4042
parser.add_argument("--pre", action="store_true", default=False)
@@ -90,3 +92,25 @@ def __call__(
9092
current = getattr(namespace, self.dest)
9193
if values not in current:
9294
current.append(values)
95+
96+
97+
class BinaryAction(Action):
98+
def __call__(
99+
self,
100+
parser: ArgumentParser, # noqa: U100
101+
namespace: Namespace,
102+
values: str | Sequence[Any] | None,
103+
option_string: str | None = None, # noqa: U100
104+
) -> None:
105+
if getattr(namespace, "no_binary", None) is None:
106+
namespace.no_binary = set()
107+
if getattr(namespace, "only_binary", None) is None:
108+
namespace.only_binary = set()
109+
110+
args = (
111+
(namespace.no_binary, namespace.only_binary)
112+
if self.dest == "no_binary"
113+
else (namespace.only_binary, namespace.no_binary)
114+
)
115+
assert values is not None
116+
handle_binary_option(values[0], *args)

src/tox/tox_env/python/pip/req/file.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from packaging.requirements import InvalidRequirement, Requirement
1616

1717
from .args import build_parser
18-
from .util import VCS, get_url_scheme, handle_binary_option, is_url, url_to_path
18+
from .util import VCS, get_url_scheme, is_url, url_to_path
1919

2020
# Matches environment variable-style values in '${MY_VARIABLE_1}' with the variable name consisting of only uppercase
2121
# letters, digits or the '_' (underscore). This follows the POSIX standard defined in IEEE Std 1003.1, 2013 Edition.
@@ -341,17 +341,10 @@ def _merge_option_line(self, base_opt: Namespace, opt: Namespace, filename: str)
341341
base_opt.trusted_hosts = []
342342
if host not in base_opt.trusted_hosts:
343343
base_opt.trusted_hosts.append(host)
344-
345-
no_binary = base_opt.no_binary if hasattr(base_opt, "no_binary") else set()
346-
only_binary = base_opt.only_binary if hasattr(base_opt, "only_binary") else set()
347344
if opt.no_binary:
348-
handle_binary_option(opt.no_binary, no_binary, only_binary)
345+
base_opt.no_binary = opt.no_binary
349346
if opt.only_binary:
350-
handle_binary_option(opt.only_binary, only_binary, no_binary)
351-
if no_binary:
352-
base_opt.no_binary = no_binary
353-
if only_binary:
354-
base_opt.only_binary = only_binary
347+
base_opt.only_binary = opt.only_binary
355348

356349
@staticmethod
357350
def _break_args_options(line: str) -> tuple[str, str]:

tests/tox_env/python/pip/req/test_file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@
177177
["--no-binary", {"foo"}],
178178
id="no-binary-none-first",
179179
),
180+
pytest.param(
181+
"--only-binary foo; sys_platform == 'aix'",
182+
{"only_binary": {"foo;"}},
183+
[],
184+
["--only-binary", {"foo;"}],
185+
id="only-binary-and-env-marker",
186+
),
180187
pytest.param("####### example-requirements.txt #######", {}, [], [], id="comment"),
181188
pytest.param("\t##### Requirements without Version Specifiers ######", {}, [], [], id="tab and comment"),
182189
pytest.param(" # start", {}, [], [], id="space and comment"),

0 commit comments

Comments
 (0)