Skip to content

Commit cada8e0

Browse files
authored
Merge pull request #28 from frndmg/fix-static-default-serialization
2 parents deb5aec + 0947296 commit cada8e0

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

radicli/tests/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ def world(c):
791791
assert arg1.arg.option == arg2.arg.option
792792
assert arg1.arg.short == arg2.arg.short
793793
assert arg1.arg.help == arg2.arg.help
794+
assert arg1.default == arg2.default
794795

795796
with pytest.raises(SystemExit):
796797
static.run(["", "--help"])
@@ -914,6 +915,26 @@ def convert_generic(value: str) -> str:
914915
assert new_arg.orig_type == stringify_type(arg.orig_type)
915916

916917

918+
def test_static_default_serialization():
919+
cli = Radicli(prog="test")
920+
921+
@cli.command("test", a=Arg("--a"))
922+
def test(a=[]):
923+
"""Hello"""
924+
...
925+
926+
with make_tempdir() as dir_path:
927+
path = dir_path / "static.json"
928+
cli.to_static(path)
929+
930+
static = StaticRadicli.load(path)
931+
932+
for cli_arg, static_arg in zip(
933+
cli.commands["test"].args, static.commands["test"].args
934+
):
935+
assert cli_arg.default == static_arg.default
936+
937+
917938
@pytest.mark.parametrize(
918939
"arg_type,expected_type,expected_str",
919940
[

radicli/util.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import inspect
99
import argparse
1010
import re
11+
import json
1112

1213
# We need this Iterable type, which is the type origin of types.Iterable
1314
try:
@@ -31,7 +32,7 @@ class StaticArg(TypedDict):
3132
option: Optional[str]
3233
short: Optional[str]
3334
orig_help: Optional[str]
34-
default: Optional[Union[bool, str]]
35+
default: str
3536
help: Optional[str]
3637
action: Optional[str]
3738
choices: Optional[List[str]]
@@ -218,9 +219,9 @@ def to_static_json(self) -> StaticArg:
218219
"option": self.arg.option,
219220
"short": self.arg.short,
220221
"orig_help": self.arg.help,
221-
"default": str(self.default)
222-
if self.default not in (False, None)
223-
else self.default,
222+
"default": DEFAULT_PLACEHOLDER
223+
if self.default == DEFAULT_PLACEHOLDER
224+
else json.dumps(self.default),
224225
"help": self.help,
225226
"action": str(self.action) if self.action else None,
226227
"choices": list(c.value if isinstance(c, Enum) else c for c in self.choices)
@@ -245,7 +246,7 @@ def from_static_json(
245246
orig_type=data["orig_type"],
246247
default=DEFAULT_PLACEHOLDER
247248
if data["default"] == DEFAULT_PLACEHOLDER
248-
else data["default"],
249+
else json.loads(data["default"]),
249250
help=data["help"],
250251
action=data["action"],
251252
choices=data["choices"],

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[metadata]
2-
version = 0.0.23
2+
version = 0.0.24
33
description = Radically lightweight command-line interfaces
44
url = https://github.com/explosion/radicli
55
author = Explosion

0 commit comments

Comments
 (0)