Skip to content

Commit 20f0712

Browse files
authored
chore: emit a warning if return_immediately is set (googleapis#355)
This flag is deprecated and should always be set to `False` when pulling messages synchronously.
1 parent b035b86 commit 20f0712

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

google/pubsub_v1/services/subscriber/async_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Type,
2929
Union,
3030
)
31+
import warnings
3132
import pkg_resources
3233

3334
import google.api_core.client_options as ClientOptions # type: ignore
@@ -935,6 +936,12 @@ async def pull(
935936
if max_messages is not None:
936937
request.max_messages = max_messages
937938

939+
if request.return_immediately:
940+
warnings.warn(
941+
"The return_immediately flag is deprecated and should be set to False.",
942+
category=DeprecationWarning,
943+
)
944+
938945
# Wrap the RPC method; this adds retry and timeout information,
939946
# and friendly error handling.
940947
rpc = gapic_v1.method_async.wrap_method(

google/pubsub_v1/services/subscriber/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Type,
3232
Union,
3333
)
34+
import warnings
3435
import pkg_resources
3536

3637
from google.api_core import client_options as client_options_lib # type: ignore
@@ -1124,6 +1125,12 @@ def pull(
11241125
if max_messages is not None:
11251126
request.max_messages = max_messages
11261127

1128+
if request.return_immediately:
1129+
warnings.warn(
1130+
"The return_immediately flag is deprecated and should be set to False.",
1131+
category=DeprecationWarning,
1132+
)
1133+
11271134
# Wrap the RPC method; this adds retry and timeout information,
11281135
# and friendly error handling.
11291136
rpc = self._transport._wrapped_methods[self._transport.pull]

synth.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
flags=re.MULTILINE | re.DOTALL,
7777
)
7878

79-
if count < 18:
79+
if count < 15:
8080
raise Exception("Expected replacements for gRPC channel options not made.")
8181

8282
# If the emulator is used, force an insecure gRPC channel to avoid SSL errors.
@@ -141,6 +141,35 @@
141141
\g<0>""",
142142
)
143143

144+
# Emit deprecation warning if return_immediately flag is set with synchronous pull.
145+
count = s.replace(
146+
"google/pubsub_v1/services/subscriber/*client.py",
147+
r"import pkg_resources",
148+
"import warnings\n\g<0>",
149+
)
150+
count = s.replace(
151+
"google/pubsub_v1/services/subscriber/*client.py",
152+
r"""
153+
([^\n\S]+(?:async\ )?def\ pull\(.*?->\ pubsub\.PullResponse:.*?)
154+
((?P<indent>[^\n\S]+)\#\ Wrap\ the\ RPC\ method)
155+
""",
156+
textwrap.dedent(
157+
"""
158+
\g<1>
159+
\g<indent>if request.return_immediately:
160+
\g<indent> warnings.warn(
161+
\g<indent> "The return_immediately flag is deprecated and should be set to False.",
162+
\g<indent> category=DeprecationWarning,
163+
\g<indent> )
164+
165+
\g<2>"""
166+
),
167+
flags=re.MULTILINE | re.DOTALL | re.VERBOSE,
168+
)
169+
170+
if count != 2:
171+
raise Exception("Too many or too few replacements in pull() methods.")
172+
144173
# Make sure that client library version is present in user agent header.
145174
s.replace(
146175
[

tests/unit/pubsub_v1/subscriber/test_subscriber_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import warnings
16+
1517
from google.auth import credentials
1618
import grpc
1719
import mock
20+
import pytest
1821

1922
from google.api_core.gapic_v1.client_info import METRICS_METADATA_KEY
2023
from google.cloud.pubsub_v1 import subscriber
@@ -217,3 +220,43 @@ def test_streaming_pull_gapic_monkeypatch():
217220
transport = client.api._transport
218221
assert hasattr(transport.streaming_pull, "_prefetch_first_result_")
219222
assert not transport.streaming_pull._prefetch_first_result_
223+
224+
225+
def test_sync_pull_warning_if_return_immediately():
226+
client = subscriber.Client()
227+
subscription_path = "projects/foo/subscriptions/bar"
228+
229+
with mock.patch.object(
230+
client.api._transport, "_wrapped_methods"
231+
), warnings.catch_warnings(record=True) as warned:
232+
client.pull(subscription=subscription_path, return_immediately=True)
233+
234+
# Setting the deprecated return_immediately flag to True should emit a warning.
235+
assert len(warned) == 1
236+
assert issubclass(warned[0].category, DeprecationWarning)
237+
warning_msg = str(warned[0].message)
238+
assert "return_immediately" in warning_msg
239+
assert "deprecated" in warning_msg
240+
241+
242+
@pytest.mark.asyncio
243+
async def test_sync_pull_warning_if_return_immediately_async():
244+
from google.pubsub_v1.services.subscriber.async_client import SubscriberAsyncClient
245+
246+
client = SubscriberAsyncClient()
247+
subscription_path = "projects/foo/subscriptions/bar"
248+
249+
patcher = mock.patch(
250+
"google.pubsub_v1.services.subscriber.async_client.gapic_v1.method_async.wrap_method",
251+
new=mock.AsyncMock,
252+
)
253+
254+
with patcher, warnings.catch_warnings(record=True) as warned:
255+
await client.pull(subscription=subscription_path, return_immediately=True)
256+
257+
# Setting the deprecated return_immediately flag to True should emit a warning.
258+
assert len(warned) == 1
259+
assert issubclass(warned[0].category, DeprecationWarning)
260+
warning_msg = str(warned[0].message)
261+
assert "return_immediately" in warning_msg
262+
assert "deprecated" in warning_msg

0 commit comments

Comments
 (0)