| 1 |
"""Tests for PEP-692 ``**kwargs: Unpack[SomeTypedDict]`` support. |
| 2 |
|
| 3 |
See https://github.com/BrianPugh/cyclopts/issues/817. |
| 4 |
""" |
| 5 |
|
| 6 |
import sys |
| 7 |
from pathlib import Path |
| 8 |
from typing import Annotated, TypedDict |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from cyclopts import MissingArgumentError, Parameter |
| 13 |
from cyclopts.exceptions import UnknownOptionError |
| 14 |
|
| 15 |
if sys.version_info < (3, 12): |
| 16 |
pytest.skip("PEP 692 Unpack[TypedDict] for **kwargs requires Python 3.12+", allow_module_level=True) |
| 17 |
|
| 18 |
from typing import NotRequired, Required, Unpack |
| 19 |
|
| 20 |
|
| 21 |
class KwArgs(TypedDict, total=False): |
| 22 |
output_file: Path |
| 23 |
verbose: bool |
| 24 |
|
| 25 |
|
| 26 |
class RequiredKwArgs(TypedDict): |
| 27 |
name: str |
| 28 |
count: NotRequired[int] |
| 29 |
|
| 30 |
|
| 31 |
class MixedKwArgs(TypedDict, total=False): |
| 32 |
name: Required[str] |
| 33 |
count: int |
| 34 |
|
| 35 |
|
| 36 |
def test_kwargs_unpack_typed_dict_basic(app, assert_parse_args): |
| 37 |
@app.default |
| 38 |
def run(*input_files: Path, **kwargs: Unpack[KwArgs]) -> None: |
| 39 |
pass |
| 40 |
|
| 41 |
assert_parse_args( |
| 42 |
run, |
| 43 |
"a.txt b.txt --output-file out.txt --verbose", |
| 44 |
Path("a.txt"), |
| 45 |
Path("b.txt"), |
| 46 |
output_file=Path("out.txt"), |
| 47 |
verbose=True, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
def test_kwargs_unpack_typed_dict_no_kwargs(app, assert_parse_args): |
| 52 |
@app.default |
| 53 |
def run(*input_files: Path, **kwargs: Unpack[KwArgs]) -> None: |
| 54 |
pass |
| 55 |
|
| 56 |
assert_parse_args(run, "a.txt b.txt", Path("a.txt"), Path("b.txt")) |
| 57 |
|
| 58 |
|
| 59 |
def test_kwargs_unpack_typed_dict_only_one_field(app, assert_parse_args): |
| 60 |
@app.default |
| 61 |
def run(*input_files: Path, **kwargs: Unpack[KwArgs]) -> None: |
| 62 |
pass |
| 63 |
|
| 64 |
assert_parse_args( |
| 65 |
run, |
| 66 |
"a.txt --verbose", |
| 67 |
Path("a.txt"), |
| 68 |
verbose=True, |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
def test_kwargs_unpack_typed_dict_rejects_unknown_option(app): |
| 73 |
@app.default |
| 74 |
def run(**kwargs: Unpack[KwArgs]) -> None: |
| 75 |
pass |
| 76 |
|
| 77 |
with pytest.raises(UnknownOptionError): |
| 78 |
app.parse_args("--unknown-flag value", print_error=False, exit_on_error=False) |
| 79 |
|
| 80 |
|
| 81 |
def test_kwargs_unpack_typed_dict_required_field_missing(app): |
| 82 |
@app.default |
| 83 |
def run(**kwargs: Unpack[RequiredKwArgs]) -> None: |
| 84 |
pass |
| 85 |
|
| 86 |
with pytest.raises(MissingArgumentError): |
| 87 |
app.parse_args("", print_error=False, exit_on_error=False) |
| 88 |
|
| 89 |
|
| 90 |
def test_kwargs_unpack_typed_dict_required_field_provided(app, assert_parse_args): |
| 91 |
@app.default |
| 92 |
def run(**kwargs: Unpack[RequiredKwArgs]) -> None: |
| 93 |
pass |
| 94 |
|
| 95 |
assert_parse_args(run, "--name alice", name="alice") |
| 96 |
assert_parse_args(run, "--name alice --count 3", name="alice", count=3) |
| 97 |
|
| 98 |
|
| 99 |
def test_kwargs_unpack_typed_dict_mixed_required(app, assert_parse_args): |
| 100 |
"""total=False overridden by Required[...] on individual fields.""" |
| 101 |
|
| 102 |
@app.default |
| 103 |
def run(**kwargs: Unpack[MixedKwArgs]) -> None: |
| 104 |
pass |
| 105 |
|
| 106 |
assert_parse_args(run, "--name alice", name="alice") |
| 107 |
with pytest.raises(MissingArgumentError): |
| 108 |
app.parse_args("--count 3", print_error=False, exit_on_error=False) |
| 109 |
|
| 110 |
|
| 111 |
def test_kwargs_unpack_typed_dict_annotated(app, assert_parse_args): |
| 112 |
"""``Annotated[Unpack[K], ...]`` must be seen through, not treated as ``dict[str, Annotated[...]]``.""" |
| 113 |
|
| 114 |
@app.default |
| 115 |
def run(**kwargs: Annotated[Unpack[KwArgs], Parameter(show=True)]) -> None: |
| 116 |
pass |
| 117 |
|
| 118 |
assert_parse_args(run, "--verbose", verbose=True) |
| 119 |
|
| 120 |
|
| 121 |
def test_kwargs_unpack_non_typeddict_raises(app): |
| 122 |
"""``Unpack[X]`` where X is not a TypedDict should raise a clear TypeError.""" |
| 123 |
|
| 124 |
class NotATypedDict: |
| 125 |
x: int |
| 126 |
|
| 127 |
with pytest.raises(TypeError, match="Unpack"): |
| 128 |
|
| 129 |
@app.default |
| 130 |
def run(**kwargs: Unpack[NotATypedDict]) -> None: |
| 131 |
pass |
| 132 |
|
| 133 |
|
| 134 |
app.parse_args("", print_error=False, exit_on_error=False) |
| 135 |
|
| 136 |
|
| 137 |
def test_kwargs_unpack_empty_typeddict_help(app, console): |
| 138 |
"""Empty TypedDict should not render a blank Parameters row.""" |
| 139 |
|
| 140 |
class Empty(TypedDict): |
| 141 |
pass |
| 142 |
|
| 143 |
@app.default |
| 144 |
def run(**kwargs: Unpack[Empty]) -> None: |
| 145 |
pass |
| 146 |
|
| 147 |
with console.capture() as capture: |
| 148 |
app("--help", console=console, exit_on_error=False) |
| 149 |
|
| 150 |
output = capture.get() |
| 151 |
assert "--[KEYWORD]" not in output |
| 152 |
all. |
| 153 |
assert "Parameters" not in output |