-
Notifications
You must be signed in to change notification settings - Fork 436
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
Fixed optional typing on non-serializable types #2939
Open
AlexejPenner
wants to merge
4
commits into
develop
Choose a base branch
from
bugfix/GROW-96-fix-optional-typing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3635f11
Fixed optional typing on non-serializable types
AlexejPenner ba67f40
Applied reviews and simplified the fix
AlexejPenner 04e627a
Adjusted test cases
AlexejPenner 15d5475
Merge branch 'develop' into bugfix/GROW-96-fix-optional-typing
AlexejPenner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ | |
Union, | ||
) | ||
|
||
from pydantic import ConfigDict, ValidationError, create_model | ||
from pydantic import ValidationError, create_model, ConfigDict | ||
|
||
from zenml.constants import ENFORCE_TYPE_ANNOTATIONS | ||
from zenml.exceptions import StepInterfaceError | ||
|
@@ -197,17 +197,23 @@ def _validate_input_value( | |
parameter: The function parameter for which the value was provided. | ||
value: The input value. | ||
""" | ||
config_dict = ConfigDict(arbitrary_types_allowed=False) | ||
|
||
# Create a pydantic model with just a single required field with the | ||
# type annotation of the parameter to verify the input type including | ||
# pydantics type coercion | ||
validation_model_class = create_model( | ||
"input_validation_model", | ||
__config__=config_dict, | ||
value=(parameter.annotation, ...), | ||
) | ||
validation_model_class(value=value) | ||
annotation = parameter.annotation | ||
|
||
# Use Pydantic for all types to take advantage of its coercion abilities | ||
try: | ||
config_dict = ConfigDict(arbitrary_types_allowed=True) | ||
validation_model_class = create_model( | ||
"input_validation_model", | ||
__config__=type("Config", (), config_dict), | ||
value=(annotation, ...), | ||
) | ||
validation_model_class(value=value) | ||
except Exception: | ||
# If Pydantic can't handle it, fall back to isinstance | ||
if not isinstance(value, annotation): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this might fail with the raw annotation: isinstance(int, Union[int, str])
# TypeError: Subscripted generics cannot be used with class and instance checks |
||
raise TypeError( | ||
f"Expected {annotation}, but got {type(value)}" | ||
) | ||
|
||
|
||
def validate_entrypoint_function( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
instead? In case pydantic fails with a regular validation error, we actually want to raise that because it means the types don't match right?