-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import dataclasses | ||
from typing import Any, Literal | ||
|
||
import pytest | ||
|
||
import dcargs | ||
|
||
|
||
def test_union_basic(): | ||
def main(x: int | str) -> int | str: | ||
return x | ||
|
||
assert dcargs.cli(main, args=["--x", "5"]) == 5 | ||
assert dcargs.cli(main, args=["--x", "6"]) == 6 | ||
assert dcargs.cli(main, args=["--x", "five"]) == "five" | ||
|
||
|
||
def test_union_with_list(): | ||
def main(x: int | str | list[bool]) -> Any: | ||
return x | ||
|
||
assert dcargs.cli(main, args=["--x", "5"]) == 5 | ||
assert dcargs.cli(main, args=["--x", "6"]) == 6 | ||
assert dcargs.cli(main, args=["--x", "five"]) == "five" | ||
assert dcargs.cli(main, args=["--x", "True"]) == "True" | ||
assert dcargs.cli(main, args=["--x", "True", "False"]) == [True, False] | ||
|
||
|
||
def test_union_literal(): | ||
def main(x: Literal[1, 2] | Literal[3, 4, 5] | str) -> int | str: | ||
return x | ||
|
||
assert dcargs.cli(main, args=["--x", "5"]) == 5 | ||
assert dcargs.cli(main, args=["--x", "6"]) == "6" | ||
assert dcargs.cli(main, args=["--x", "five"]) == "five" | ||
|
||
|
||
def test_super_nested(): | ||
def main( | ||
x: None | ||
| list[ | ||
tuple[ | ||
None | int, | ||
Literal[3, 4], | ||
tuple[int, int] | tuple[str, str], | ||
] | ||
] = None | ||
) -> Any: | ||
return x | ||
|
||
assert dcargs.cli(main, args=[]) is None | ||
assert dcargs.cli(main, args="--x None".split(" ")) is None | ||
assert dcargs.cli(main, args="--x None 3 2 2".split(" ")) == [(None, 3, (2, 2))] | ||
assert dcargs.cli(main, args="--x 2 3 x 2".split(" ")) == [(2, 3, ("x", "2"))] | ||
assert dcargs.cli(main, args="--x 2 3 x 2 2 3 1 2".split(" ")) == [ | ||
(2, 3, ("x", "2")), | ||
(2, 3, (1, 2)), | ||
] | ||
with pytest.raises(SystemExit): | ||
dcargs.cli(main, args=["--help"]) |