-
Notifications
You must be signed in to change notification settings - Fork 2.9k
added support for union type syntax #4406
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -247,7 +247,7 @@ def _parse_schema_from_parameter( | |||||
| _raise_if_schema_unsupported(variant, schema) | ||||||
| return schema | ||||||
| if ( | ||||||
| get_origin(param.annotation) is Union | ||||||
| get_origin(param.annotation) in (Union, typing_types.UnionType) | ||||||
| # only parse simple UnionType, example int | str | float | bool | ||||||
| # complex types.UnionType will be invoked in raise branch | ||||||
| and all( | ||||||
|
|
@@ -330,7 +330,7 @@ def _parse_schema_from_parameter( | |||||
| schema.default = param.default | ||||||
| _raise_if_schema_unsupported(variant, schema) | ||||||
| return schema | ||||||
| if origin is Union: | ||||||
| if origin in (Union, typing_types.UnionType): | ||||||
|
Contributor
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. This check will raise an
Suggested change
|
||||||
| schema.any_of = [] | ||||||
| schema.type = types.Type.OBJECT | ||||||
| unique_types = set() | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||
| from abc import ABC | ||||||
| import inspect | ||||||
| import logging | ||||||
| import types as typing_types | ||||||
| from typing import Any | ||||||
| from typing import Callable | ||||||
| from typing import get_args | ||||||
|
|
@@ -168,7 +169,7 @@ def from_config( | |||||
| value = config_dict[param_name] | ||||||
|
|
||||||
| # Get the actual type T of the parameter if it's Optional[T] | ||||||
| if get_origin(param_type) is Union: | ||||||
| if get_origin(param_type) in (Union, typing_types.UnionType): | ||||||
|
Contributor
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. This check will raise an
Suggested change
|
||||||
| # This is Optional[T] which is Union[T, None] | ||||||
| args = get_args(param_type) | ||||||
| if len(args) == 2 and type(None) in args: | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||
|
|
||||||
| import inspect | ||||||
| import logging | ||||||
| import types as typing_types | ||||||
| from typing import Any | ||||||
| from typing import Callable | ||||||
| from typing import get_args | ||||||
|
|
@@ -122,7 +123,7 @@ def _preprocess_args(self, args: dict[str, Any]) -> dict[str, Any]: | |||||
| target_type = param.annotation | ||||||
|
|
||||||
| # Handle Optional[PydanticModel] types | ||||||
| if get_origin(param.annotation) is Union: | ||||||
| if get_origin(param.annotation) in (Union, typing_types.UnionType): | ||||||
|
Contributor
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. This check will raise an
Suggested change
|
||||||
| union_args = get_args(param.annotation) | ||||||
| # Find the non-None type in Optional[T] (which is Union[T, None]) | ||||||
| non_none_types = [arg for arg in union_args if arg is not type(None)] | ||||||
|
|
||||||
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.
This check will raise an
AttributeErroron Python versions below 3.10, astypes.UnionTypewas introduced in Python 3.10. To ensure backward compatibility, it's safer to accessUnionTypeusinggetattrwith a default value that won't be matched.