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

Unhide cookiecutter errors in kedro new #3693

Merged
merged 10 commits into from
Mar 14, 2024
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Upcoming Release 0.19.4

## Major features and improvements
* Cookiecutter errors are shown in short format without the `--verbose` flag.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the error now shown partly for all CLI commands or just kedro new?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now they're shown in short format for all commands but just for cookiecutter errors. So instead of Error: Failed to generate project when running cookiecutter. we will also get one line with a real error.

* Kedro commands now work from any subdirectory within a Kedro project.
* Kedro CLI now provides a better error message when project commands are run outside of a project i.e. `kedro run`

Expand Down
10 changes: 8 additions & 2 deletions kedro/framework/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,21 @@ class KedroCliError(click.exceptions.ClickException):

VERBOSE_ERROR = False
VERBOSE_EXISTS = True
COOKIECUTTER_EXCEPTIONS_PREFIX = "cookiecutter.exceptions"

def show(self, file: IO | None = None) -> None:
if self.VERBOSE_ERROR:
click.secho(traceback.format_exc(), nl=False, fg="yellow")
elif self.VERBOSE_EXISTS:
etype, value, _ = sys.exc_info()
etype, value, tb = sys.exc_info()
formatted_exception = "".join(traceback.format_exception_only(etype, value))
cookiecutter_exception = ""
for ex_line in traceback.format_exception(etype, value, tb):
if self.COOKIECUTTER_EXCEPTIONS_PREFIX in ex_line:
cookiecutter_exception = ex_line
break
click.secho(
f"{formatted_exception}Run with --verbose to see the full exception",
f"{cookiecutter_exception}{formatted_exception}Run with --verbose to see the full exception",
fg="yellow",
)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/framework/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ def test_run_with_invalid_config(
"Key `node-names` in provided configuration is not valid. \n\nDid you mean one of "
"these?\n node_names\n to_nodes\n namespace" in result.stdout
)
KedroCliError.VERBOSE_EXISTS = True

@mark.parametrize(
"fake_run_config_with_params,expected",
Expand Down
9 changes: 9 additions & 0 deletions tests/framework/cli/test_starters.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ def test_fail_if_dir_exists(self, fake_kedro_cli):
assert result.exit_code != 0
assert "directory already exists" in result.output

def test_cookiecutter_exception_if_no_verbose(self, fake_kedro_cli):
"""Check if the original cookiecutter exception is present in the output
if no verbose flag is provided."""
Path("new-kedro-project").mkdir()
result = CliRunner().invoke(
fake_kedro_cli, ["new"], input=_make_cli_prompt_input()
)
assert "cookiecutter.exceptions" in result.output

def test_prompt_no_title(self, fake_kedro_cli):
shutil.copytree(TEMPLATE_PATH, "template")
_write_yaml(Path("template") / "prompts.yml", {"repo_name": {}})
Expand Down