Skip to content

Commit 0720a44

Browse files
author
Jon Wayne Parrott
authored
Add google.api.core.helpers.general_helpers.wraps (#4166)
1 parent 339736c commit 0720a44

6 files changed

Lines changed: 84 additions & 5 deletions

File tree

packages/google-cloud-core/google/api/core/gapic_v1/method.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from google.api.core import page_iterator
2828
from google.api.core import timeout
29+
from google.api.core.helpers import general_helpers
2930
from google.api.core.helpers import grpc_helpers
3031

3132
_PY_VERSION = platform.python_version()
@@ -252,7 +253,7 @@ def get_topic(name, timeout=None):
252253
if metadata is not None:
253254
metadata = _prepare_metadata(metadata)
254255

255-
return six.wraps(func)(
256+
return general_helpers.wraps(func)(
256257
_GapicCallable(func, default_retry, default_timeout, metadata))
257258

258259

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for general Python functionality."""
16+
17+
import functools
18+
19+
import six
20+
21+
22+
# functools.partial objects lack several attributes present on real function
23+
# objects. In Python 2 wraps fails on this so use a restricted set instead.
24+
_PARTIAL_VALID_ASSIGNMENTS = ('__doc__',)
25+
26+
27+
def wraps(wrapped):
28+
"""A functools.wraps helper that handles partial objects on Python 2."""
29+
if isinstance(wrapped, functools.partial):
30+
return six.wraps(wrapped, assigned=_PARTIAL_VALID_ASSIGNMENTS)
31+
else:
32+
return six.wraps(wrapped)

packages/google-cloud-core/google/api/core/helpers/grpc_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import six
1919

2020
from google.api.core import exceptions
21+
from google.api.core.helpers import general_helpers
2122
import google.auth
2223
import google.auth.transport.grpc
2324
import google.auth.transport.requests
@@ -63,7 +64,7 @@ def _wrap_stream_errors(callable_):
6364
"""
6465
_patch_callable_name(callable_)
6566

66-
@six.wraps(callable_)
67+
@general_helpers.wraps(callable_)
6768
def error_remapped_callable(*args, **kwargs):
6869
try:
6970
result = callable_(*args, **kwargs)

packages/google-cloud-core/google/api/core/retry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def check_if_exists():
6666

6767
from google.api.core import exceptions
6868
from google.api.core.helpers import datetime_helpers
69+
from google.api.core.helpers import general_helpers
6970

7071
_LOGGER = logging.getLogger(__name__)
7172
_DEFAULT_INITIAL_DELAY = 1.0 # seconds
@@ -244,7 +245,7 @@ def __call__(self, func, on_error=None):
244245
Callable: A callable that will invoke ``func`` with retry
245246
behavior.
246247
"""
247-
@six.wraps(func)
248+
@general_helpers.wraps(func)
248249
def retry_wrapped_func(*args, **kwargs):
249250
"""A wrapper that calls target function with retry."""
250251
target = functools.partial(func, *args, **kwargs)

packages/google-cloud-core/google/api/core/timeout.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def is_thing_ready(timeout=None):
5858
import six
5959

6060
from google.api.core.helpers import datetime_helpers
61+
from google.api.core.helpers import general_helpers
6162

6263
_DEFAULT_INITIAL_TIMEOUT = 5.0 # seconds
6364
_DEFAULT_MAXIMUM_TIMEOUT = 30.0 # seconds
@@ -92,7 +93,7 @@ def __call__(self, func):
9293
Returns:
9394
Callable: The wrapped function.
9495
"""
95-
@six.wraps(func)
96+
@general_helpers.wraps(func)
9697
def func_with_timeout(*args, **kwargs):
9798
"""Wrapped function that adds timeout."""
9899
kwargs['timeout'] = self._timeout
@@ -198,7 +199,7 @@ def __call__(self, func):
198199
timeouts = _exponential_timeout_generator(
199200
self._initial, self._maximum, self._multiplier, self._deadline)
200201

201-
@six.wraps(func)
202+
@general_helpers.wraps(func)
202203
def func_with_timeout(*args, **kwargs):
203204
"""Wrapped function that adds timeout."""
204205
kwargs['timeout'] = next(timeouts)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2017, Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import functools
16+
17+
from google.api.core.helpers import general_helpers
18+
19+
20+
def test_wraps_normal_func():
21+
22+
def func():
23+
return 42
24+
25+
@general_helpers.wraps(func)
26+
def replacement():
27+
return func()
28+
29+
assert replacement() == 42
30+
31+
32+
def test_wraps_partial():
33+
34+
def func():
35+
return 42
36+
37+
partial = functools.partial(func)
38+
39+
@general_helpers.wraps(partial)
40+
def replacement():
41+
return func()
42+
43+
assert replacement() == 42

0 commit comments

Comments
 (0)