Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit a82e80d

Browse files
build: treat warnings as errors (#1033)
* build: treat warnings as errors * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * lint * fix warning in samples * remove warning from pytest.ini which doesn't have a tracking bug --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ac6d912 commit a82e80d

8 files changed

Lines changed: 46 additions & 23 deletions

File tree

pytest.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[pytest]
2+
filterwarnings =
3+
# treat all warnings as errors
4+
error
5+
# Remove once https://github.com/protocolbuffers/protobuf/issues/12186 is fixed
6+
ignore:.*custom tp_new.*in Python 3.14:DeprecationWarning
7+
# Remove once https://github.com/googleapis/python-api-common-protos/pull/191 is merged
8+
ignore:.*pkg_resources.declare_namespace:DeprecationWarning
9+
ignore:.*pkg_resources is deprecated as an API:DeprecationWarning
10+
# Remove once release PR https://github.com/googleapis/proto-plus-python/pull/391 is merged
11+
ignore:datetime.datetime.utcfromtimestamp\(\) is deprecated:DeprecationWarning:proto.datetime_helpers
12+
# Remove once release PR https://github.com/googleapis/python-api-core/pull/555 is merged
13+
ignore:datetime.datetime.utcnow\(\) is deprecated:DeprecationWarning:google.api_core.datetime_helpers
14+
# Remove once https://github.com/grpc/grpc/issues/35086 is fixed
15+
ignore:There is no current event loop:DeprecationWarning:grpc.aio._channel
16+
# Remove once a version of grpcio newer than 1.59.3 is released to PyPI
17+
ignore:datetime.datetime.utcnow\(\) is deprecated:DeprecationWarning:grpc._channel

samples/snippets/schema.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ def publish_avro_records(project_id: str, topic_id: str, avsc_file: str) -> None
473473
topic_path = publisher_client.topic_path(project_id, topic_id)
474474

475475
# Prepare to write Avro records to the binary output stream.
476-
avro_schema = schema.parse(open(avsc_file, "rb").read())
476+
with open(avsc_file, "rb") as file:
477+
avro_schema = schema.parse(file.read())
477478
writer = DatumWriter(avro_schema)
478479
bout = io.BytesIO()
479480

@@ -579,7 +580,8 @@ def subscribe_with_avro_schema(
579580
subscriber = SubscriberClient()
580581
subscription_path = subscriber.subscription_path(project_id, subscription_id)
581582

582-
avro_schema = schema.parse(open(avsc_file, "rb").read())
583+
with open(avsc_file, "rb") as file:
584+
avro_schema = schema.parse(file.read())
583585

584586
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
585587
# Get the message serialization type.
@@ -642,7 +644,8 @@ def subscribe_with_avro_schema_with_revisions(
642644
subscriber = SubscriberClient()
643645
subscription_path = subscriber.subscription_path(project_id, subscription_id)
644646

645-
writer_avro_schema = schema.parse(open(avsc_file, "rb").read())
647+
with open(avsc_file, "rb") as file:
648+
writer_avro_schema = schema.parse(file.read())
646649
# Dict to keep readers for different schema revisions.
647650
revisions_to_readers = {}
648651

tests/unit/gapic/pubsub_v1/test_subscriber.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,7 +2888,6 @@ def test_pull_flattened_error():
28882888
client.pull(
28892889
pubsub.PullRequest(),
28902890
subscription="subscription_value",
2891-
return_immediately=True,
28922891
max_messages=1277,
28932892
)
28942893

@@ -2942,7 +2941,6 @@ async def test_pull_flattened_error_async():
29422941
await client.pull(
29432942
pubsub.PullRequest(),
29442943
subscription="subscription_value",
2945-
return_immediately=True,
29462944
max_messages=1277,
29472945
)
29482946

@@ -6993,7 +6991,6 @@ def test_pull_rest_flattened():
69936991
# get truthy value for each flattened field
69946992
mock_args = dict(
69956993
subscription="subscription_value",
6996-
return_immediately=True,
69976994
max_messages=1277,
69986995
)
69996996
mock_args.update(sample_request)
@@ -7032,7 +7029,6 @@ def test_pull_rest_flattened_error(transport: str = "rest"):
70327029
client.pull(
70337030
pubsub.PullRequest(),
70347031
subscription="subscription_value",
7035-
return_immediately=True,
70367032
max_messages=1277,
70377033
)
70387034

tests/unit/pubsub_v1/publisher/test_flow_controller.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from typing import Callable
2020
from typing import Sequence
2121
from typing import Union
22-
import warnings
2322

2423
import pytest
2524

@@ -179,7 +178,10 @@ def test_incorrectly_releasing_too_many_messages():
179178
msg3 = grpc_types.PubsubMessage(data=b"z" * 100)
180179

181180
# Releasing a message that would make the load negative should result in a warning.
182-
with warnings.catch_warnings(record=True) as warned:
181+
with pytest.warns(
182+
RuntimeWarning,
183+
match="Releasing a message that was never added or already released",
184+
) as warned:
183185
flow_controller.release(msg1)
184186

185187
assert len(warned) == 1
@@ -438,7 +440,7 @@ def test_warning_on_internal_reservation_stats_error_when_unblocking():
438440
assert reservation is not None, "No messages blocked by flow controller."
439441
reservation.bytes_reserved = reservation.bytes_needed + 1
440442

441-
with warnings.catch_warnings(record=True) as warned:
443+
with pytest.warns(RuntimeWarning, match="Too many bytes reserved.") as warned:
442444
_run_in_daemon(flow_controller.release, [msg1], releasing_1_done)
443445
if not releasing_1_done.wait(timeout=0.1):
444446
pytest.fail("Releasing a message blocked or errored.") # pragma: NO COVER

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import pytest
3030
import time
31-
import warnings
3231

3332
from google.api_core import gapic_v1
3433
from google.api_core import retry as retries
@@ -60,7 +59,7 @@ def _assert_retries_equal(retry, retry2):
6059
def test_api_property_deprecated(creds):
6160
client = publisher.Client(credentials=creds)
6261

63-
with warnings.catch_warnings(record=True) as warned:
62+
with pytest.warns(DeprecationWarning, match="client.api") as warned:
6463
client.api
6564

6665
assert len(warned) == 1
@@ -72,7 +71,7 @@ def test_api_property_deprecated(creds):
7271
def test_api_property_proxy_to_generated_client(creds):
7372
client = publisher.Client(credentials=creds)
7473

75-
with warnings.catch_warnings(record=True):
74+
with pytest.warns(DeprecationWarning, match="client.api"):
7675
api_object = client.api
7776

7877
# Not a perfect check, but we are satisficed if the returned API object indeed

tests/unit/pubsub_v1/subscriber/test_dispatcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ def test_unknown_request_type():
361361

362362
items = ["a random string, not a known request type"]
363363
manager.send_unary_ack.return_value = (items, [])
364-
dispatcher_.dispatch_callback(items)
364+
with pytest.warns(RuntimeWarning, match="Skipping unknown request item of type"):
365+
dispatcher_.dispatch_callback(items)
365366

366367

367368
def test_ack():

tests/unit/pubsub_v1/subscriber/test_scheduler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
import concurrent.futures
1616
import queue
17+
import pytest
1718
import sys
1819
import threading
1920
import time
20-
import warnings
2121

2222
# special case python < 3.8
2323
if sys.version_info.major == 3 and sys.version_info.minor < 8:
@@ -76,7 +76,9 @@ def callback(*args, **kwargs):
7676
scheduler_.schedule(callback, "arg1", kwarg1="meep")
7777
scheduler_._executor.shutdown()
7878

79-
with warnings.catch_warnings(record=True) as warned:
79+
with pytest.warns(
80+
RuntimeWarning, match="Scheduling a callback after executor shutdown"
81+
) as warned:
8082
scheduler_.schedule(callback, "arg2", kwarg2="boop")
8183

8284
assert len(warned) == 1

tests/unit/pubsub_v1/subscriber/test_subscriber_client.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import warnings
1716

1817
import grpc
1918

@@ -218,7 +217,7 @@ def test_context_manager_raises_if_closed(creds):
218217
def test_api_property_deprecated(creds):
219218
client = subscriber.Client(credentials=creds)
220219

221-
with warnings.catch_warnings(record=True) as warned:
220+
with pytest.warns(DeprecationWarning, match="client.api") as warned:
222221
client.api
223222

224223
assert len(warned) == 1
@@ -230,7 +229,7 @@ def test_api_property_deprecated(creds):
230229
def test_api_property_proxy_to_generated_client(creds):
231230
client = subscriber.Client(credentials=creds)
232231

233-
with warnings.catch_warnings(record=True):
232+
with pytest.warns(DeprecationWarning, match="client.api"):
234233
api_object = client.api
235234

236235
# Not a perfect check, but we are satisficed if the returned API object indeed
@@ -262,9 +261,10 @@ def test_sync_pull_warning_if_return_immediately(creds):
262261
client = subscriber.Client(credentials=creds)
263262
subscription_path = "projects/foo/subscriptions/bar"
264263

265-
with mock.patch.object(
266-
client._transport, "_wrapped_methods"
267-
), warnings.catch_warnings(record=True) as warned:
264+
with mock.patch.object(client._transport, "_wrapped_methods"), pytest.warns(
265+
DeprecationWarning,
266+
match="The return_immediately flag is deprecated and should be set to False",
267+
) as warned:
268268
client.pull(subscription=subscription_path, return_immediately=True)
269269

270270
# Setting the deprecated return_immediately flag to True should emit a warning.
@@ -287,7 +287,10 @@ async def test_sync_pull_warning_if_return_immediately_async(creds):
287287
new=mock.AsyncMock,
288288
)
289289

290-
with patcher, warnings.catch_warnings(record=True) as warned:
290+
with patcher, pytest.warns(
291+
DeprecationWarning,
292+
match="The return_immediately flag is deprecated and should be set to False",
293+
) as warned:
291294
await client.pull(subscription=subscription_path, return_immediately=True)
292295

293296
# Setting the deprecated return_immediately flag to True should emit a warning.

0 commit comments

Comments
 (0)