Skip to content

Commit

Permalink
Merge pull request #28 from frndmg/fix-static-default-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Jun 2, 2023
2 parents deb5aec + 0947296 commit cada8e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
21 changes: 21 additions & 0 deletions radicli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ def world(c):
assert arg1.arg.option == arg2.arg.option
assert arg1.arg.short == arg2.arg.short
assert arg1.arg.help == arg2.arg.help
assert arg1.default == arg2.default

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


def test_static_default_serialization():
cli = Radicli(prog="test")

@cli.command("test", a=Arg("--a"))
def test(a=[]):
"""Hello"""
...

with make_tempdir() as dir_path:
path = dir_path / "static.json"
cli.to_static(path)

static = StaticRadicli.load(path)

for cli_arg, static_arg in zip(
cli.commands["test"].args, static.commands["test"].args
):
assert cli_arg.default == static_arg.default


@pytest.mark.parametrize(
"arg_type,expected_type,expected_str",
[
Expand Down
11 changes: 6 additions & 5 deletions radicli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import inspect
import argparse
import re
import json

# We need this Iterable type, which is the type origin of types.Iterable
try:
Expand All @@ -31,7 +32,7 @@ class StaticArg(TypedDict):
option: Optional[str]
short: Optional[str]
orig_help: Optional[str]
default: Optional[Union[bool, str]]
default: str
help: Optional[str]
action: Optional[str]
choices: Optional[List[str]]
Expand Down Expand Up @@ -218,9 +219,9 @@ def to_static_json(self) -> StaticArg:
"option": self.arg.option,
"short": self.arg.short,
"orig_help": self.arg.help,
"default": str(self.default)
if self.default not in (False, None)
else self.default,
"default": DEFAULT_PLACEHOLDER
if self.default == DEFAULT_PLACEHOLDER
else json.dumps(self.default),
"help": self.help,
"action": str(self.action) if self.action else None,
"choices": list(c.value if isinstance(c, Enum) else c for c in self.choices)
Expand All @@ -245,7 +246,7 @@ def from_static_json(
orig_type=data["orig_type"],
default=DEFAULT_PLACEHOLDER
if data["default"] == DEFAULT_PLACEHOLDER
else data["default"],
else json.loads(data["default"]),
help=data["help"],
action=data["action"],
choices=data["choices"],
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.0.23
version = 0.0.24
description = Radically lightweight command-line interfaces
url = https://github.com/explosion/radicli
author = Explosion
Expand Down

0 comments on commit cada8e0

Please sign in to comment.