Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply type casting & remove empty kwarg for Command #108

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading