From c868c2a386a98e30a8fac81eab0dcab70345ec2e Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Mon, 28 Jul 2025 18:34:26 -0700 Subject: [PATCH 01/19] feat: provide Python run-time version support This checks the runtime versions. Note that the defaults mean - we get the correct behavior for 3.7 and 3.8: both of these became immediately unsupported; - we get the most conservative behavior for 3.9: it becomes immediately deprecated (desired, since Python 3.9 reaches its end of life in 2025-10), and will become unsupported once it reaches its end of life (which is a conservative policy that we may or may not want to relax in a follow-up) Still todo: echo the package name in the warning message. --- google/api_core/__init__.py | 2 + google/api_core/_python_version_support.py | 186 +++++++++++++++++++ tests/unit/test_python_version_support.py | 206 +++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 google/api_core/_python_version_support.py create mode 100644 tests/unit/test_python_version_support.py diff --git a/google/api_core/__init__.py b/google/api_core/__init__.py index b80ea372..2adbdc2d 100644 --- a/google/api_core/__init__.py +++ b/google/api_core/__init__.py @@ -17,6 +17,8 @@ This package contains common code and utilities used by Google client libraries. """ +from google.api_core import _python_version_support from google.api_core import version as api_core_version __version__ = api_core_version.__version__ +check_python_version = _python_version_support.check_python_version diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py new file mode 100644 index 00000000..9f953914 --- /dev/null +++ b/google/api_core/_python_version_support.py @@ -0,0 +1,186 @@ +# Copyright 2025 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. + +"""Code to check Python versions supported by Google Cloud Client Libraries.""" + +import datetime +import enum +import logging +import sys +import textwrap +from typing import NamedTuple, Optional, Dict, Tuple + + +class PythonVersionStatus(enum.Enum): + """Represent the support status of a Python version.""" + + PYTHON_VERSION_UNSUPPORTED = "PYTHON_VERSION_UNSUPPORTED" + PYTHON_VERSION_EOL = "PYTHON_VERSION_EOL" + PYTHON_VERSION_DEPRECATED = "PYTHON_VERSION_DEPRECATED" + PYTHON_VERSION_SUPPORTED = "PYTHON_VERSION_SUPPORTED" + + +class VersionInfo(NamedTuple): + """Hold release and support date information for a Python version.""" + + python_beta: Optional[datetime.date] + python_start: datetime.date + python_eol: datetime.date + gapic_start: Optional[datetime.date] = None + gapic_deprecation: Optional[datetime.date] = None + gapic_end: Optional[datetime.date] = None + dep_unpatchable_cve: Optional[datetime.date] = None + + +PYTHON_VERSION_INFO: Dict[Tuple[int, int], VersionInfo] = { + # Refer to https://devguide.python.org/versions/ and the PEPs linked therefrom. + (3, 7): VersionInfo( + python_beta=None, + python_start=datetime.date(2018, 6, 27), + python_eol=datetime.date(2023, 6, 27), + ), + (3, 8): VersionInfo( + python_beta=None, + python_start=datetime.date(2019, 10, 14), + python_eol=datetime.date(2024, 10, 7), + ), + (3, 9): VersionInfo( + python_beta=datetime.date(2020, 5, 18), + python_start=datetime.date(2020, 10, 5), + python_eol=datetime.date(2025, 10, 5), # TODO: specify day when announced + ), + (3, 10): VersionInfo( + python_beta=datetime.date(2021, 5, 3), + python_start=datetime.date(2021, 10, 4), + python_eol=datetime.date(2026, 10, 4), # TODO: specify day when announced + ), + (3, 11): VersionInfo( + python_beta=datetime.date(2022, 5, 8), + python_start=datetime.date(2022, 10, 24), + python_eol=datetime.date(2027, 10, 24), # TODO: specify day when announced + ), + (3, 12): VersionInfo( + python_beta=datetime.date(2023, 5, 22), + python_start=datetime.date(2023, 10, 2), + python_eol=datetime.date(2028, 10, 2), # TODO: specify day when announced + ), + (3, 13): VersionInfo( + python_beta=datetime.date(2024, 5, 8), + python_start=datetime.date(2024, 10, 7), + python_eol=datetime.date(2029, 10, 7), # TODO: specify day when announced + ), + (3, 14): VersionInfo( + python_beta=datetime.date(2025, 5, 7), + python_start=datetime.date(2025, 10, 7), + python_eol=datetime.date(2030, 10, 7), # TODO: specify day when announced + ), +} + +LOWEST_TRACKED_VERSION = min(PYTHON_VERSION_INFO.keys()) +FAKE_PAST_DATE = datetime.date(1970, 1, 1) +FAKE_FUTURE_DATE = datetime.date(9000, 1, 1) + + +def _flatten_message(text: str) -> str: + """Dedent a multi-line string and flattens it into a single line.""" + return textwrap.dedent(text).strip().replace("\n", " ") + + +def check_python_version(today: Optional[datetime.date] = None) -> PythonVersionStatus: + """Check the running Python version and issue a support warning if needed. + + Args: + today: The date to check against. Defaults to the current date. + + Returns: + The support status of the current Python version. + """ + today = today or datetime.date.today() + + python_version = sys.version_info + version_tuple = (python_version.major, python_version.minor) + py_version_str = f"{python_version.major}.{python_version.minor}" + + version_info = PYTHON_VERSION_INFO.get(version_tuple) + + if not version_info: + if version_tuple < LOWEST_TRACKED_VERSION: + version_info = VersionInfo( + python_beta=FAKE_PAST_DATE, + python_start=FAKE_PAST_DATE, + python_eol=FAKE_PAST_DATE, + ) + else: + version_info = VersionInfo( + python_beta=FAKE_FUTURE_DATE, + python_start=FAKE_FUTURE_DATE, + python_eol=FAKE_FUTURE_DATE, + ) + + gapic_deprecation = version_info.gapic_deprecation or ( + version_info.python_eol - datetime.timedelta(days=365) + ) + gapic_end = version_info.gapic_end or ( + version_info.python_eol + datetime.timedelta(weeks=1) + ) + + def min_python(date: datetime.date) -> str: + """Find the minimum supported Python version for a given date.""" + for version, info in sorted(PYTHON_VERSION_INFO.items()): + if info.python_start <= date < info.python_eol: + return f"{version[0]}.{version[1]}" + return "at a supported version" + + if gapic_end < today: + message = _flatten_message( + f""" + You are using a non-supported Python version ({py_version_str}). + You will receive no updates to this client library. We suggest + you upgrade to the latest Python version, or at least Python + {min_python(today)}, and then update this library. + """ + ) + logging.warning(message) + return PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED + + eol_date = version_info.python_eol + datetime.timedelta(weeks=1) + if eol_date <= today <= gapic_end: + message = _flatten_message( + f""" + You are using a Python version ({py_version_str}) past its end + of life. This client library will continue receiving critical + bug fixes on a best-effort basis, but not any other fixes or + features. We suggest you upgrade to the latest Python version, + or at least Python {min_python(today)}, and then update this + library. + """ + ) + logging.warning(message) + return PythonVersionStatus.PYTHON_VERSION_EOL + + if gapic_deprecation <= today <= gapic_end: + message = _flatten_message( + f""" + You are using a Python version ({py_version_str}), which new + releases of this client library will stop supporting when it + reaches its end of life ({version_info.python_eol}). We + suggest you upgrade to the latest Python version, or at least + Python {min_python(version_info.python_eol)}, and then update + this library. + """ + ) + logging.warning(message) + return PythonVersionStatus.PYTHON_VERSION_DEPRECATED + + return PythonVersionStatus.PYTHON_VERSION_SUPPORTED diff --git a/tests/unit/test_python_version_support.py b/tests/unit/test_python_version_support.py new file mode 100644 index 00000000..43a8c014 --- /dev/null +++ b/tests/unit/test_python_version_support.py @@ -0,0 +1,206 @@ +# Copyright 2025 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 datetime +import textwrap +from collections import namedtuple + +from unittest.mock import patch + +# Code to be tested +from google.api_core._python_version_support import ( + check_python_version, + PythonVersionStatus, + PYTHON_VERSION_INFO, +) + +# Helper object for mocking sys.version_info +VersionInfoMock = namedtuple("VersionInfoMock", ["major", "minor"]) + + +def _create_failure_message(expected, result, py_version, date, + gapic_dep, py_eol, eol_warn, gapic_end): + """Create a detailed failure message for a test.""" + return textwrap.dedent( + f""" + --- Test Failed --- + Expected status: {expected.name} + Received status: {result.name} + --------------------- + Context: + - Mocked Python Version: {py_version} + - Mocked Today's Date: {date} + Calculated Dates: + - gapic_deprecation: {gapic_dep} + - python_eol: {py_eol} + - eol_warning_starts: {eol_warn} + - gapic_end: {gapic_end} + """ + ) + + +def generate_tracked_version_test_cases(): + """ + Yields test parameters for all tracked versions and boundary conditions. + """ + for version_tuple, version_info in PYTHON_VERSION_INFO.items(): + py_version_str = f"{version_tuple[0]}.{version_tuple[1]}" + gapic_dep = version_info.gapic_deprecation or ( + version_info.python_eol - datetime.timedelta(days=365) + ) + gapic_end = version_info.gapic_end or ( + version_info.python_eol + datetime.timedelta(weeks=1) + ) + eol_warning_starts = version_info.python_eol + datetime.timedelta(weeks=1) + + test_cases = { + "supported_before_deprecation_date": { + "date": gapic_dep - datetime.timedelta(days=1), + "expected": PythonVersionStatus.PYTHON_VERSION_SUPPORTED, + }, + "deprecated_on_deprecation_date": { + "date": gapic_dep, + "expected": PythonVersionStatus.PYTHON_VERSION_DEPRECATED, + }, + "deprecated_on_eol_date": { + "date": version_info.python_eol, + "expected": PythonVersionStatus.PYTHON_VERSION_DEPRECATED, + }, + "deprecated_before_eol_warning_starts": { + "date": eol_warning_starts - datetime.timedelta(days=1), + "expected": PythonVersionStatus.PYTHON_VERSION_DEPRECATED, + }, + "eol_on_eol_warning_date": { + "date": eol_warning_starts, + "expected": PythonVersionStatus.PYTHON_VERSION_EOL, + }, + "eol_on_gapic_end_date": { + "date": gapic_end, + "expected": PythonVersionStatus.PYTHON_VERSION_EOL, + }, + "unsupported_after_gapic_end_date": { + "date": gapic_end + datetime.timedelta(days=1), + "expected": PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED, + }, + } + + for name, params in test_cases.items(): + yield pytest.param( + version_tuple, params["date"], params["expected"], + gapic_dep, gapic_end, eol_warning_starts, + id=f"{py_version_str}-{name}" + ) + + +@pytest.mark.parametrize( + "version_tuple, mock_date, expected_status, gapic_dep, gapic_end, eol_warning_starts", + generate_tracked_version_test_cases() +) +def test_all_tracked_versions_and_date_scenarios( + version_tuple, mock_date, expected_status, gapic_dep, gapic_end, + eol_warning_starts +): + """Test all outcomes for each tracked version using parametrization.""" + mock_py_v = VersionInfoMock( + major=version_tuple[0], minor=version_tuple[1] + ) + + with patch("google.api_core._python_version_support.sys.version_info", mock_py_v): + with patch("google.api_core._python_version_support.logging.warning") as mock_log: + result = check_python_version(today=mock_date) + + if ((result != expected_status) or + (result != PythonVersionStatus.PYTHON_VERSION_SUPPORTED) and mock_log.call_count != 1): + py_version_str = f"{version_tuple[0]}.{version_tuple[1]}" + version_info = PYTHON_VERSION_INFO[version_tuple] + + fail_msg = _create_failure_message( + expected_status, result, py_version_str, + mock_date, gapic_dep, version_info.python_eol, + eol_warning_starts, gapic_end + ) + pytest.fail(fail_msg, pytrace=False) + + +def test_override_gapic_end_only(): + """Test behavior when only gapic_end is manually overridden.""" + version_tuple = (3, 9) + original_info = PYTHON_VERSION_INFO[version_tuple] + mock_py_version = VersionInfoMock( + major=version_tuple[0], minor=version_tuple[1] + ) + + custom_gapic_end = original_info.python_eol + datetime.timedelta(days=212) + overridden_info = original_info._replace(gapic_end=custom_gapic_end) + + with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): + with patch.dict('google.api_core._python_version_support.PYTHON_VERSION_INFO', {version_tuple: overridden_info}): + result_at_boundary = check_python_version(today=custom_gapic_end) + assert result_at_boundary == PythonVersionStatus.PYTHON_VERSION_EOL + + result_after_boundary = check_python_version( + today=custom_gapic_end + datetime.timedelta(days=1) + ) + assert result_after_boundary == PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED + + +def test_override_gapic_deprecation_only(): + """Test behavior when only gapic_deprecation is manually overridden.""" + version_tuple = (3, 9) + original_info = PYTHON_VERSION_INFO[version_tuple] + mock_py_version = VersionInfoMock( + major=version_tuple[0], minor=version_tuple[1] + ) + + custom_gapic_dep = original_info.python_eol - datetime.timedelta(days=120) + overridden_info = original_info._replace(gapic_deprecation=custom_gapic_dep) + + with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): + with patch.dict('google.api_core._python_version_support.PYTHON_VERSION_INFO', {version_tuple: overridden_info}): + result_before_boundary = check_python_version( + today=custom_gapic_dep - datetime.timedelta(days=1) + ) + assert result_before_boundary == PythonVersionStatus.PYTHON_VERSION_SUPPORTED + + result_at_boundary = check_python_version(today=custom_gapic_dep) + assert result_at_boundary == PythonVersionStatus.PYTHON_VERSION_DEPRECATED + + +def test_untracked_older_version_is_unsupported(): + """Test that an old, untracked version is unsupported and logs.""" + mock_py_version = VersionInfoMock(major=3, minor=6) + + with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): + with patch("google.api_core._python_version_support.logging.warning") as mock_log: + mock_date = datetime.date(2025, 1, 15) + result = check_python_version(today=mock_date) + + assert result == PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED + mock_log.assert_called_once() + call_args = mock_log.call_args[0][0] + assert "non-supported" in call_args + + +def test_untracked_newer_version_is_supported(): + """Test that a new, untracked version is supported and does not log.""" + mock_py_version = VersionInfoMock(major=4, minor=0) + + with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): + with patch("google.api_core._python_version_support.logging.warning") as mock_log: + mock_date = datetime.date(2025, 1, 15) + result = check_python_version(today=mock_date) + + assert result == PythonVersionStatus.PYTHON_VERSION_SUPPORTED + mock_log.assert_not_called() From 133a1e0a175bda85d55da256cb5e03a0bc32e8fa Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 29 Jul 2025 14:29:39 -0700 Subject: [PATCH 02/19] feat: apply Python version suport warnings to api_core --- google/api_core/__init__.py | 1 + google/api_core/_python_version_support.py | 28 +++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/google/api_core/__init__.py b/google/api_core/__init__.py index 2adbdc2d..b1f1c951 100644 --- a/google/api_core/__init__.py +++ b/google/api_core/__init__.py @@ -22,3 +22,4 @@ __version__ = api_core_version.__version__ check_python_version = _python_version_support.check_python_version +check_python_version(package="package google-api-core (google.api_core)") diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index 9f953914..84523e4b 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -37,10 +37,10 @@ class VersionInfo(NamedTuple): python_beta: Optional[datetime.date] python_start: datetime.date python_eol: datetime.date - gapic_start: Optional[datetime.date] = None + gapic_start: Optional[datetime.date] = None # unused gapic_deprecation: Optional[datetime.date] = None gapic_end: Optional[datetime.date] = None - dep_unpatchable_cve: Optional[datetime.date] = None + dep_unpatchable_cve: Optional[datetime.date] = None # unused PYTHON_VERSION_INFO: Dict[Tuple[int, int], VersionInfo] = { @@ -97,7 +97,8 @@ def _flatten_message(text: str) -> str: return textwrap.dedent(text).strip().replace("\n", " ") -def check_python_version(today: Optional[datetime.date] = None) -> PythonVersionStatus: +def check_python_version(package: Optional[str] = "this package", + today: Optional[datetime.date] = None) -> PythonVersionStatus: """Check the running Python version and issue a support warning if needed. Args: @@ -146,9 +147,9 @@ def min_python(date: datetime.date) -> str: message = _flatten_message( f""" You are using a non-supported Python version ({py_version_str}). - You will receive no updates to this client library. We suggest + Google will not post any further updates to {package}. We suggest you upgrade to the latest Python version, or at least Python - {min_python(today)}, and then update this library. + {min_python(today)}, and then update {package}. """ ) logging.warning(message) @@ -159,11 +160,10 @@ def min_python(date: datetime.date) -> str: message = _flatten_message( f""" You are using a Python version ({py_version_str}) past its end - of life. This client library will continue receiving critical - bug fixes on a best-effort basis, but not any other fixes or + of life. Google will update {package} with critical + bug fixes on a best-effort basis, but not with any other fixes or features. We suggest you upgrade to the latest Python version, - or at least Python {min_python(today)}, and then update this - library. + or at least Python {min_python(today)}, and then update {package}. """ ) logging.warning(message) @@ -172,12 +172,12 @@ def min_python(date: datetime.date) -> str: if gapic_deprecation <= today <= gapic_end: message = _flatten_message( f""" - You are using a Python version ({py_version_str}), which new - releases of this client library will stop supporting when it + You are using a Python version ({py_version_str}), + which Google will stop supporting in {package} when it reaches its end of life ({version_info.python_eol}). We - suggest you upgrade to the latest Python version, or at least - Python {min_python(version_info.python_eol)}, and then update - this library. + suggest you upgrade to the latest Python version, or at + least Python {min_python(version_info.python_eol)}, and + then update {package}. """ ) logging.warning(message) From 4d49aa0730a54124824b03bc4fdbba870411d6b6 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 30 Jul 2025 13:26:58 -0700 Subject: [PATCH 03/19] feat: add deprecation check for the protobuf package --- google/api_core/__init__.py | 5 ++ google/api_core/_python_package_support.py | 89 ++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 google/api_core/_python_package_support.py diff --git a/google/api_core/__init__.py b/google/api_core/__init__.py index b1f1c951..a605109c 100644 --- a/google/api_core/__init__.py +++ b/google/api_core/__init__.py @@ -17,9 +17,14 @@ This package contains common code and utilities used by Google client libraries. """ +from google.api_core import _python_package_support from google.api_core import _python_version_support from google.api_core import version as api_core_version __version__ = api_core_version.__version__ + check_python_version = _python_version_support.check_python_version check_python_version(package="package google-api-core (google.api_core)") + +check_dependency_versions = _python_package_support.check_dependency_versions +check_dependency_versions("google-api-core (google.api_core)") diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py new file mode 100644 index 00000000..d3d07e7c --- /dev/null +++ b/google/api_core/_python_package_support.py @@ -0,0 +1,89 @@ +# Copyright 2025 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. + +"""Code to check versions of dependencies used by Google Cloud Client Libraries.""" + +import logging +import sys +from typing import Optional +from ._python_version_support import _flatten_message + +# It is a good practice to alias the Version class for clarity in type hints. +from packaging.version import parse as parse_version, Version as PackagingVersion + + +def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: + """Get the parsed version of an installed package dependency. + + This function checks for an installed package and returns its version + as a `packaging.version.Version` object for safe comparison. It handles + both modern (Python 3.8+) and legacy (Python 3.7) environments. + + Args: + dependency_name: The distribution name of the package (e.g., 'requests'). + + Returns: + A `packaging.version.Version` object, or `None` if the package + is not found or another error occurs during version discovery. + """ + try: + if sys.version_info >= (3, 8): + from importlib import metadata + version_string = metadata.version(dependency_name) + return parse_version(version_string) + + # TODO: Remove this code path once we drop support for Python 3.7 + else: + # Use pkg_resources, which is part of setuptools. + import pkg_resources + version_string = pkg_resources.get_distribution(dependency_name).version + return parse_version(version_string) + + except: + return None + + +def warn_deprecation_for_versions_less_than(dependent_package:str, + dependency_name:str, + next_supported_version:str, + message_template: Optional[str] = None): + if not dependent_package or not dependency_name or not next_supported_version: + return + version_used = get_dependency_version(dependency_name) + if not version_used: + return + if version_used < parse_version(next_supported_version): + message_template = message_template or _flatten_message( + """DEPRECATION: Package {dependent_package} depends on + {dependency_name}, currently installed at version + {version_used.__str__}. Future updates to + {dependent_package} will require {dependency_name} at + version {next_supported_version} or higher. Please ensure + that either (a) your Python environment doesn't pin the + version of {dependency_name}, so that updates to + {dependent_package} can require the higher version, or (b) + you manually update your Python environment to use at + least version {next_supported_version} of + {dependency_name}.""" + ) + logging.warning( + message_template.format( + dependent_package=dependent_package, + dependency_name=dependency_name, + next_supported_version=next_supported_version, + version_used=version_used, + )) + +def check_dependency_versions(dependent_package: str): + warn_deprecation_for_versions_less_than(dependent_package, "protobuf (google.protobuf)", "4.25.8") From 1545e468017056fd25e97d0a4128b77893021c44 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 30 Jul 2025 15:11:48 -0700 Subject: [PATCH 04/19] format files --- google/api_core/_python_package_support.py | 20 +++-- google/api_core/_python_version_support.py | 5 +- tests/unit/test_python_version_support.py | 96 ++++++++++++++-------- 3 files changed, 80 insertions(+), 41 deletions(-) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index d3d07e7c..8c0b2706 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -40,6 +40,7 @@ def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: try: if sys.version_info >= (3, 8): from importlib import metadata + version_string = metadata.version(dependency_name) return parse_version(version_string) @@ -47,6 +48,7 @@ def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: else: # Use pkg_resources, which is part of setuptools. import pkg_resources + version_string = pkg_resources.get_distribution(dependency_name).version return parse_version(version_string) @@ -54,10 +56,12 @@ def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: return None -def warn_deprecation_for_versions_less_than(dependent_package:str, - dependency_name:str, - next_supported_version:str, - message_template: Optional[str] = None): +def warn_deprecation_for_versions_less_than( + dependent_package: str, + dependency_name: str, + next_supported_version: str, + message_template: Optional[str] = None, +): if not dependent_package or not dependency_name or not next_supported_version: return version_used = get_dependency_version(dependency_name) @@ -83,7 +87,11 @@ def warn_deprecation_for_versions_less_than(dependent_package:str, dependency_name=dependency_name, next_supported_version=next_supported_version, version_used=version_used, - )) + ) + ) + def check_dependency_versions(dependent_package: str): - warn_deprecation_for_versions_less_than(dependent_package, "protobuf (google.protobuf)", "4.25.8") + warn_deprecation_for_versions_less_than( + dependent_package, "protobuf (google.protobuf)", "4.25.8" + ) diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index 84523e4b..be22ae16 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -97,8 +97,9 @@ def _flatten_message(text: str) -> str: return textwrap.dedent(text).strip().replace("\n", " ") -def check_python_version(package: Optional[str] = "this package", - today: Optional[datetime.date] = None) -> PythonVersionStatus: +def check_python_version( + package: Optional[str] = "this package", today: Optional[datetime.date] = None +) -> PythonVersionStatus: """Check the running Python version and issue a support warning if needed. Args: diff --git a/tests/unit/test_python_version_support.py b/tests/unit/test_python_version_support.py index 43a8c014..9f609744 100644 --- a/tests/unit/test_python_version_support.py +++ b/tests/unit/test_python_version_support.py @@ -30,8 +30,9 @@ VersionInfoMock = namedtuple("VersionInfoMock", ["major", "minor"]) -def _create_failure_message(expected, result, py_version, date, - gapic_dep, py_eol, eol_warn, gapic_end): +def _create_failure_message( + expected, result, py_version, date, gapic_dep, py_eol, eol_warn, gapic_end +): """Create a detailed failure message for a test.""" return textwrap.dedent( f""" @@ -98,38 +99,49 @@ def generate_tracked_version_test_cases(): for name, params in test_cases.items(): yield pytest.param( - version_tuple, params["date"], params["expected"], - gapic_dep, gapic_end, eol_warning_starts, - id=f"{py_version_str}-{name}" + version_tuple, + params["date"], + params["expected"], + gapic_dep, + gapic_end, + eol_warning_starts, + id=f"{py_version_str}-{name}", ) @pytest.mark.parametrize( "version_tuple, mock_date, expected_status, gapic_dep, gapic_end, eol_warning_starts", - generate_tracked_version_test_cases() + generate_tracked_version_test_cases(), ) def test_all_tracked_versions_and_date_scenarios( - version_tuple, mock_date, expected_status, gapic_dep, gapic_end, - eol_warning_starts + version_tuple, mock_date, expected_status, gapic_dep, gapic_end, eol_warning_starts ): """Test all outcomes for each tracked version using parametrization.""" - mock_py_v = VersionInfoMock( - major=version_tuple[0], minor=version_tuple[1] - ) + mock_py_v = VersionInfoMock(major=version_tuple[0], minor=version_tuple[1]) with patch("google.api_core._python_version_support.sys.version_info", mock_py_v): - with patch("google.api_core._python_version_support.logging.warning") as mock_log: + with patch( + "google.api_core._python_version_support.logging.warning" + ) as mock_log: result = check_python_version(today=mock_date) - if ((result != expected_status) or - (result != PythonVersionStatus.PYTHON_VERSION_SUPPORTED) and mock_log.call_count != 1): + if ( + (result != expected_status) + or (result != PythonVersionStatus.PYTHON_VERSION_SUPPORTED) + and mock_log.call_count != 1 + ): py_version_str = f"{version_tuple[0]}.{version_tuple[1]}" version_info = PYTHON_VERSION_INFO[version_tuple] fail_msg = _create_failure_message( - expected_status, result, py_version_str, - mock_date, gapic_dep, version_info.python_eol, - eol_warning_starts, gapic_end + expected_status, + result, + py_version_str, + mock_date, + gapic_dep, + version_info.python_eol, + eol_warning_starts, + gapic_end, ) pytest.fail(fail_msg, pytrace=False) @@ -138,41 +150,51 @@ def test_override_gapic_end_only(): """Test behavior when only gapic_end is manually overridden.""" version_tuple = (3, 9) original_info = PYTHON_VERSION_INFO[version_tuple] - mock_py_version = VersionInfoMock( - major=version_tuple[0], minor=version_tuple[1] - ) + mock_py_version = VersionInfoMock(major=version_tuple[0], minor=version_tuple[1]) custom_gapic_end = original_info.python_eol + datetime.timedelta(days=212) overridden_info = original_info._replace(gapic_end=custom_gapic_end) - with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): - with patch.dict('google.api_core._python_version_support.PYTHON_VERSION_INFO', {version_tuple: overridden_info}): + with patch( + "google.api_core._python_version_support.sys.version_info", mock_py_version + ): + with patch.dict( + "google.api_core._python_version_support.PYTHON_VERSION_INFO", + {version_tuple: overridden_info}, + ): result_at_boundary = check_python_version(today=custom_gapic_end) assert result_at_boundary == PythonVersionStatus.PYTHON_VERSION_EOL result_after_boundary = check_python_version( today=custom_gapic_end + datetime.timedelta(days=1) ) - assert result_after_boundary == PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED + assert ( + result_after_boundary == PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED + ) def test_override_gapic_deprecation_only(): """Test behavior when only gapic_deprecation is manually overridden.""" version_tuple = (3, 9) original_info = PYTHON_VERSION_INFO[version_tuple] - mock_py_version = VersionInfoMock( - major=version_tuple[0], minor=version_tuple[1] - ) + mock_py_version = VersionInfoMock(major=version_tuple[0], minor=version_tuple[1]) custom_gapic_dep = original_info.python_eol - datetime.timedelta(days=120) overridden_info = original_info._replace(gapic_deprecation=custom_gapic_dep) - with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): - with patch.dict('google.api_core._python_version_support.PYTHON_VERSION_INFO', {version_tuple: overridden_info}): + with patch( + "google.api_core._python_version_support.sys.version_info", mock_py_version + ): + with patch.dict( + "google.api_core._python_version_support.PYTHON_VERSION_INFO", + {version_tuple: overridden_info}, + ): result_before_boundary = check_python_version( today=custom_gapic_dep - datetime.timedelta(days=1) ) - assert result_before_boundary == PythonVersionStatus.PYTHON_VERSION_SUPPORTED + assert ( + result_before_boundary == PythonVersionStatus.PYTHON_VERSION_SUPPORTED + ) result_at_boundary = check_python_version(today=custom_gapic_dep) assert result_at_boundary == PythonVersionStatus.PYTHON_VERSION_DEPRECATED @@ -182,8 +204,12 @@ def test_untracked_older_version_is_unsupported(): """Test that an old, untracked version is unsupported and logs.""" mock_py_version = VersionInfoMock(major=3, minor=6) - with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): - with patch("google.api_core._python_version_support.logging.warning") as mock_log: + with patch( + "google.api_core._python_version_support.sys.version_info", mock_py_version + ): + with patch( + "google.api_core._python_version_support.logging.warning" + ) as mock_log: mock_date = datetime.date(2025, 1, 15) result = check_python_version(today=mock_date) @@ -197,8 +223,12 @@ def test_untracked_newer_version_is_supported(): """Test that a new, untracked version is supported and does not log.""" mock_py_version = VersionInfoMock(major=4, minor=0) - with patch("google.api_core._python_version_support.sys.version_info", mock_py_version): - with patch("google.api_core._python_version_support.logging.warning") as mock_log: + with patch( + "google.api_core._python_version_support.sys.version_info", mock_py_version + ): + with patch( + "google.api_core._python_version_support.logging.warning" + ) as mock_log: mock_date = datetime.date(2025, 1, 15) result = check_python_version(today=mock_date) From e5a7b1c41b05859f5a37872d1cbd9f958047e3a3 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 30 Jul 2025 15:26:03 -0700 Subject: [PATCH 05/19] fix lint warning --- google/api_core/_python_package_support.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index 8c0b2706..926aba8b 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -52,7 +52,7 @@ def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: version_string = pkg_resources.get_distribution(dependency_name).version return parse_version(version_string) - except: + except Exception: return None From 31321e1617bf5059819963474597f7ca36fbb8fc Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 30 Jul 2025 15:26:12 -0700 Subject: [PATCH 06/19] add docstring to `warn_deprecation_for_versions_less_than` --- google/api_core/_python_package_support.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index 926aba8b..b8602dfa 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -62,6 +62,25 @@ def warn_deprecation_for_versions_less_than( next_supported_version: str, message_template: Optional[str] = None, ): + """Issue a deprecation warning for outdated versions of `dependency_name`. + + If `dependency_name` is installed at a version less than + `next_supported_versions`, this issues a warning using either a + default `message_template` or one provided by the user. The + default `message_template informs users that they will not receive + future updates `dependent_package` if `dependency_name` is somehow + pinned to a version lower than `next_supported_version`. + + Args: + dependent_package: The distribution name of the package that needs `dependency_name`. + dependency_name: The distribution name oft he dependency to check. + next_supported_version: The version number below which a deprecation warning will be logged. + message_template: A custom default message template to replace + the default. This `message_template` is treated as an + f-string, where the following variables are defined: + `dependency_name`, `dependent_package`, + `next_supported_version`, and `version_used`. + """ if not dependent_package or not dependency_name or not next_supported_version: return version_used = get_dependency_version(dependency_name) From 1d27b341ce6ff88ffc653a6bc71f638da36fe333 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 30 Jul 2025 15:29:32 -0700 Subject: [PATCH 07/19] Add/fix docstrings --- google/api_core/_python_package_support.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index b8602dfa..e45afdff 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -72,9 +72,11 @@ def warn_deprecation_for_versions_less_than( pinned to a version lower than `next_supported_version`. Args: - dependent_package: The distribution name of the package that needs `dependency_name`. + dependent_package: The distribution name of the package that + needs `dependency_name`. dependency_name: The distribution name oft he dependency to check. - next_supported_version: The version number below which a deprecation warning will be logged. + next_supported_version: The version number below which a deprecation + warning will be logged. message_template: A custom default message template to replace the default. This `message_template` is treated as an f-string, where the following variables are defined: @@ -111,6 +113,17 @@ def warn_deprecation_for_versions_less_than( def check_dependency_versions(dependent_package: str): + """Bundle checks for all package dependencies. + + This function can be called by all depndents of google.api_core, + to emit needed deprecation warnings for any of their + dependencies. The dependencies to check should be updated here. + + Args: + dependent_package: The distribution name of the calling package, whose + dependencies we're checking. + + """ warn_deprecation_for_versions_less_than( dependent_package, "protobuf (google.protobuf)", "4.25.8" ) From a4d1145ab4fb73ea9a36bcf1db798f5c685b71c1 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 13:23:58 -0700 Subject: [PATCH 08/19] fix typo --- google/api_core/_python_package_support.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index e45afdff..1a94ad23 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -115,7 +115,7 @@ def warn_deprecation_for_versions_less_than( def check_dependency_versions(dependent_package: str): """Bundle checks for all package dependencies. - This function can be called by all depndents of google.api_core, + This function can be called by all dependents of google.api_core, to emit needed deprecation warnings for any of their dependencies. The dependencies to check should be updated here. From 4e25ec2a7402ffde47d7c5eb92bf71c0f7b2c7a7 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 13:43:04 -0700 Subject: [PATCH 09/19] add test for _python_package_support.py --- tests/unit/test_python_package_support.py | 99 +++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/unit/test_python_package_support.py diff --git a/tests/unit/test_python_package_support.py b/tests/unit/test_python_package_support.py new file mode 100644 index 00000000..f7167842 --- /dev/null +++ b/tests/unit/test_python_package_support.py @@ -0,0 +1,99 @@ +# Copyright 2025 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 sys +from unittest.mock import patch, MagicMock + +import pytest +from packaging.version import parse as parse_version + +from google.api_core._python_package_support import ( + get_dependency_version, + warn_deprecation_for_versions_less_than, +) + + +@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher") +@patch("importlib.metadata.version") +def test_get_dependency_version_py38_plus(mock_version): + """Test get_dependency_version on Python 3.8+.""" + mock_version.return_value = "1.2.3" + assert get_dependency_version("some-package") == parse_version("1.2.3") + mock_version.assert_called_once_with("some-package") + + # Test package not found + mock_version.side_effect = ImportError + assert get_dependency_version("not-a-package") is None + + +@pytest.mark.skipif(sys.version_info >= (3, 8), reason="requires python3.7") +@patch("pkg_resources.get_distribution") +def test_get_dependency_version_py37(mock_get_distribution): + """Test get_dependency_version on Python 3.7.""" + mock_dist = MagicMock() + mock_dist.version = "4.5.6" + mock_get_distribution.return_value = mock_dist + assert get_dependency_version("another-package") == parse_version("4.5.6") + mock_get_distribution.assert_called_once_with("another-package") + + # Test package not found + mock_get_distribution.side_effect = Exception # pkg_resources has its own exception types + assert get_dependency_version("not-a-package") is None + + +@patch("google.api_core._python_package_support.get_dependency_version") +@patch("google.api_core._python_package_support.logging.warning") +def test_warn_deprecation_for_versions_less_than(mock_log_warning, mock_get_version): + """Test the deprecation warning logic.""" + # Case 1: Installed version is less than required, should warn. + mock_get_version.return_value = parse_version("1.0.0") + warn_deprecation_for_versions_less_than( + "my-package", "dep-package", "2.0.0" + ) + mock_log_warning.assert_called_once() + assert "DEPRECATION: Package my-package depends on dep-package" in mock_log_warning.call_args[0][0] + + # Case 2: Installed version is equal to required, should not warn. + mock_log_warning.reset_mock() + mock_get_version.return_value = parse_version("2.0.0") + warn_deprecation_for_versions_less_than( + "my-package", "dep-package", "2.0.0" + ) + mock_log_warning.assert_not_called() + + # Case 3: Installed version is greater than required, should not warn. + mock_log_warning.reset_mock() + mock_get_version.return_value = parse_version("3.0.0") + warn_deprecation_for_versions_less_than( + "my-package", "dep-package", "2.0.0" + ) + mock_log_warning.assert_not_called() + + # Case 4: Dependency not found, should not warn. + mock_log_warning.reset_mock() + mock_get_version.return_value = None + warn_deprecation_for_versions_less_than( + "my-package", "dep-package", "2.0.0" + ) + mock_log_warning.assert_not_called() + + # Case 5: Custom message template. + mock_log_warning.reset_mock() + mock_get_version.return_value = parse_version("1.0.0") + template = "Custom warning for {dependency_name} used by {dependent_package}." + warn_deprecation_for_versions_less_than( + "my-package", "dep-package", "2.0.0", message_template=template + ) + mock_log_warning.assert_called_once() + assert "Custom warning for dep-package used by my-package." in mock_log_warning.call_args[0][0] From 96e6059ee9fc19edb21177899601d0f3acd4da57 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 13:43:32 -0700 Subject: [PATCH 10/19] add constants for various buffer periods --- google/api_core/_python_version_support.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index be22ae16..6ef6b28f 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -90,6 +90,8 @@ class VersionInfo(NamedTuple): LOWEST_TRACKED_VERSION = min(PYTHON_VERSION_INFO.keys()) FAKE_PAST_DATE = datetime.date(1970, 1, 1) FAKE_FUTURE_DATE = datetime.date(9000, 1, 1) +DEPRECATION_WARNING_PERIOD = datetime.timedelta(days=365) +EOL_GRACE_PERIOD = datetime.timedelta(weeks=1) def _flatten_message(text: str) -> str: @@ -131,10 +133,10 @@ def check_python_version( ) gapic_deprecation = version_info.gapic_deprecation or ( - version_info.python_eol - datetime.timedelta(days=365) + version_info.python_eol - DEPRECATION_WARNING_PERIOD ) gapic_end = version_info.gapic_end or ( - version_info.python_eol + datetime.timedelta(weeks=1) + version_info.python_eol + EOL_GRACE_PERIOD ) def min_python(date: datetime.date) -> str: @@ -156,7 +158,7 @@ def min_python(date: datetime.date) -> str: logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED - eol_date = version_info.python_eol + datetime.timedelta(weeks=1) + eol_date = version_info.python_eol + EOL_GRACE_PERIOD if eol_date <= today <= gapic_end: message = _flatten_message( f""" @@ -184,4 +186,4 @@ def min_python(date: datetime.date) -> str: logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_DEPRECATED - return PythonVersionStatus.PYTHON_VERSION_SUPPORTED + return PythonVersionStatus.PYTHON_VERSION_SUPPORTED \ No newline at end of file From f9c58a5aac12b8d55e584f27208ae5c90eadbce7 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 15:03:03 -0700 Subject: [PATCH 11/19] Update warning code to only require import package names We automatically get the distribution package names under the hood. --- google/api_core/__init__.py | 4 +- google/api_core/_python_package_support.py | 72 +++++++++++++------- google/api_core/_python_version_support.py | 79 +++++++++++++++------- tests/unit/test_python_package_support.py | 30 ++++---- 4 files changed, 119 insertions(+), 66 deletions(-) diff --git a/google/api_core/__init__.py b/google/api_core/__init__.py index a605109c..78d11424 100644 --- a/google/api_core/__init__.py +++ b/google/api_core/__init__.py @@ -24,7 +24,7 @@ __version__ = api_core_version.__version__ check_python_version = _python_version_support.check_python_version -check_python_version(package="package google-api-core (google.api_core)") +check_python_version(package="google.api_core") check_dependency_versions = _python_package_support.check_dependency_versions -check_dependency_versions("google-api-core (google.api_core)") +check_dependency_versions("google.api_core") diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index 1a94ad23..42878a45 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -17,7 +17,10 @@ import logging import sys from typing import Optional -from ._python_version_support import _flatten_message +from ._python_version_support import ( + _flatten_message, + _get_distribution_and_import_packages, +) # It is a good practice to alias the Version class for clarity in type hints. from packaging.version import parse as parse_version, Version as PackagingVersion @@ -57,62 +60,79 @@ def get_dependency_version(dependency_name: str) -> Optional[PackagingVersion]: def warn_deprecation_for_versions_less_than( - dependent_package: str, - dependency_name: str, + dependent_import_package: str, + dependency_import_package: str, next_supported_version: str, message_template: Optional[str] = None, ): - """Issue a deprecation warning for outdated versions of `dependency_name`. + """Issue any needed deprecation warnings for `dependency_import_package`. - If `dependency_name` is installed at a version less than + If `dependency_import_package` is installed at a version less than `next_supported_versions`, this issues a warning using either a default `message_template` or one provided by the user. The default `message_template informs users that they will not receive - future updates `dependent_package` if `dependency_name` is somehow - pinned to a version lower than `next_supported_version`. + future updates `dependent_import_package` if + `dependency_import_package` is somehow pinned to a version lower + than `next_supported_version`. Args: - dependent_package: The distribution name of the package that - needs `dependency_name`. - dependency_name: The distribution name oft he dependency to check. + dependent_import_package: The import name of the package that + needs `dependency_import_package`. + dependency_import_package: The import name of the dependency to check. next_supported_version: The version number below which a deprecation warning will be logged. message_template: A custom default message template to replace the default. This `message_template` is treated as an f-string, where the following variables are defined: - `dependency_name`, `dependent_package`, - `next_supported_version`, and `version_used`. + `dependency_import_package`, `dependent_import_package`; + `dependency_packages` and `dependent_packages`, which contain both the + distribution and import packages for the dependency and the dependent, + respectively; and `next_supported_version`, and `version_used`, which + refer to supported and currently-used versions of the dependency. + """ - if not dependent_package or not dependency_name or not next_supported_version: + if ( + not dependent_import_package + or not dependency_import_package + or not next_supported_version + ): return - version_used = get_dependency_version(dependency_name) + version_used = get_dependency_version(dependency_import_package) if not version_used: return if version_used < parse_version(next_supported_version): + ( + dependency_packages, + dependency_distribution_package, + ) = _get_distribution_and_import_packages(dependency_import_package) + ( + dependent_packages, + dependent_distribution_package, + ) = _get_distribution_and_import_packages(dependent_import_package) message_template = message_template or _flatten_message( - """DEPRECATION: Package {dependent_package} depends on - {dependency_name}, currently installed at version + """DEPRECATION: Package {dependent_packages} depends on + {dependency_packages}, currently installed at version {version_used.__str__}. Future updates to - {dependent_package} will require {dependency_name} at + {dependent_packages} will require {dependency_packages} at version {next_supported_version} or higher. Please ensure that either (a) your Python environment doesn't pin the - version of {dependency_name}, so that updates to - {dependent_package} can require the higher version, or (b) - you manually update your Python environment to use at + version of {dependency_packages}, so that updates to + {dependent_packages} can require the higher version, or + (b) you manually update your Python environment to use at least version {next_supported_version} of - {dependency_name}.""" + {dependency_packages}.""" ) logging.warning( message_template.format( - dependent_package=dependent_package, - dependency_name=dependency_name, + dependent_import_package=dependent_import_package, + dependency_import_package=dependency_import_package, next_supported_version=next_supported_version, version_used=version_used, ) ) -def check_dependency_versions(dependent_package: str): +def check_dependency_versions(dependent_import_package: str): """Bundle checks for all package dependencies. This function can be called by all dependents of google.api_core, @@ -120,10 +140,10 @@ def check_dependency_versions(dependent_package: str): dependencies. The dependencies to check should be updated here. Args: - dependent_package: The distribution name of the calling package, whose + dependent_import_package: The distribution name of the calling package, whose dependencies we're checking. """ warn_deprecation_for_versions_less_than( - dependent_package, "protobuf (google.protobuf)", "4.25.8" + dependent_import_package, "google.protobuf", "4.25.8" ) diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index 6ef6b28f..3a915789 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -99,6 +99,44 @@ def _flatten_message(text: str) -> str: return textwrap.dedent(text).strip().replace("\n", " ") +# TODO: Remove once we no longer support Python3.7 +if sys.version_info < (3, 8): + + def _get_pypi_package_name(module_name): + """Determine the PyPI package name for a given module name.""" + return None + +else: + from importlib import metadata + + def _get_pypi_package_name(module_name): + """Determine the PyPI package name for a given module name.""" + try: + # Get the mapping of modules to distributions + module_to_distributions = metadata.packages_distributions() + + # Check if the module is found in the mapping + if module_name in module_to_distributions: + # The value is a list of distribution names, take the first one + return module_to_distributions[module_name][0] + else: + return None # Module not found in the mapping + except Exception as e: + print(f"An error occurred: {e}") + return None + + +def _get_distribution_and_import_packages(import_package: str) -> Optional[str]: + """Return a pretty string with distribution & import package names.""" + distribution_package = _get_pypi_package_name(import_package) + dependency_distribution_and_import_packages = ( + f"package {distribution_package} ({import_package})" + if distribution_package + else import_package + ) + return dependency_distribution_and_import_packages, distribution_package + + def check_python_version( package: Optional[str] = "this package", today: Optional[datetime.date] = None ) -> PythonVersionStatus: @@ -111,6 +149,7 @@ def check_python_version( The support status of the current Python version. """ today = today or datetime.date.today() + package_label, _ = _get_distribution_and_import_packages(package) python_version = sys.version_info version_tuple = (python_version.major, python_version.minor) @@ -135,9 +174,7 @@ def check_python_version( gapic_deprecation = version_info.gapic_deprecation or ( version_info.python_eol - DEPRECATION_WARNING_PERIOD ) - gapic_end = version_info.gapic_end or ( - version_info.python_eol + EOL_GRACE_PERIOD - ) + gapic_end = version_info.gapic_end or (version_info.python_eol + EOL_GRACE_PERIOD) def min_python(date: datetime.date) -> str: """Find the minimum supported Python version for a given date.""" @@ -148,12 +185,11 @@ def min_python(date: datetime.date) -> str: if gapic_end < today: message = _flatten_message( - f""" - You are using a non-supported Python version ({py_version_str}). - Google will not post any further updates to {package}. We suggest - you upgrade to the latest Python version, or at least Python - {min_python(today)}, and then update {package}. - """ + f"""You are using a non-supported Python version + ({py_version_str}). Google will not post any further + updates to {package_label}. We suggest you upgrade to the + latest Python version, or at least Python + {min_python(today)}, and then update {package_label}. """ ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED @@ -161,29 +197,26 @@ def min_python(date: datetime.date) -> str: eol_date = version_info.python_eol + EOL_GRACE_PERIOD if eol_date <= today <= gapic_end: message = _flatten_message( - f""" - You are using a Python version ({py_version_str}) past its end - of life. Google will update {package} with critical - bug fixes on a best-effort basis, but not with any other fixes or - features. We suggest you upgrade to the latest Python version, - or at least Python {min_python(today)}, and then update {package}. - """ + f"""You are using a Python version ({py_version_str}) + past its end of life. Google will update {package_label} + with critical bug fixes on a best-effort basis, but not + with any other fixes or features. We suggest you upgrade + to the latest Python version, or at least Python + {min_python(today)}, and then update {package_label}.""" ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_EOL if gapic_deprecation <= today <= gapic_end: message = _flatten_message( - f""" - You are using a Python version ({py_version_str}), - which Google will stop supporting in {package} when it - reaches its end of life ({version_info.python_eol}). We + f"""You are using a Python version ({py_version_str}), + which Google will stop supporting in {package_label} when + it reaches its end of life ({version_info.python_eol}). We suggest you upgrade to the latest Python version, or at least Python {min_python(version_info.python_eol)}, and - then update {package}. - """ + then update {package_label}.""" ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_DEPRECATED - return PythonVersionStatus.PYTHON_VERSION_SUPPORTED \ No newline at end of file + return PythonVersionStatus.PYTHON_VERSION_SUPPORTED diff --git a/tests/unit/test_python_package_support.py b/tests/unit/test_python_package_support.py index f7167842..d32b336c 100644 --- a/tests/unit/test_python_package_support.py +++ b/tests/unit/test_python_package_support.py @@ -48,7 +48,9 @@ def test_get_dependency_version_py37(mock_get_distribution): mock_get_distribution.assert_called_once_with("another-package") # Test package not found - mock_get_distribution.side_effect = Exception # pkg_resources has its own exception types + mock_get_distribution.side_effect = ( + Exception # pkg_resources has its own exception types + ) assert get_dependency_version("not-a-package") is None @@ -58,34 +60,29 @@ def test_warn_deprecation_for_versions_less_than(mock_log_warning, mock_get_vers """Test the deprecation warning logic.""" # Case 1: Installed version is less than required, should warn. mock_get_version.return_value = parse_version("1.0.0") - warn_deprecation_for_versions_less_than( - "my-package", "dep-package", "2.0.0" - ) + warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") mock_log_warning.assert_called_once() - assert "DEPRECATION: Package my-package depends on dep-package" in mock_log_warning.call_args[0][0] + assert ( + "DEPRECATION: Package my-package depends on dep-package" + in mock_log_warning.call_args[0][0] + ) # Case 2: Installed version is equal to required, should not warn. mock_log_warning.reset_mock() mock_get_version.return_value = parse_version("2.0.0") - warn_deprecation_for_versions_less_than( - "my-package", "dep-package", "2.0.0" - ) + warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") mock_log_warning.assert_not_called() # Case 3: Installed version is greater than required, should not warn. mock_log_warning.reset_mock() mock_get_version.return_value = parse_version("3.0.0") - warn_deprecation_for_versions_less_than( - "my-package", "dep-package", "2.0.0" - ) + warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") mock_log_warning.assert_not_called() # Case 4: Dependency not found, should not warn. mock_log_warning.reset_mock() mock_get_version.return_value = None - warn_deprecation_for_versions_less_than( - "my-package", "dep-package", "2.0.0" - ) + warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") mock_log_warning.assert_not_called() # Case 5: Custom message template. @@ -96,4 +93,7 @@ def test_warn_deprecation_for_versions_less_than(mock_log_warning, mock_get_vers "my-package", "dep-package", "2.0.0", message_template=template ) mock_log_warning.assert_called_once() - assert "Custom warning for dep-package used by my-package." in mock_log_warning.call_args[0][0] + assert ( + "Custom warning for dep-package used by my-package." + in mock_log_warning.call_args[0][0] + ) From ad87fba6de5e847e63770ebca8b8759d27253163 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 15:15:49 -0700 Subject: [PATCH 12/19] Fix messaegs and test --- google/api_core/_python_package_support.py | 8 +++-- google/api_core/_python_version_support.py | 18 +++++++---- tests/unit/test_python_package_support.py | 35 ++++++++++++++++------ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/google/api_core/_python_package_support.py b/google/api_core/_python_package_support.py index 42878a45..9cf86baf 100644 --- a/google/api_core/_python_package_support.py +++ b/google/api_core/_python_package_support.py @@ -110,7 +110,8 @@ def warn_deprecation_for_versions_less_than( dependent_distribution_package, ) = _get_distribution_and_import_packages(dependent_import_package) message_template = message_template or _flatten_message( - """DEPRECATION: Package {dependent_packages} depends on + """ + DEPRECATION: Package {dependent_packages} depends on {dependency_packages}, currently installed at version {version_used.__str__}. Future updates to {dependent_packages} will require {dependency_packages} at @@ -120,12 +121,15 @@ def warn_deprecation_for_versions_less_than( {dependent_packages} can require the higher version, or (b) you manually update your Python environment to use at least version {next_supported_version} of - {dependency_packages}.""" + {dependency_packages}. + """ ) logging.warning( message_template.format( dependent_import_package=dependent_import_package, dependency_import_package=dependency_import_package, + dependency_packages=dependency_packages, + dependent_packages=dependent_packages, next_supported_version=next_supported_version, version_used=version_used, ) diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index 3a915789..1b77fe04 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -185,11 +185,13 @@ def min_python(date: datetime.date) -> str: if gapic_end < today: message = _flatten_message( - f"""You are using a non-supported Python version + f""" + You are using a non-supported Python version ({py_version_str}). Google will not post any further updates to {package_label}. We suggest you upgrade to the latest Python version, or at least Python - {min_python(today)}, and then update {package_label}. """ + {min_python(today)}, and then update {package_label}. + """ ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_UNSUPPORTED @@ -197,24 +199,28 @@ def min_python(date: datetime.date) -> str: eol_date = version_info.python_eol + EOL_GRACE_PERIOD if eol_date <= today <= gapic_end: message = _flatten_message( - f"""You are using a Python version ({py_version_str}) + f""" + You are using a Python version ({py_version_str}) past its end of life. Google will update {package_label} with critical bug fixes on a best-effort basis, but not with any other fixes or features. We suggest you upgrade to the latest Python version, or at least Python - {min_python(today)}, and then update {package_label}.""" + {min_python(today)}, and then update {package_label}. + """ ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_EOL if gapic_deprecation <= today <= gapic_end: message = _flatten_message( - f"""You are using a Python version ({py_version_str}), + f""" + You are using a Python version ({py_version_str}), which Google will stop supporting in {package_label} when it reaches its end of life ({version_info.python_eol}). We suggest you upgrade to the latest Python version, or at least Python {min_python(version_info.python_eol)}, and - then update {package_label}.""" + then update {package_label}. + """ ) logging.warning(message) return PythonVersionStatus.PYTHON_VERSION_DEPRECATED diff --git a/tests/unit/test_python_package_support.py b/tests/unit/test_python_package_support.py index d32b336c..b037a9d5 100644 --- a/tests/unit/test_python_package_support.py +++ b/tests/unit/test_python_package_support.py @@ -54,46 +54,63 @@ def test_get_dependency_version_py37(mock_get_distribution): assert get_dependency_version("not-a-package") is None +@patch("google.api_core._python_package_support._get_distribution_and_import_packages") @patch("google.api_core._python_package_support.get_dependency_version") @patch("google.api_core._python_package_support.logging.warning") -def test_warn_deprecation_for_versions_less_than(mock_log_warning, mock_get_version): +def test_warn_deprecation_for_versions_less_than( + mock_log_warning, mock_get_version, mock_get_packages +): """Test the deprecation warning logic.""" + # Mock the helper function to return predictable package strings + mock_get_packages.side_effect = [ + ("dep-package (dep.package)", "dep-package"), + ("my-package (my.package)", "my-package"), + ] + # Case 1: Installed version is less than required, should warn. mock_get_version.return_value = parse_version("1.0.0") - warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") + warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0") mock_log_warning.assert_called_once() assert ( - "DEPRECATION: Package my-package depends on dep-package" + "DEPRECATION: Package my-package (my.package) depends on dep-package (dep.package)" in mock_log_warning.call_args[0][0] ) # Case 2: Installed version is equal to required, should not warn. mock_log_warning.reset_mock() + mock_get_packages.reset_mock() mock_get_version.return_value = parse_version("2.0.0") - warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") + warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0") mock_log_warning.assert_not_called() # Case 3: Installed version is greater than required, should not warn. mock_log_warning.reset_mock() + mock_get_packages.reset_mock() mock_get_version.return_value = parse_version("3.0.0") - warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") + warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0") mock_log_warning.assert_not_called() # Case 4: Dependency not found, should not warn. mock_log_warning.reset_mock() + mock_get_packages.reset_mock() mock_get_version.return_value = None - warn_deprecation_for_versions_less_than("my-package", "dep-package", "2.0.0") + warn_deprecation_for_versions_less_than("my.package", "dep.package", "2.0.0") mock_log_warning.assert_not_called() # Case 5: Custom message template. mock_log_warning.reset_mock() + mock_get_packages.reset_mock() + mock_get_packages.side_effect = [ + ("dep-package (dep.package)", "dep-package"), + ("my-package (my.package)", "my-package"), + ] mock_get_version.return_value = parse_version("1.0.0") - template = "Custom warning for {dependency_name} used by {dependent_package}." + template = "Custom warning for {dependency_packages} used by {dependent_packages}." warn_deprecation_for_versions_less_than( - "my-package", "dep-package", "2.0.0", message_template=template + "my.package", "dep.package", "2.0.0", message_template=template ) mock_log_warning.assert_called_once() assert ( - "Custom warning for dep-package used by my-package." + "Custom warning for dep-package (dep.package) used by my-package (my.package)." in mock_log_warning.call_args[0][0] ) From f74b7946f9f9aab1bcb58b8d3e21c070acd4a87a Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Thu, 31 Jul 2025 15:18:07 -0700 Subject: [PATCH 13/19] Add TODO: provide the functionality in previous versions of api_core We have to add this functionality manually if the user has not upgraded to this version of api_copre --- google/api_core/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/google/api_core/__init__.py b/google/api_core/__init__.py index 78d11424..4f6c9bd3 100644 --- a/google/api_core/__init__.py +++ b/google/api_core/__init__.py @@ -23,6 +23,10 @@ __version__ = api_core_version.__version__ +# TODO: Until dependent artifacts require this version of +# google.api_core, the functionality below must be made available +# manually in those artifacts. + check_python_version = _python_version_support.check_python_version check_python_version(package="google.api_core") From 0a3434e292c5f0e3e08e1c814b8380b2bb1ee046 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 10:02:59 -0700 Subject: [PATCH 14/19] Fix mypy failures --- google/api_core/_python_version_support.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/google/api_core/_python_version_support.py b/google/api_core/_python_version_support.py index 1b77fe04..f7d2d565 100644 --- a/google/api_core/_python_version_support.py +++ b/google/api_core/_python_version_support.py @@ -19,7 +19,7 @@ import logging import sys import textwrap -from typing import NamedTuple, Optional, Dict, Tuple +from typing import Any, NamedTuple, Optional, Dict, Tuple class PythonVersionStatus(enum.Enum): @@ -126,7 +126,7 @@ def _get_pypi_package_name(module_name): return None -def _get_distribution_and_import_packages(import_package: str) -> Optional[str]: +def _get_distribution_and_import_packages(import_package: str) -> Tuple[str, Any]: """Return a pretty string with distribution & import package names.""" distribution_package = _get_pypi_package_name(import_package) dependency_distribution_and_import_packages = ( @@ -138,7 +138,7 @@ def _get_distribution_and_import_packages(import_package: str) -> Optional[str]: def check_python_version( - package: Optional[str] = "this package", today: Optional[datetime.date] = None + package: str = "this package", today: Optional[datetime.date] = None ) -> PythonVersionStatus: """Check the running Python version and issue a support warning if needed. @@ -188,7 +188,7 @@ def min_python(date: datetime.date) -> str: f""" You are using a non-supported Python version ({py_version_str}). Google will not post any further - updates to {package_label}. We suggest you upgrade to the + updates to {package_label}. Please upgrade to the latest Python version, or at least Python {min_python(today)}, and then update {package_label}. """ @@ -203,7 +203,7 @@ def min_python(date: datetime.date) -> str: You are using a Python version ({py_version_str}) past its end of life. Google will update {package_label} with critical bug fixes on a best-effort basis, but not - with any other fixes or features. We suggest you upgrade + with any other fixes or features. Please upgrade to the latest Python version, or at least Python {min_python(today)}, and then update {package_label}. """ @@ -214,10 +214,10 @@ def min_python(date: datetime.date) -> str: if gapic_deprecation <= today <= gapic_end: message = _flatten_message( f""" - You are using a Python version ({py_version_str}), + You are using a Python version ({py_version_str}) which Google will stop supporting in {package_label} when - it reaches its end of life ({version_info.python_eol}). We - suggest you upgrade to the latest Python version, or at + it reaches its end of life ({version_info.python_eol}). Please + upgrade to the latest Python version, or at least Python {min_python(version_info.python_eol)}, and then update {package_label}. """ From 17ef4f0a246da798bb6c81d06a78bc89dddac609 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 12:10:15 -0700 Subject: [PATCH 15/19] Try to remove a round-off error causing a test mock failure --- tests/unit/gapic/test_method.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 8896429c..ca6a2e71 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -198,10 +198,11 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): method, default_retry, default_timeout ) - result = wrapped_method(timeout=22) + specified_timeout = 22 + result = wrapped_method(timeout=specified_timeout) assert result == 42 - method.assert_called_once_with(timeout=22, metadata=mock.ANY) + method.assert_called_once_with(timeout=specified_timeout, metadata=mock.ANY) def test_wrap_method_with_call(): From 6d077fec19b737055113168153eeb0160bcb86c5 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 12:55:07 -0700 Subject: [PATCH 16/19] Remove asyncio mark from non-async test function --- tests/asyncio/test_operation_async.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index 9d9fb5d2..d777d64f 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -84,7 +84,6 @@ async def test_constructor(): assert await future.running() -@pytest.mark.asyncio def test_metadata(): expected_metadata = struct_pb2.Struct() future, _, _ = make_operation_future( From 57594fd7b5db2e08e5cae8f8a9f618d02e99b32d Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 15:18:01 -0700 Subject: [PATCH 17/19] Remov emore potential test failures/warnings Removed @pytest.mark.asyncio Defined literl timeouts as a specifie_timeout local variable --- tests/asyncio/test_operation_async.py | 1 - tests/unit/gapic/test_method.py | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/asyncio/test_operation_async.py b/tests/asyncio/test_operation_async.py index d777d64f..939be094 100644 --- a/tests/asyncio/test_operation_async.py +++ b/tests/asyncio/test_operation_async.py @@ -176,7 +176,6 @@ async def test_unexpected_result(unused_sleep): assert "Unexpected state" in "{!r}".format(exception) -@pytest.mark.asyncio def test_from_gapic(): operation_proto = make_operation_proto(done=True) operations_client = mock.create_autospec( diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index ca6a2e71..191c96c7 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -177,16 +177,19 @@ def test_wrap_method_with_overriding_retry_timeout_compression(unused_sleep): method, default_retry, default_timeout, default_compression ) + specified_timeout = 22 result = wrapped_method( retry=retry.Retry(retry.if_exception_type(exceptions.NotFound)), - timeout=timeout.ConstantTimeout(22), + timeout=timeout.ConstantTimeout(specified_timeout), compression=grpc.Compression.Deflate, ) assert result == 42 assert method.call_count == 2 method.assert_called_with( - timeout=22, compression=grpc.Compression.Deflate, metadata=mock.ANY + timeout=specified_timeout, + compression=grpc.Compression.Deflate, + metadata=mock.ANY ) From cfbc10c8a82171f9cd9abe73248e9fd32549c4c4 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 15:22:41 -0700 Subject: [PATCH 18/19] Format --- tests/unit/gapic/test_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 191c96c7..1e1703bb 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -189,7 +189,7 @@ def test_wrap_method_with_overriding_retry_timeout_compression(unused_sleep): method.assert_called_with( timeout=specified_timeout, compression=grpc.Compression.Deflate, - metadata=mock.ANY + metadata=mock.ANY, ) From bf6792f75b5414ccec5c2315cef02c7930bc5704 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Tue, 12 Aug 2025 15:52:36 -0700 Subject: [PATCH 19/19] Try making the specified_timeout a float --- tests/unit/gapic/test_method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 1e1703bb..3b31244b 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -177,7 +177,7 @@ def test_wrap_method_with_overriding_retry_timeout_compression(unused_sleep): method, default_retry, default_timeout, default_compression ) - specified_timeout = 22 + specified_timeout = 22.0 result = wrapped_method( retry=retry.Retry(retry.if_exception_type(exceptions.NotFound)), timeout=timeout.ConstantTimeout(specified_timeout), @@ -201,7 +201,7 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): method, default_retry, default_timeout ) - specified_timeout = 22 + specified_timeout = 22.0 result = wrapped_method(timeout=specified_timeout) assert result == 42