-
-
Notifications
You must be signed in to change notification settings - Fork 798
✨Adds the ability to use union in type signatures #1148
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
Open
arielf212
wants to merge
6
commits into
fastapi:master
Choose a base branch
from
arielf212:feature/union
base: master
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c902ac3
Adds support for unions with no complex types
arielf212 639de2b
Adds tests
arielf212 24bc37c
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 535b6ee
Merge branch 'master' into feature/union
svlandeg 697855e
Merge branch 'master' into feature/union
svlandeg 6662d62
Merge branch 'master' into feature/union
YuriiMotov 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 hidden or 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
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -702,6 +702,30 @@ def wrapper(**kwargs: Any) -> Any: | |||||
| return wrapper | ||||||
|
|
||||||
|
|
||||||
| class UnionParamType(click.ParamType): | ||||||
| @property | ||||||
| def name(self) -> str: # type: ignore | ||||||
| return " | ".join(_type.name for _type in self._types) | ||||||
|
|
||||||
| def __init__(self, types: List[click.ParamType]): | ||||||
| super().__init__() | ||||||
| self._types = types | ||||||
|
|
||||||
| def convert( | ||||||
| self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context] | ||||||
| ) -> Any: | ||||||
| # *types, last = self._types | ||||||
| error_messages = [] | ||||||
| for _type in self._types: | ||||||
| try: | ||||||
| return _type.convert(value, param, ctx) | ||||||
| except click.BadParameter as e: | ||||||
| print(type(e)) | ||||||
| error_messages.append(str(e)) | ||||||
| # return last.convert(value, param, ctx) | ||||||
| raise self.fail("\n" + "\nbut also\n".join(error_messages), param, ctx) | ||||||
|
|
||||||
|
|
||||||
| def get_click_type( | ||||||
| *, annotation: Any, parameter_info: ParameterInfo | ||||||
| ) -> click.ParamType: | ||||||
|
|
@@ -797,6 +821,12 @@ def get_click_type( | |||||
| [item.value for item in annotation], | ||||||
| case_sensitive=parameter_info.case_sensitive, | ||||||
| ) | ||||||
| elif get_origin(annotation) is not None and is_union(get_origin(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.
Suggested change
Seems that this should work |
||||||
| types = [ | ||||||
| get_click_type(annotation=arg, parameter_info=parameter_info) | ||||||
| for arg in get_args(annotation) | ||||||
| ] | ||||||
| return UnionParamType(types) | ||||||
| raise RuntimeError(f"Type not yet supported: {annotation}") # pragma: no cover | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -847,9 +877,14 @@ def get_click_param( | |||||
| if type_ is NoneType: | ||||||
| continue | ||||||
| types.append(type_) | ||||||
| assert len(types) == 1, "Typer Currently doesn't support Union types" | ||||||
| main_type = types[0] | ||||||
| origin = get_origin(main_type) | ||||||
| if len(types) == 1: | ||||||
| (main_type,) = types | ||||||
|
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. IMO, Also, comment above should be updated ( |
||||||
| origin = get_origin(main_type) | ||||||
| else: | ||||||
| for type_ in get_args(main_type): | ||||||
| assert not get_origin(type_), ( | ||||||
| "Union types with complex sub-types are not currently supported" | ||||||
| ) | ||||||
| # Handle Tuples and Lists | ||||||
| if lenient_issubclass(origin, List): | ||||||
| main_type = get_args(main_type)[0] | ||||||
|
|
||||||
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.
Tests should be simpler.
Something like this