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

Improve IPython extension error handling #1761

Merged
merged 10 commits into from
Aug 11, 2022

Conversation

antonymilne
Copy link
Contributor

@antonymilne antonymilne commented Aug 4, 2022

Description

Resolves #1590.

Note. I still need to double check my tests cover everything they should, but this is ready for review.

There were two problems with how the IPython extension was handling errors:

  1. The catching of ImportError and ModuleNotFound error was meant to notify the user about when they didn't have Kedro installed but was too broad. The same error (Kedro appears not to be installed in your current environment) would also be raised when other dependencies (e.g. pandas) weren't installed, which was very confusing
  2. All other exceptions were being swallowed up and replaced with WARNING Kedro extension was registered but couldn't find a Kedro project. This is also very confusing because e.g. if you have a syntax error in your catalog.yml file you would have no idea what the problem was and instead think you were just in the wrong directory

Warning. Important lesson. This kind of behaviour is exactly what the pylint broad-except is trying to guard against. We should not swallow up exceptions unless we really mean to. In this case, it makes it much harder for users to debug what's going on.

What happens now?

If the Kedro project can't be found, a warning is emitted saying so:
image

Any other exception will be shown as is.
e.g. dataset dependency not installed:
image

e.g. catalog.yml syntax error:
image

Slight catch

When using kedro ipython/jupyter, these error messages appear on the terminal rather than in the Jupyter notebook itself. There's no easy way to push them to the Jupyter notebook. However, they can be reproduced there by executing %reload_kedro. I've added a note to the documentation to explain this. In the case of managed Jupyter instances this won't be a problem at all because you'll need to execute %load_ext in your notebook anyway, so the error will be surfaced there automatically.

This is perfectly satisfactory for now I think. I also considered:

  • making a new startup_error variable which captures the exception and is available in Jupyter notebooks. This is actually what we used to do before the IPython extension was created and we could put it back. But all the cases it solves are already covered by running %reload_kedro in your notebook and it doesn't seem any more discoverable than that
  • populate the catalog, context, etc. variables with Exceptions by default, so that if they're not available then displaying them will explain why. This seems like overkill for now but could be done in future if people find the current system not clear enough. From observing what current user problems are, I think what we have here solves 99% of the problems.

Checklist

  • Read the contributing guidelines
  • Opened this PR as a 'Draft Pull Request' if it is work-in-progress
  • Updated the documentation to reflect the code changes
  • Added a description of this change in the RELEASE.md file
  • Added tests to cover my changes

Signed-off-by: Antony Milne <[email protected]>
Signed-off-by: Antony Milne <[email protected]>
Signed-off-by: Antony Milne <[email protected]>
Signed-off-by: Antony Milne <[email protected]>
@antonymilne antonymilne changed the title Bugfix/ipython error handling Improve IPython extension error handling Aug 5, 2022
@antonymilne antonymilne marked this pull request as ready for review August 5, 2022 09:35
@@ -62,18 +56,18 @@ def my_register_pipeline():
mocker.patch(
"kedro.framework.startup.bootstrap_project", return_value=fake_metadata
)
mock_line_magic = mocker.MagicMock()
mock_line_magic = mocker.Mock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious is this necessary? Or we just prefer Mock in general. I think we use MagicMock quite a lot but without actually needing it in some tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question - I did a quick survey of our use of MagicMock vs. Mock and found that nearly always it's Mock apart from in this file. There's no need for the extra capability of MagicMock here so I changed them all.

Copy link
Contributor

@noklam noklam left a comment

Choose a reason for hiding this comment

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

Great catch! I think this is a small but significant change. I just fall into this trap this morning😅

When using kedro ipython/jupyter, these error messages appear on the terminal rather than in the Jupyter notebook itself. There's no easy way to push them to the Jupyter notebook. However, they can be reproduced there by executing %reload_kedro. I've added a note to the documentation to explain this. In the case of managed Jupyter instances this won't be a problem at all because you'll need to execute %load_ext in your notebook anyway, so the error will be surfaced there automatically.

I also checked this recently and couldn't find a way to do it, which is a bit annoying as almost 99% of people won't look at the terminal when they are using notebook.

Copy link
Member

@merelcht merelcht left a comment

Choose a reason for hiding this comment

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

Great PR description as always! ⭐ I've got some questions, but the overall changes look good to me. It's slightly annoying the error messages don't show in the notebook, but I think your comment in the docs is very clear and I agree we can always populate the variables with Exceptions if users don't find the error handling clear.

docs/source/development/commands_reference.md Outdated Show resolved Hide resolved
requirements.txt Show resolved Hide resolved
kedro/extras/extensions/ipython.py Show resolved Hide resolved
Copy link
Member

@merelcht merelcht left a comment

Choose a reason for hiding this comment

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

Really nice improvement! 👍

Signed-off-by: Antony Milne <[email protected]>
antonymilne and others added 3 commits August 11, 2022 09:16
Signed-off-by: Antony Milne <[email protected]>
…nto bugfix/ipython-error-handling

Signed-off-by: Antony Milne <[email protected]>

Signed-off-by: Antony Milne <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Jupyter workflow when configuration is broken
3 participants