Skip to content

fix: resolve CI failures, drop Python 3.9, and clean up test deprecations#1504

Open
aseering wants to merge 15 commits intogoogleapis:mainfrom
aseering:fix/test-failures
Open

fix: resolve CI failures, drop Python 3.9, and clean up test deprecations#1504
aseering wants to merge 15 commits intogoogleapis:mainfrom
aseering:fix/test-failures

Conversation

@aseering
Copy link

@aseering aseering commented Feb 27, 2026

This PR addresses several underlying issues causing CI flakiness and hard failures, alongside general code health improvements to clean up the test
output.

Changes Made:

  • Fix Spanner Metrics Exporter Crash (fix)

    • Issue: The OpenTelemetry background thread was attempting to export project-level metrics using the spanner_instance_client resource type,
      which requires an instance_id. Missing labels caused 400 InvalidArgument errors, crashing the thread and failing the Nox session.
    • Fix: Updates CloudMonitoringMetricsExporter to inject project_id and silently drop metrics lacking an instance_id.
  • Drop Python 3.9 Support (build)

    • Issue: Kokoro environment issues with unit-3.9 and outdated infrastructure.
    • Fix: Performed a comprehensive removal of Python 3.9 support across noxfile.py, setup.py, .kokoro/ configurations, .librarian/ (synthtool
      inputs), and GitHub Actions.
  • Fix SyntaxWarning in cursor.py (fix)

    • Issue: Python 3.14 emits a warning for return inside a finally block as it swallows exceptions.
    • Fix: Moved return rows out of the finally block in google/cloud/spanner_dbapi/cursor.py.
  • Resolve Deprecation Warnings & Pytest Issues (chore)

    • Issue: Deprecated datetime.utcnow() and pytest collection warnings for classes starting with Test.
    • Fix: Migrated to timezone-aware datetime.now(timezone.utc) and renamed TestCredentials to MockCredentials.

Prevents swallowing exceptions and fixes SyntaxWarning in Python 3.14.
- Replaces deprecated datetime.utcnow() with timezone-aware datetime.now(timezone.utc).
- Renames MockCredentials and MockClass to avoid pytest/unittest collection warnings.
Silently drops metrics missing instance_id to prevent 400 InvalidArgument errors from Cloud Monitoring for project-level operations.
@aseering aseering requested review from a team as code owners February 27, 2026 14:49
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Feb 27, 2026
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/python-spanner API. label Feb 27, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the stability and maintainability of the codebase by addressing critical CI failures and cleaning up test output. It resolves a metrics exporter crash, removes support for an outdated Python version, fixes a SyntaxWarning, and updates deprecated datetime usage and pytest class naming conventions. The overall impact is a more robust and less noisy development and testing environment.

Highlights

  • Spanner Metrics Exporter Crash Fixed: Resolved an issue where the OpenTelemetry background thread crashed due to invalid metrics lacking 'instance_id', by ensuring 'project_id' injection and silently dropping problematic metrics.
  • Python 3.9 Support Dropped: Removed Python 3.9 from CI configurations and noxfile.py due to environment support issues, streamlining the build process.
  • SyntaxWarning in cursor.py Resolved: Addressed a Python 3.14 SyntaxWarning by moving the 'return rows' statement out of a 'finally' block in google/cloud/spanner_dbapi/cursor.py.
  • Deprecation Warnings & Pytest Issues Cleaned Up: Migrated 'datetime.utcnow()' to timezone-aware 'datetime.now(timezone.utc)' and renamed 'TestCredentials' to 'MockCredentials' to eliminate test log noise and pytest collection warnings.
Changelog
  • google/cloud/spanner_dbapi/cursor.py
    • Moved 'return rows' statement outside the 'finally' block to prevent SyntaxWarning.
  • google/cloud/spanner_v1/metrics/metrics_exporter.py
    • Converted a static method to an instance method.
    • Injected 'project_id' into monitored resource labels.
    • Added logic to filter out metrics missing 'instance_id' to prevent exporter crashes.
  • google/cloud/spanner_v1/pool.py
    • Updated the '_NOW' function to use 'datetime.now(datetime.timezone.utc)' instead of 'datetime.utcnow'.
  • google/cloud/spanner_v1/session.py
    • Imported 'timezone' from 'datetime'.
    • Updated '_last_use_time' initialization to use 'datetime.now(timezone.utc)'.
  • tests/system/test_dbapi.py
    • Replaced 'datetime.datetime.utcnow().replace(tzinfo=UTC)' with 'datetime.datetime.now(UTC)'.
  • tests/system/test_session_api.py
    • Replaced 'datetime.datetime.utcnow().replace(tzinfo=UTC)' with 'datetime.datetime.now(UTC)' in multiple test cases.
  • tests/unit/test__helpers.py
    • Renamed the internal test class 'test_class' to 'MockClass' and updated all references to it.
  • tests/unit/test_backup.py
    • Updated timestamp creation to use 'datetime.datetime.now(UTC)' and 'datetime.now(timezone.utc)'.
  • tests/unit/test_batch.py
    • Replaced 'datetime.datetime.utcnow().replace(tzinfo=UTC)' with 'datetime.datetime.now(UTC)' in several commit-related tests.
  • tests/unit/test_database.py
    • Updated timestamp creation to use 'datetime.datetime.now(UTC)' in various test methods.
  • tests/unit/test_instance.py
    • Updated timestamp creation to use 'datetime.datetime.now(UTC)'.
  • tests/unit/test_metrics.py
    • Renamed 'TestCredentials' to 'MockCredentials'.
    • Updated client initialization to use 'MockCredentials'.
    • Added 'patch' context managers for metric service client and exporter.
  • tests/unit/test_pool.py
    • Imported 'timezone' from 'datetime'.
    • Updated 'datetime.utcnow()' calls to 'datetime.now(timezone.utc)' or 'datetime.now(UTC)' in session pool tests.
  • tests/unit/test_session.py
    • Replaced 'datetime.datetime.utcnow().replace(tzinfo=UTC)' with 'datetime.datetime.now(UTC)' in numerous transaction-related tests.
Activity
  • No specific human activity (comments, reviews, progress updates) has been recorded for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a set of valuable changes that address CI failures, remove support for Python 3.9, and clean up various deprecation warnings in the test suite. The fix for the Spanner Metrics Exporter crash, by ensuring project_id is present and filtering out metrics lacking an instance_id, is well-implemented and correctly addresses the underlying issue. The codebase is also improved by replacing deprecated datetime.utcnow() calls with the timezone-aware datetime.now(timezone.utc) and resolving a SyntaxWarning in cursor.py. The test suite is made more robust by renaming helper classes to avoid pytest warnings. Overall, the changes are correct and improve the health and reliability of the codebase.

@aseering aseering requested a review from a team as a code owner February 27, 2026 19:04
- Fix missing imports in metrics_exporter.py

- Fix shadow datetime import in test_pool.py

- Fix exception handling in cursor.py that broke dbapi tests
@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants