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

Conversation

Rusteam
Copy link
Contributor

@Rusteam Rusteam commented Jun 28, 2024

TypedDict is unable to handle Union[type, NoneType], it requires the type to be Optional[type]. I've added a small fix for that. This fixes #25.

Example traceback with deploying a pipeline that contains DocumentJoiner:

Traceback (most recent call last):
...
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/__init__.py", line 1, in <module>
    from hayhooks.server.app import app
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/app.py", line 32, in <module>
    app = create_app()
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/app.py", line 27, in create_app
    deployed_pipeline = deploy_pipeline_def(app, pipeline_defintion)
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/deploy_utils.py", line 20, in deploy
_pipeline_def
    PipelineRunRequest = get_request_model(pipeline_def.name, pipe.inputs())
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/pipelines/models.py", line 33, in get_requ
est_model
    raise e
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/pipelines/models.py", line 30, in get_requ
est_model
    input_type = handle_unsupported_types(typedef["type"], {DataFrame: dict})
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 51, in h
andle_unsupported_types
    return _handle_generics(type_)
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 25, in _
handle_generics
    result = handle_unsupported_types(t, types_mapping)
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 35, in h
andle_unsupported_types
    new_type[arg_name] = _handle_generics(arg_type)
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 25, in _
handle_generics
    result = handle_unsupported_types(t, types_mapping)
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 47, in h
andle_unsupported_types
    raise e
  File "/Users/rustynail/Projects/github/hayhooks/src/hayhooks/server/utils/create_valid_type.py", line 40, in h
andle_unsupported_types
    return TypedDict(type_.__name__, new_type)
  File "/Users/rustynail/miniconda3/envs/vlm-rag/lib/python3.10/site-packages/typing_extensions.py", line 1123, 
in TypedDict
    td = _TypedDictMeta(typename, (), ns, total=total, closed=closed)
  File "/Users/rustynail/miniconda3/envs/vlm-rag/lib/python3.10/site-packages/typing_extensions.py", line 954, i
n __new__
    own_annotations = {
  File "/Users/rustynail/miniconda3/envs/vlm-rag/lib/python3.10/site-packages/typing_extensions.py", line 955, i
n <dictcomp>
    n: typing._type_check(tp, msg, module=tp_dict.__module__)
  File "/Users/rustynail/miniconda3/envs/vlm-rag/lib/python3.10/typing.py", line 171, in _type_check
    raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.Union[str, NoneType] is not valid as type argument

@Rusteam
Copy link
Contributor Author

Rusteam commented Jul 3, 2024

any update on this?

Comment on lines 39 to 44
# 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. :)

@shadeMe
Copy link

shadeMe commented Jul 22, 2024

@Rusteam Could you make the above-requested changes?

@Rusteam
Copy link
Contributor Author

Rusteam commented Jul 22, 2024

Yes I will during this week.

@Rusteam
Copy link
Contributor Author

Rusteam commented Jul 23, 2024

@silvanocerza added changes, is this what you mean?

Copy link
Contributor

@silvanocerza silvanocerza left a comment

Choose a reason for hiding this comment

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

Thanks! 🙏

@silvanocerza
Copy link
Contributor

@Rusteam sorry for the late review, was caught by higher priority stuff.

@silvanocerza silvanocerza merged commit 112ae25 into deepset-ai:main Jul 31, 2024
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.

Hayhooks fails to deploy Pipeline with input/output type having Optional Fields
3 participants