Skip to content

Commit 3a437b7

Browse files
committed
Use warnings module instead of logging module
1 parent 86b5710 commit 3a437b7

File tree

4 files changed

+71
-69
lines changed

4 files changed

+71
-69
lines changed

google/api_core/_python_package_support.py

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

1515
"""Code to check versions of dependencies used by Google Cloud Client Libraries."""
1616

17-
import logging
17+
import warnings
1818
import sys
1919
from typing import Optional
2020
from ._python_version_support import (
@@ -128,7 +128,7 @@ def warn_deprecation_for_versions_less_than(
128128
{dependency_package}.
129129
"""
130130
)
131-
logging.warning(
131+
warnings.warn(
132132
message_template.format(
133133
dependent_import_package=dependent_import_package,
134134
dependency_import_package=dependency_import_package,

google/api_core/_python_version_support.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import datetime
1818
import enum
19-
import logging
19+
import warnings
2020
import sys
2121
import textwrap
2222
from typing import Any, NamedTuple, Optional, Dict, Tuple
@@ -195,7 +195,7 @@ def min_python(date: datetime.date) -> str:
195195
{min_python(today)}, and then update {package_label}.
196196
"""
197197
)
198-
logging.warning(message)
198+
warnings.warn(message)
199199
return PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED
200200

201201
eol_date = version_info.python_eol + EOL_GRACE_PERIOD
@@ -210,7 +210,7 @@ def min_python(date: datetime.date) -> str:
210210
{min_python(today)}, and then update {package_label}.
211211
"""
212212
)
213-
logging.warning(message)
213+
warnings.warn(message)
214214
return PythonVersionStatus.PYTHON_VERSION_EOL
215215

216216
if gapic_deprecation <= today <= gapic_end:
@@ -224,7 +224,7 @@ def min_python(date: datetime.date) -> str:
224224
then update {package_label}.
225225
"""
226226
)
227-
logging.warning(message)
227+
warnings.warn(message)
228228
return PythonVersionStatus.PYTHON_VERSION_DEPRECATED
229229

230230
return PythonVersionStatus.PYTHON_VERSION_SUPPORTED

tests/unit/test_python_package_support.py

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

1515
import sys
16+
import warnings
1617
from unittest.mock import patch, MagicMock
1718

1819
import pytest
@@ -60,9 +61,8 @@ def test_get_dependency_version_py37(mock_get_distribution):
6061

