Skip to content

Commit 115b870

Browse files
committed
config: use intermixed arg parsing also in parse_known_and_unknown_args
Missed in fc2d649; we should be consistent here.
1 parent 7ba4d69 commit 115b870

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/_pytest/config/argparsing.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Mapping
77
from collections.abc import Sequence
88
import os
9+
import sys
910
from typing import Any
1011
from typing import final
1112
from typing import Literal
@@ -164,7 +165,17 @@ def parse_known_and_unknown_args(
164165
"""
165166
optparser = self._getparser()
166167
strargs = [os.fspath(x) for x in args]
167-
return optparser.parse_known_args(strargs, namespace=namespace)
168+
if sys.version_info < (3, 12):
169+
# Older argparse have a bugged parse_known_intermixed_args.
170+
namespace, unknown = self.optparser.parse_known_args(strargs, namespace)
171+
assert namespace is not None
172+
file_or_dir = getattr(namespace, FILE_OR_DIR)
173+
unknown_flags: list[str] = []
174+
for arg in unknown:
175+
(unknown_flags if arg.startswith("-") else file_or_dir).append(arg)
176+
return namespace, unknown_flags
177+
else:
178+
return optparser.parse_known_intermixed_args(strargs, namespace)
168179

169180
def addini(
170181
self,

testing/test_parseopt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ def test_parse_known_args(self, parser: parseopt.Parser) -> None:
142142
parser.parse_known_args([Path(".")])
143143
parser.addoption("--hello", action="store_true")
144144
ns = parser.parse_known_args(["x", "--y", "--hello", "this"])
145-
assert ns.hello
146-
assert ns.file_or_dir == ["x"]
145+
assert ns.hello is True
146+
assert ns.file_or_dir == ["x", "this"]
147147

148148
def test_parse_known_and_unknown_args(self, parser: parseopt.Parser) -> None:
149149
parser.addoption("--hello", action="store_true")
150150
ns, unknown = parser.parse_known_and_unknown_args(
151151
["x", "--y", "--hello", "this"]
152152
)
153-
assert ns.hello
154-
assert ns.file_or_dir == ["x"]
155-
assert unknown == ["--y", "this"]
153+
assert ns.hello is True
154+
assert ns.file_or_dir == ["x", "this"]
155+
assert unknown == ["--y"]
156156

157157
def test_parse_will_set_default(self, parser: parseopt.Parser) -> None:
158158
parser.addoption("--hello", dest="hello", default="x", action="store")

0 commit comments

Comments
 (0)