| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import App, Parameter |
| 6 |
|
| 7 |
|
| 8 |
def test_no_parse_pos(app, assert_parse_args_partial): |
| 9 |
@app.default |
| 10 |
def foo(buzz: str, *, fizz: Annotated[str, Parameter(parse=False)]): |
| 11 |
pass |
| 12 |
|
| 13 |
assert_parse_args_partial(foo, "buzz_value", "buzz_value") |
| 14 |
_, _, ignored = app.parse_args("buzz_value") |
| 15 |
assert ignored == {"fizz": str} |
| 16 |
|
| 17 |
|
| 18 |
def test_no_parse_invalid_kind(app): |
| 19 |
"""Parameter.parse=False must be used with KEYWORD_ONLY or have a default value.""" |
| 20 |
|
| 21 |
with pytest.raises(ValueError): |
| 22 |
|
| 23 |
@app.default |
| 24 |
def foo(buzz: str, fizz: Annotated[str, Parameter(parse=False)]): |
| 25 |
pass |
| 26 |
|
| 27 |
app([]) |
| 28 |
|
| 29 |
|
| 30 |
def test_no_parse_with_default_allowed(app): |
| 31 |
"""Parameter.parse=False is allowed for non-KEYWORD_ONLY with default.""" |
| 32 |
|
| 33 |
@app.default |
| 34 |
def foo(buzz: str, fizz: Annotated[str, Parameter(parse=False)] = "default_value"): |
| 35 |
return (buzz, fizz) |
| 36 |
|
| 37 |
result = app("buzz_value", exit_on_error=False) |
| 38 |
assert result == ("buzz_value", "default_value") |
| 39 |
|
| 40 |
|
| 41 |
def test_no_parse_keyword_only_with_default(app): |
| 42 |
"""Parameter.parse=False is allowed for KEYWORD_ONLY with default.""" |
| 43 |
|
| 44 |
@app.default |
| 45 |
def foo(buzz: str, *, fizz: Annotated[str, Parameter(parse=False)] = "default_value"): |
| 46 |
return (buzz, fizz) |
| 47 |
|
| 48 |
result = app("buzz_value", exit_on_error=False) |
| 49 |
assert result == ("buzz_value", "default_value") |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def test_parse_regex_via_default_parameter(): |
| 56 |
"""App.default_parameter with regex skips underscore-prefixed params.""" |
| 57 |
app = App(default_parameter=Parameter(parse="^(?!_)")) |
| 58 |
|
| 59 |
@app.default |
| 60 |
def foo(buzz: str, *, visible: str = "visible_default", _hidden: str = "hidden_default"): |
| 61 |
return (buzz, visible, _hidden) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
_, bound, ignored = app.parse_args(["buzz_value", "--visible", "cli_visible"]) |
| 66 |
assert bound.args == ("buzz_value",) |
| 67 |
assert bound.kwargs == {"visible": "cli_visible"} |
| 68 |
assert ignored == {"_hidden": str} |
| 69 |
|
| 70 |
|
| 71 |
def test_parse_regex_not_in_help(capsys): |
| 72 |
"""Params not matching regex should not appear in help.""" |
| 73 |
app = App(default_parameter=Parameter(parse="^(?!_)"), result_action="return_value") |
| 74 |
|
| 75 |
@app.default |
| 76 |
def foo(visible: str, *, _private: str = "default"): |
| 77 |
pass |
| 78 |
|
| 79 |
app(["--help"], exit_on_error=False) |
| 80 |
output = capsys.readouterr().out |
| 81 |
assert "visible" in output.lower() |
| 82 |
assert "_private" not in output.lower() |
| 83 |
assert "private" not in output.lower() |
| 84 |
|
| 85 |
|
| 86 |
def test_parse_regex_explicit_parse_true_override(): |
| 87 |
"""Explicit Parameter(parse=True) overrides app-level regex.""" |
| 88 |
app = App(default_parameter=Parameter(parse="^(?!_)")) |
| 89 |
|
| 90 |
@app.default |
| 91 |
def foo(buzz: str, *, _private: Annotated[str, Parameter(parse=True)] = "default"): |
| 92 |
return (buzz, _private) |
| 93 |
|
| 94 |
|
| 95 |
_, bound, ignored = app.parse_args(["buzz_value", "--private", "cli_value"]) |
| 96 |
assert bound.args == ("buzz_value",) |
| 97 |
assert bound.kwargs == {"_private": "cli_value"} |
| 98 |
assert ignored == {} |
| 99 |
|
| 100 |
|
| 101 |
def test_parse_regex_explicit_show_true_override(capsys): |
| 102 |
"""Explicit Parameter(show=True) overrides regex-based auto-hide.""" |
| 103 |
app = App(default_parameter=Parameter(parse="^(?!_)"), result_action="return_value") |
| 104 |
|
| 105 |
@app.default |
| 106 |
def foo(visible: str, *, _private: Annotated[str, Parameter(show=True)] = "default"): |
| 107 |
pass |
| 108 |
|
| 109 |
app(["--help"], exit_on_error=False) |
| 110 |
output = capsys.readouterr().out |
| 111 |
assert "private" in output.lower() |
| 112 |
|
| 113 |
|
| 114 |
def test_parse_compiled_regex_via_default_parameter(): |
| 115 |
"""App.default_parameter with pre-compiled regex skips underscore-prefixed params.""" |
| 116 |
import re |
| 117 |
|
| 118 |
app = App(default_parameter=Parameter(parse=re.compile("^(?!_)"))) |
| 119 |
|
| 120 |
@app.default |
| 121 |
def foo(buzz: str, *, visible: str = "visible_default", _hidden: str = "hidden_default"): |
| 122 |
return (buzz, visible, _hidden) |
| 123 |
|
| 124 |
_, bound, ignored = app.parse_args(["buzz_value", "--visible", "cli_visible"]) |
| 125 |
assert bound.args == ("buzz_value",) |
| 126 |
assert bound.kwargs == {"visible": "cli_visible"} |
| 127 |
assert ignored == {"_hidden": str} |
| 128 |
|
| 129 |
|
| 130 |
def test_parse_regex_invalid_positional_no_default(): |
| 131 |
"""App.default_parameter regex that skips a required positional param should raise.""" |
| 132 |
app = App(default_parameter=Parameter(parse="^(?!_)")) |
| 133 |
|
| 134 |
@app.default |
| 135 |
def foo(_value: str): |
| 136 |
pass |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
with pytest.raises(ValueError, match="KEYWORD_ONLY"): |
| 141 |
app.parse_args([]) |
| 142 |
|
| 143 |
|
| 144 |
def test_no_parse_did_you_mean_excludes_non_parsed(app): |
| 145 |
"""Issue #730: UnknownOptionError should not suggest parse=False parameters. |
| 146 |
|
| 147 |
When a parameter has parse=False, it should not be included in the |
| 148 |
"Did you mean" suggestions for unknown options, since it's not a valid |
| 149 |
CLI option. |
| 150 |
""" |
| 151 |
from cyclopts.exceptions import UnknownOptionError |
| 152 |
|
| 153 |
@app.default |
| 154 |
def action(*, verbose: Annotated[bool, Parameter(parse=False)] = False): |
| 155 |
pass |
| 156 |
|
| 157 |
with pytest.raises(UnknownOptionError) as e: |
| 158 |
app.parse_args(["--verbose"], exit_on_error=False) |
| 159 |
|
| 160 |
|
| 161 |
error_message = str(e.value) |
| 162 |
assert "Unknown option: --verbose" in error_message |
| 163 |
|
| 164 |
assert "Did you mean" not in error_message |