6162
@patch("google.api_core._python_package_support._get_distribution_and_import_packages")
6263
@patch("google.api_core._python_package_support.get_dependency_version")
63-
@patch("google.api_core._python_package_support.logging.warning")
6464
def test_warn_deprecation_for_versions_less_than(
65-
mock_log_warning, mock_get_version, mock_get_packages
65+
mock_get_version, mock_get_packages
6666
):
6767
"""Test the deprecation warning logic."""
6868
# Mock the helper function to return predictable package strings
@@ -73,48 +73,50 @@ def test_warn_deprecation_for_versions_less_than(
7373

7474
# Case 1: Installed version is less than required, should warn.
7575
mock_get_version.return_value = parse_version("1.0.0")
76-
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
77-
mock_log_warning.assert_called_once()
76+
with pytest.warns(UserWarning) as record:
77+
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
78+
assert len(record) == 1
7879
assert (
7980
"DEPRECATION: Package my-package (my.package) depends on dep-package (dep.package)"
80-
in mock_log_warning.call_args[0][0]
81+
in str(record[0].message)
8182
)
8283

83-
# Case 2: Installed version is equal to required, should not warn.
84-
mock_log_warning.reset_mock()
85-
mock_get_packages.reset_mock()
86-
mock_get_version.return_value = parse_version("2.0.0")
87-
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
88-
mock_log_warning.assert_not_called()
84+
# Cases where no warning should be issued
85+
with warnings.catch_warnings(record=True) as w:
86+
warnings.simplefilter("always") # Capture all warnings
8987

90-
# Case 3: Installed version is greater than required, should not warn.
91-
mock_log_warning.reset_mock()
92-
mock_get_packages.reset_mock()
93-
mock_get_version.return_value = parse_version("3.0.0")
94-
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
95-
mock_log_warning.assert_not_called()
88+
# Case 2: Installed version is equal to required, should not warn.
89+
mock_get_packages.reset_mock()
90+
mock_get_version.return_value = parse_version("2.0.0")
91+
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
9692

97-
# Case 4: Dependency not found, should not warn.
98-
mock_log_warning.reset_mock()
99-
mock_get_packages.reset_mock()
100-
mock_get_version.return_value = None
101-
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
102-
mock_log_warning.assert_not_called()
93+
# Case 3: Installed version is greater than required, should not warn.
94+
mock_get_packages.reset_mock()
95+
mock_get_version.return_value = parse_version("3.0.0")
96+
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
97+
98+
# Case 4: Dependency not found, should not warn.
99+
mock_get_packages.reset_mock()
100+
mock_get_version.return_value = None
101+
warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0")
102+
103+
# Assert that no warnings were recorded
104+
assert len(w) == 0
103105

104106
# Case 5: Custom message template.
105-
mock_log_warning.reset_mock()
106107
mock_get_packages.reset_mock()
107108
mock_get_packages.side_effect = [
108109
("dep-package (dep.package)", "dep-package"),
109110
("my-package (my.package)", "my-package"),
110111
]
111112
mock_get_version.return_value = parse_version("1.0.0")
112113
template = "Custom warning for {dependency_package} used by {dependent_package}."
113-
warn_deprecation_for_versions_less_than(
114-
"my.package", "dep.package", "2.0.0", message_template=template
115-
)
116-
mock_log_warning.assert_called_once()
114+
with pytest.warns(UserWarning) as record:
115+
warn_deprecation_for_versions_less_than(
116+
"my.package", "dep.package", "2.0.0", message_template=template
117+
)
118+
assert len(record) == 1
117119
assert (
118120
"Custom warning for dep-package (dep.package) used by my-package (my.package)."
119-
in mock_log_warning.call_args[0][0]
121+
in str(record[0].message)
120122
)

tests/unit/test_python_version_support.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pytest
1616
import datetime
1717
import textwrap
18+
import warnings
1819
from collections import namedtuple
1920

2021
from unittest.mock import patch
@@ -120,30 +121,33 @@ def test_all_tracked_versions_and_date_scenarios(
120121
mock_py_v = VersionInfoMock(major=version_tuple[0], minor=version_tuple[1])
121122

122123
with patch("google.api_core._python_version_support.sys.version_info", mock_py_v):
123-
with patch(
124-
"google.api_core._python_version_support.logging.warning"
125-
) as mock_log:
126-
result = check_python_version(today=mock_date)
127-
128-
if (
129-
(result != expected_status)
130-
or (result != PythonVersionStatus.PYTHON_VERSION_SUPPORTED)
131-
and mock_log.call_count != 1
132-
): # pragma: NO COVER
133-
py_version_str = f"{version_tuple[0]}.{version_tuple[1]}"
134-
version_info = PYTHON_VERSION_INFO[version_tuple]
135-
136-
fail_msg = _create_failure_message(
137-
expected_status,
138-
result,
139-
py_version_str,
140-
mock_date,
141-
gapic_dep,
142-
version_info.python_eol,
143-
eol_warning_starts,
144-
gapic_end,
145-
)
146-
pytest.fail(fail_msg, pytrace=False)
124+
# Supported versions should not issue warnings
125+
if expected_status == PythonVersionStatus.PYTHON_VERSION_SUPPORTED:
126+
with warnings.catch_warnings(record=True) as w:
127+
warnings.simplefilter("always")
128+
result = check_python_version(today=mock_date)
129+
assert len(w) == 0
130+
# All other statuses should issue a warning
131+
else:
132+
with pytest.warns(UserWarning) as record:
133+
result = check_python_version(today=mock_date)
134+
assert len(record) == 1
135+
136+
if result != expected_status: # pragma: NO COVER
137+
py_version_str = f"{version_tuple[0]}.{version_tuple[1]}"
138+
version_info = PYTHON_VERSION_INFO[version_tuple]
139+
140+
fail_msg = _create_failure_message(
141+
expected_status,
142+
result,
143+
py_version_str,
144+
mock_date,
145+
gapic_dep,
146+
version_info.python_eol,
147+
eol_warning_starts,
148+
gapic_end,
149+
)
150+
pytest.fail(fail_msg, pytrace=False)
147151

148152

149153
def test_override_gapic_end_only():
@@ -212,16 +216,13 @@ def test_untracked_older_version_is_unsupported():
212216
with patch(
213217
"google.api_core._python_version_support.sys.version_info", mock_py_version
214218
):
215-
with patch(
216-
"google.api_core._python_version_support.logging.warning"
217-
) as mock_log:
219+
with pytest.warns(UserWarning) as record:
218220
mock_date = datetime.date(2025, 1, 15)
219221
result = check_python_version(today=mock_date)
220222

221223
assert result == PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED
222-
mock_log.assert_called_once()
223-
call_args = mock_log.call_args[0][0]
224-
assert "non-supported" in call_args
224+
assert len(record) == 1
225+
assert "non-supported" in str(record[0].message)
225226

226227

227228
def test_untracked_newer_version_is_supported():
@@ -231,11 +232,10 @@ def test_untracked_newer_version_is_supported():
231232
with patch(
232233
"google.api_core._python_version_support.sys.version_info", mock_py_version
233234
):
234-
with patch(
235-
"google.api_core._python_version_support.logging.warning"
236-
) as mock_log:
235+
with warnings.catch_warnings(record=True) as w:
236+
warnings.simplefilter("always")
237237
mock_date = datetime.date(2025, 1, 15)
238238
result = check_python_version(today=mock_date)
239239

240240
assert result == PythonVersionStatus.PYTHON_VERSION_SUPPORTED
241-
mock_log.assert_not_called()
241+
assert len(w) == 0

0 commit comments

Comments
 (0)