Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/tyro/_backends/_tyro_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ def _recurse(
add_help=add_help,
console_outputs=console_outputs,
seen_double_dash=True,
return_unknown_args=return_unknown_args,
)
continue

Expand Down Expand Up @@ -484,7 +483,6 @@ def _recurse(
local_prog,
add_help=add_help,
console_outputs=console_outputs,
return_unknown_args=return_unknown_args,
min_remaining_positional=self._min_positional_consumption(
positional_args
),
Expand Down Expand Up @@ -544,7 +542,6 @@ def _recurse(
local_prog,
add_help=add_help,
console_outputs=console_outputs,
return_unknown_args=return_unknown_args,
)
continue

Expand Down Expand Up @@ -796,7 +793,6 @@ def _consume_argument(
add_help: bool,
console_outputs: bool,
seen_double_dash: bool = False,
return_unknown_args: bool = False,
min_remaining_positional: int = 0,
):
arg_values: list[str] = []
Expand Down Expand Up @@ -838,13 +834,12 @@ def _consume_argument(
break

# To match argparse behavior, any flag-like string
# terminates when return_unknown_args is set. We check
# for a leading alpha character after stripping dashes
# to avoid treating negative numbers (like -2 or -3.14)
# terminates positional nargs consumption. We check for
# a leading alpha character after stripping dashes to
# avoid treating negative numbers (like -2 or -3.14)
# as flags.
if (
return_unknown_args
and token_key.startswith("-")
token_key.startswith("-")
and len(token_key) > 1
and token_key.lstrip("-")[:1].isalpha()
):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_positional.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pathlib
from dataclasses import dataclass
from typing import List, Optional, Sequence, Tuple

Expand Down Expand Up @@ -373,3 +374,21 @@ class Config:
result = tyro.cli(Config, args=["-x", "a", "--", "b", "c"])
assert result.pos == ("b", "c")
assert result.var_len == ("a",)


def test_positional_varlen_rejects_unknown_flags() -> None:
"""Regression test for #457: unknown flags should not be consumed as positional values.

With tyro >= 1.0.9, --invalid was incorrectly accepted as a positional
path value instead of raising an unrecognized-options error.
"""

@dataclass
class Arguments:
logfile_paths: tyro.conf.Positional[tuple[pathlib.Path, ...]]
cfg_file: Annotated[pathlib.Path, tyro.conf.arg(aliases=["-c"])] = pathlib.Path(
"config.yaml"
)

with pytest.raises(SystemExit):
tyro.cli(Arguments, args=["--invalid"])
19 changes: 19 additions & 0 deletions tests/test_py311_generated/test_positional_generated.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pathlib
from dataclasses import dataclass
from typing import Annotated, List, Optional, Sequence, Tuple

Expand Down Expand Up @@ -372,3 +373,21 @@ class Config:
result = tyro.cli(Config, args=["-x", "a", "--", "b", "c"])
assert result.pos == ("b", "c")
assert result.var_len == ("a",)


def test_positional_varlen_rejects_unknown_flags() -> None:
"""Regression test for #457: unknown flags should not be consumed as positional values.

With tyro >= 1.0.9, --invalid was incorrectly accepted as a positional
path value instead of raising an unrecognized-options error.
"""

@dataclass
class Arguments:
logfile_paths: tyro.conf.Positional[tuple[pathlib.Path, ...]]
cfg_file: Annotated[pathlib.Path, tyro.conf.arg(aliases=["-c"])] = pathlib.Path(
"config.yaml"
)

with pytest.raises(SystemExit):
tyro.cli(Arguments, args=["--invalid"])
Loading