-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add methods to api_core used by new autogenerator. #6267
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
Merged
+120
−0
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
accff32
Add dispatch and deserialize methods.
6411c7c
Bump version to 1.6.0a1
0ad32f2
Mark test as Python 3 only.
3a3b4e5
Fix import order.
fdee800
Address @theacodes feedback.
c0d3486
Revert alpha designation.
6e8c927
Move dispatch to gapic_v2; alias remaining gapic_v1 modules.
132830b
Fix import order.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import functools | ||
|
|
||
|
|
||
| def dispatch(func): | ||
| """Return a decorated method that dispatches on the second argument. | ||
|
|
||
| This is the equivalent of :meth:`functools.singledispatch`, but for | ||
| bound methods. | ||
| """ | ||
| base_dispatcher = functools.singledispatch(func) | ||
|
|
||
| # Define a wrapper function that works off args[1] instead of args[0]. | ||
| # This is needed because we are overloading *methods*, and their first | ||
| # argument is always `self`. | ||
| @functools.wraps(base_dispatcher) | ||
| def wrapper(*args, **kwargs): | ||
| return base_dispatcher.dispatch(args[1].__class__)(*args, **kwargs) | ||
|
|
||
| # The register function is not changed, so let singledispatch do the work. | ||
| wrapper.register = base_dispatcher.register | ||
|
|
||
| # Done; return the decorated method. | ||
| return wrapper | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import pytest | ||
| import six | ||
|
|
||
| from google.api_core.gapic_v1.dispatch import dispatch | ||
|
|
||
|
|
||
| @pytest.mark.skipif(six.PY2, reason='dispatch only works on Python 3.') | ||
| def test_dispatch(): | ||
| class Foo(object): | ||
| @dispatch | ||
| def bar(self, number, letter): | ||
| return 'Brought by the letter {} and the number {}.'.format( | ||
| letter, number, | ||
| ) | ||
|
|
||
| @bar.register(str) | ||
| def _bar_with_string(self, letter): | ||
| return self.bar(11, letter) | ||
|
|
||
| foo = Foo() | ||
| assert foo.bar(8, 'L') == 'Brought by the letter L and the number 8.' | ||
| assert foo.bar('Z') == 'Brought by the letter Z and the number 11.' |
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
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.
Uh oh!
There was an error while loading. Please reload this page.