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

convert union type with none to optional #31

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/hayhooks/server/pipelines/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def get_request_model(pipeline_name: str, pipeline_inputs):
for component_name, inputs in pipeline_inputs.items():
component_model = {}
for name, typedef in inputs.items():
input_type = handle_unsupported_types(typedef["type"], {DataFrame: dict})
try:
input_type = handle_unsupported_types(typedef["type"], {DataFrame: dict})
except TypeError as e:
print(f"ERROR at {component_name!r}, {name}: {typedef}")
raise e
component_model[name] = (
input_type,
typedef.get("default_value", ...),
Expand Down Expand Up @@ -70,7 +74,10 @@ def convert_component_output(component_output):
"""
result = {}
for output_name, data in component_output.items():
get_value = lambda data: data.to_dict()["init_parameters"] if hasattr(data, "to_dict") else data

def get_value(data):
return data.to_dict()["init_parameters"] if hasattr(data, "to_dict") else data

if type(data) is list:
result[output_name] = [get_value(d) for d in data]
else:
Expand Down
8 changes: 7 additions & 1 deletion src/hayhooks/server/utils/create_valid_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from inspect import isclass
from types import GenericAlias
from typing import Dict, Union, get_args, get_origin, get_type_hints
from typing import Dict, Union, Optional, get_args, get_origin, get_type_hints

from typing_extensions import TypedDict

Expand Down Expand Up @@ -36,6 +36,12 @@ def _handle_generics(t_) -> GenericAlias:
else:
new_type[arg_name] = arg_type
if new_type:
# because TypedDict can't handle union types with None
# rewrite them as Optional[type]
for arg_name, arg_type in new_type.items():
type_args = get_args(arg_type)
if len(type_args) == 2 and type_args[1] is type(None):
new_type[arg_name] = Optional[type_args[0]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this be better in _handle_generics? 🤔

If you have nested TypedDicts this would fail in any case because we're checking only the first level right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could try. Just the code was a little hard to understand and I've decided to do it the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, if you can't manage I can give it a try. Don't want to spoil all the fun. :)

return TypedDict(type_.__name__, new_type)

return type_
Expand Down