Skip to content

deps: Updates deprecation warning to FutureWarning re: 3.7 and 3.8 #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions db_dtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,22 @@ def __sub__(self, other):
return super().__sub__(other)


sys_major, sys_minor, sys_micro = _versions_helpers.extract_runtime_version()
if sys_major == 3 and sys_minor in (7, 8):
warnings.warn(
"The python-bigquery library will stop supporting Python 3.7 "
"and Python 3.8 in a future major release expected in Q4 2024. "
f"Your Python version is {sys_major}.{sys_minor}.{sys_micro}. We "
"recommend that you update soon to ensure ongoing support. For "
"more details, see: [Google Cloud Client Libraries Supported Python Versions policy](https://cloud.google.com/python/docs/supported-python-versions)",
PendingDeprecationWarning,
)
def _check_python_version():
"""Checks the runtime Python version and issues a warning if needed."""
sys_major, sys_minor, sys_micro = _versions_helpers.extract_runtime_version()
if sys_major == 3 and sys_minor in (7, 8):
warnings.warn(
"The python-bigquery library as well as the python-db-dtypes-pandas library no "
"longer supports Python 3.7 and Python 3.8. "
f"Your Python version is {sys_major}.{sys_minor}.{sys_micro}. We "
"recommend that you update soon to ensure ongoing support. For "
"more details, see: [Google Cloud Client Libraries Supported Python Versions policy](https://cloud.google.com/python/docs/supported-python-versions)",
FutureWarning,
stacklevel=2, # Point warning to the caller of __init__
)


_check_python_version()


if not JSONArray or not JSONDtype:
Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def default(session, tests_path):
session.run(
"py.test",
"--quiet",
"-W default::PendingDeprecationWarning",
"-W default::FutureWarning",
f"--junitxml={os.path.split(tests_path)[-1]}_{session.python}_sponge_log.xml",
"--cov=db_dtypes",
"--cov=tests/unit",
Expand Down Expand Up @@ -265,7 +265,7 @@ def prerelease(session, tests_path):
session.run(
"py.test",
"--quiet",
"-W default::PendingDeprecationWarning",
"-W default::FutureWarning",
f"--junitxml={os.path.split(tests_path)[-1]}_prerelease_{session.python}_sponge_log.xml",
"--cov=db_dtypes",
"--cov=tests/unit",
Expand Down
85 changes: 85 additions & 0 deletions tests/unit/test__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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.

from unittest import mock

import pytest

# Module paths used for mocking
MODULE_PATH = "db_dtypes"
HELPER_MODULE_PATH = f"{MODULE_PATH}._versions_helpers"
MOCK_EXTRACT_VERSION = f"{HELPER_MODULE_PATH}.extract_runtime_version"
MOCK_WARN = "warnings.warn" # Target the standard warnings module


@pytest.mark.parametrize(
"mock_version_tuple, version_str",
[
((3, 7, 10), "3.7.10"),
((3, 7, 0), "3.7.0"),
((3, 8, 5), "3.8.5"),
((3, 8, 12), "3.8.12"),
],
)
def test_check_python_version_warns_on_unsupported(mock_version_tuple, version_str):
"""
Test that _check_python_version issues a FutureWarning for Python 3.7/3.8.
"""

from db_dtypes import _check_python_version

# Mock the helper function it calls and the warnings.warn function
with mock.patch(MOCK_EXTRACT_VERSION, return_value=mock_version_tuple), mock.patch(
MOCK_WARN
) as mock_warn_call:
Comment on lines +43 to +45
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if you've tried pytest.warns context manager rather than mocking out warnings.warn? https://docs.pytest.org/en/stable/how-to/capture-warnings.html#warns

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the pytest.warns context manager in several places. During troubleshooting this, we tried many, many approaches and all of them failed so we swapped out a ton of things to try and find a solution that ended up working.

  • dependencies and import timing will be the death of me.

_check_python_version() # Call the function

# Assert that warnings.warn was called exactly once
mock_warn_call.assert_called_once()

# Check the arguments passed to warnings.warn
args, kwargs = mock_warn_call.call_args
assert len(args) >= 1 # Should have at least the message
warning_message = args[0]
warning_category = args[1] if len(args) > 1 else kwargs.get("category")

# Verify message content and category
assert "longer supports Python 3.7 and Python 3.8" in warning_message
assert warning_category == FutureWarning


@pytest.mark.parametrize(
"mock_version_tuple",
[
(3, 9, 1),
(3, 10, 0),
(3, 11, 2),
(3, 12, 0),
],
)
def test_check_python_version_does_not_warn_on_supported(mock_version_tuple):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a pytest.skip for older Pythons if the extra warning is causing trouble here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a pytest issue, per se. i.e. it is not the test that triggers the problem and thus we can't mark a specific test as .skipif(sys.version <= (3, 8)).

When we run a file such as test_arrow, line 24 is import db-dtypes and the process of importing triggers the warning if we run under Py 3.7 and 3.8, which we definitely do in this PR cause another PR removes dependencies on Py37 and Py38.

So this is kind of a catch-22.

"""
Test that _check_python_version does NOT issue a warning for other versions.
"""

from db_dtypes import _check_python_version

# Mock the helper function it calls and the warnings.warn function
with mock.patch(MOCK_EXTRACT_VERSION, return_value=mock_version_tuple), mock.patch(
MOCK_WARN
) as mock_warn_call:
_check_python_version()

# Assert that warnings.warn was NOT called
mock_warn_call.assert_not_called()