question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use 'from' to explicitly chain exceptions

See original GitHub issue

Current behavior

I wanted to spend a little time contributing this weekend, so I ran pylint over this repo. I see this project has a .pylintrc but it looks like pylint isn’t run in CI, so some things do show up.

I found one class of warning from pylint that I think is worth addressing: explicitly re-raising exceptions in try-catches with from.

The newest version of pylint added a check for this. You can see details in “Explicit Exception Chaining”. That PEP describes a way to improve stack traces from try-catched exceptions.

without from

try:
    {}["a"]
except KeyError:
    raise RuntimeError("hey this does not exist")
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError: 'a'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: hey this does not exist

with from

try:
    {}["a"]
except KeyError  as err:
    raise RuntimeError("hey this does not exist") from err
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError: 'a'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: hey this does not exist

I think this language of “was the direct cause of the following exception” is clearer, and helps you to understand exactly where things started going wrong.

Proposed behavior

Would you be open to a pull request that changes try-catched exceptions in prefect to use this pattern? I wanted to ask first because right now there are 1334 places where that happens so it would touch a lot of code.

you can see them all like this:

pylint src/prefect/ | grep raise-missing-from > pylint.txt

I think this change would improve prefect and help save users some debugging time. Especially users who are not very experienced in Python.

Thanks for your time and consideration.

Notes for Hacktoberfest Contributors

This issue has been left open for Hacktoberfest 2020 contributors. If that describes you, welcome!

  1. Leave a comment below claiming a sub-module
  2. Identify all cases of this issue in that submodule by running code like the following in a terminal:
    pylint src/prefect/engine/cloud | grep "raise-missing-from"
    
  3. Fix all of these cases.
    • see #3320 for an example
  4. Open a pull request, following the steps in https://github.com/PrefectHQ/prefect/pulls
    • if there is already a changes/issue3306.yaml, add your name to the list of contributors
    • if there is not already a changes/issue3306.yaml, create one in your pull request, like in #3320

sub-modules with this problem

  • ~src/prefect/client~ - @jameslamb (#3320)
  • ~src/prefect/core~ - @brainless (#3383)
  • ~src/prefect/engine~ - @brainless (#3383)
  • src/prefect/environments/execution/dask
  • src/prefect/environments/execution/k8s
  • ~src/prefect/environments/storage~ - @shalika10
  • src/prefect/tasks/airtable
  • ~src/prefect/tasks/aws~ - @heyitskevin
  • src/prefect/tasks/azure
  • src/prefect/tasks/azureml
  • ~src/prefect/tasks/databricks~ - @rikturr (#3448)
  • src/prefect/tasks/dbt
  • src/prefect/tasks/dropbox
  • ~src/prefect/tasks/gcp~ - @ericlundy87 (#3326)
  • src/prefect/tasks/great_expectations
  • src/prefect/tasks/gsheets
  • src/prefect/tasks/kubernetes
  • ~src/prefect/tasks/mysql~ - @luthrap (#3428)
  • ~src/prefect/tasks/postgres~ - @brunocasarotti
  • src/prefect/tasks/redis
  • src/prefect/tasks/rss
  • src/prefect/tasks/snowflake
  • ~src/prefect/tasks/spacy~ - @sp1thas
  • src/prefect/tasks/template
  • src/prefect/tasks/twitter
  • ~src/prefect/utilities~ - @gabrielcalderon (#3429)

NOTE: This issue is not urgent. It’s ok to claim a sub-module now and not contribute until Hacktoberfest begins on October 1st.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:43

github_iconTop GitHub Comments

4reactions
rikturrcommented, Sep 15, 2020

I’ll take the next one on the list! (and more if others don’t volunteer after Hacktoberfest)

  • src/prefect/contrib/tasks/databricks
3reactions
shalika10commented, Oct 9, 2020

@jameslamb @joshmeek yayy @jcrist my first PR got merged 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 3134 – Exception Chaining and Embedded Tracebacks
Implicit Exception Chaining · Each thread has an exception context initially set to None . · Whenever an exception is raised, if the...
Read more >
Why we need explicit exception chaining in Python 3
Implicit chaining can be considered as error in exception handler and explicit tells you about uncaught exception. The net effect is that tests ......
Read more >
Chained Exceptions in Java - Baeldung
In this tutorial we are covering the topic of chained exceptions in Java, based on the code examples.
Read more >
Improving Python exception chaining with raise-from
I wrote a little script that uses Python's ast module to analyze a codebase and find all instances where this syntax isn't used...
Read more >
Python | Raising an Exception to Another Exception
To chain exceptions, use the raise from statement instead of a simple raise statement. This will give you information about both errors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found