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

Adds FunctionInputOutputTypeChecker #757

Merged
merged 5 commits into from
Mar 12, 2024
Merged

Adds FunctionInputOutputTypeChecker #757

merged 5 commits into from
Mar 12, 2024

Conversation

skrawcz
Copy link
Collaborator

@skrawcz skrawcz commented Mar 12, 2024

This is a lifecycle adapter that one can add to strictly enforce the types of things at runtime. It will check the input to and the output from a function and check that it matches the expected type annotation.

It modifies/fixes the pandas split-apply-combine example to conform to the expected types; it actually found mismatches between what was typed and what was passed in.

I had to modify graph_functions.py so that the error message from the pre-node-execute hook would show up in the right place and the post-node-execute hook would not run.

Note: this does not do anything smart with Pandera or Annotated types just yet, but it could in future.

Test generated with the help of co-pilot. A little ugly, but they do the job.

Changes

  • split-apply-combine example
  • graph_functions.py -- to handle error case of pre-node-execute and post-node-execute and creating an error message
  • adds FunctionInputOutputTypeChecker to lifecycle.default & check type function

How I tested this

  • ran it locally

Notes

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

This is a lifecycle adapter that one can add to strictly enforce the types
of things at runtime. It will check the input to and the output from a function
and check that it matches the expected type annotation.

It modifies/fixes the pandas split-apply-combine example to conform
to the expected types; it actually found mismatches between what was
typed and what was passed in.

I had to modify graph_functions.py so that the error message from the
pre-node-execute hook would show up in the right place and the
post-node-execute hook would not run.

Note: this does not do anything smart with Pandera or Annotated types
just yet, but it could in future.

Test generated with the help of co-pilot. A little ugly, but they do the
job.
They changed what was output, so test needed to be fixed.
result=result,
task_id=task_id,
)
if not pre_node_execute_errored and adapter.does_hook(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm we should really have a context manager that does this... But this works for now.

@@ -206,6 +208,7 @@ def pre_node_execute(
node_return_type=node_.type,
task_id=task_id,
run_id=run_id,
node_input_types={k: v[0] for k, v in node_.input_types.items()},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add TODO for integrating the HamiltonNode class

import time
from typing import Any, Callable, Dict, List, Optional, Union

if sys.version_info >= (3, 9):
from typing import Literal
Copy link
Collaborator

Choose a reason for hiding this comment

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

Think literal is 3.8+?

hamilton/lifecycle/default.py Outdated Show resolved Hide resolved
To centralize this and make it clear we have two ways of checking types.
I don't want to merge them because the input type checker is less strict.
It doesn't recursively go check the values of inputs. So there's a TODO:
to figure out what to do, and how to unify. In the meantime they're in
the same file.
@elijahbenizzy elijahbenizzy self-requested a review March 12, 2024 05:57
Copy link
Collaborator

@elijahbenizzy elijahbenizzy left a comment

Choose a reason for hiding this comment

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

As discussed, looks good when you make the changes

Also adds more to example
@skrawcz skrawcz merged commit f481b3d into main Mar 12, 2024
23 checks passed
@skrawcz skrawcz deleted the add_strict_adapter branch March 12, 2024 14:53
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.

None yet

2 participants