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

Graceful errors #926

Merged
merged 1 commit into from
Jun 5, 2024
Merged

Graceful errors #926

merged 1 commit into from
Jun 5, 2024

Conversation

elijahbenizzy
Copy link
Collaborator

@elijahbenizzy
Copy link
Collaborator Author

elijahbenizzy commented May 29, 2024

POC, this works!

from hamilton import driver
from hamilton.lifecycle import default


class DoNotProceed(Exception):
    pass


def working_res_1() -> int:
    return 1


def working_res_2() -> int:
    return 2


def do_not_proceed_1(working_res_1: int, working_res_2: int) -> int:
    raise DoNotProceed()


def proceed_1(working_res_1: int, working_res_2: int) -> int:
    return working_res_1 + working_res_2


def proceed_2(working_res_1: int, working_res_2: int) -> int:
    return working_res_1 * working_res_2


def short_circuited_1(working_res_1: int, working_res_2: int, do_not_proceed_1: int) -> int:
    return 1  # this should not be reached


def proceed_3(working_res_1: int, working_res_2: int) -> int:
    return working_res_1 - working_res_2


def do_not_proceed_2(proceed_1: int, proceed_2: int, proceed_3: int) -> int:
    raise DoNotProceed()


def short_circuited_2(proceed_1: int, proceed_2: int, proceed_3: int, do_not_proceed_2: int) -> int:
    return 1  # this should not be reached


def short_circuited_3(short_circuited_1: int) -> int:
    return 1  # this should not be reached


def proceed_4(proceed_1: int, proceed_2: int, proceed_3: int) -> int:
    return proceed_1 + proceed_2 + proceed_3


if __name__ == "__main__":
    import __main__

    dr = (
        driver.Builder()
        .with_modules(__main__)
        .with_adapters(
            default.GracefulErrorAdapter(error_to_catch=DoNotProceed, sentinel_value=None)
        )
        .build()
    )
    dr.display_all_functions()
    vars = dr.list_available_variables()
    res = dr.execute(vars)
    print(res) # has nones instead of values we don't want to compute or we want to short-circuit
>> python graceful_errors.py # above
{'do_not_proceed_1': None,
 'do_not_proceed_2': None,
 'proceed_1': 3,
 'proceed_2': 2,
 'proceed_3': -1,
 'proceed_4': 4,
 'short_circuited_1': None,
 'short_circuited_2': None,
 'short_circuited_3': None,
 'working_res_1': 1,
 'working_res_2': 2}

@skrawcz
Copy link
Collaborator

skrawcz commented May 29, 2024

This is a vote to allow multiple adapters to implement run_to_execute_node but we can cross that bridge when someone gets there.

@elijahbenizzy elijahbenizzy changed the title WIP for graceful errors Graceful errors Jun 4, 2024
@elijahbenizzy elijahbenizzy marked this pull request as ready for review June 4, 2024 17:59

Be careful using ``None`` as the default -- feel free to replace it with a sentinel value
of your choice (this could negatively impact your graph's execution if you actually *do* intend
to use ``None`` return values).
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you provide python example of usage, e.g. passing in the Exception type to catch, and what it would do.

GracefulErrorAdapter(ValueError) vs GracefulErrorAdapter(Exception) etc

Copy link
Collaborator

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

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

just add more to the doc so a user can cut and paste an example.

This is still a bit of a WIP, but the API will stay backwards compatible
so I'm OK putting it in the stdlib.

This efectively cascades through null (customizable sentinel value)
results, not running a node if any of its dependencies are null.
@elijahbenizzy elijahbenizzy merged commit 1b26a1c into main Jun 5, 2024
23 checks passed
@elijahbenizzy elijahbenizzy deleted the graceful-failure branch June 5, 2024 02:45
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.

2 participants