Skip to content

Commit

Permalink
Apply type casting & remove empty kwarg for Command (#108)
Browse files Browse the repository at this point in the history
Currently, all kwargs are passed into `App.command` as `str`. Empty
kwarg is passed as empty string.

In this PR, we remove any empty kwarg, and apply casting based on the
kwarg annotation for primitive types
  • Loading branch information
nkvuong authored May 23, 2024
1 parent cc19164 commit cb16357
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
19 changes: 17 additions & 2 deletions src/databricks/labs/blueprint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def prompts_argument_name(self) -> str | None:
return param.name
return None

def get_argument_type(self, argument_name: str) -> str | None:
sig = inspect.signature(self.fn)
if argument_name not in sig.parameters:
return None
return sig.parameters[argument_name].annotation.__name__


class App:
def __init__(self, __file: str):
Expand Down Expand Up @@ -77,9 +83,18 @@ def _route(self, raw):
log_level = "info"
databricks_logger = logging.getLogger("databricks")
databricks_logger.setLevel(log_level.upper())
kwargs = {k.replace("-", "_"): v for k, v in flags.items()}
kwargs = {k.replace("-", "_"): v for k, v in flags.items() if v != ""}
cmd = self._mapping[command]
# modify kwargs to match the type of the argument
for kwarg in list(kwargs.keys()):
match cmd.get_argument_type(kwarg):
case "int":
kwargs[kwarg] = int(kwargs[kwarg])
case "bool":
kwargs[kwarg] = kwargs[kwarg].lower() == "true"
case "float":
kwargs[kwarg] = float(kwargs[kwarg])
try:
cmd = self._mapping[command]
if cmd.needs_workspace_client():
kwargs["w"] = self._workspace_client()
elif cmd.is_account:
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"command": "foo",
"flags": {
"name": "y",
"age": "100",
"salary": "100.5",
"address": "",
"is_customer": "true",
"log_level": "disabled",
},
}
Expand All @@ -22,27 +26,27 @@ def test_commands():
app = App(inspect.getfile(App))

@app.command(is_unauthenticated=True)
def foo(name: str):
def foo(name: str, age: int, salary: float, is_customer: bool, address: str = "default"):
"""Some comment"""
some(name)
some(name, age, salary, is_customer, address)

with mock.patch.object(sys, "argv", [..., FOO_COMMAND]):
app()

some.assert_called_with("y")
some.assert_called_with("y", 100, 100.5, True, "default")


def test_injects_prompts():
some = mock.Mock()
app = App(inspect.getfile(App))

@app.command(is_unauthenticated=True)
def foo(name: str, prompts: Prompts):
def foo(name: str, age: int, salary: float, is_customer: bool, prompts: Prompts, address: str = "default"):
"""Some comment"""
assert isinstance(prompts, Prompts)
some(name)
some(name, age, salary, is_customer, address)

with mock.patch.object(sys, "argv", [..., FOO_COMMAND]):
app()

some.assert_called_with("y")
some.assert_called_with("y", 100, 100.5, True, "default")

0 comments on commit cb16357

Please sign in to comment.