Skip to content

docs(spanner): improve Getting Started samples following code-for-llm - #1

Open
thecodewreck wants to merge 2 commits into
mainfrom
docs/spanner-getting-started-code-for-llm
Open

thecodewreck wants to merge 2 commits into
mainfrom
docs/spanner-getting-started-code-for-llm

Conversation

@thecodewreck

Copy link
Copy Markdown
Owner

Summary

Upgrades the 19 Cloud Spanner Getting Started code snippets in snippets.py, pg_snippets.py and quickstart.py following code-for-llm best practices while preserving all existing function signatures, region tags, schemas, SQL statements, and stdout output.

Key Improvements

  • Self-contained contracts inside [START ...] regions: Added Google-style docstrings (Args:, Raises: with Recovery:) to the 14 regions that contain their def, and contract comments or a callback docstring to the 5 regions that start inside a function body. Each one names the google.api_core.exceptions class raised for a missing instance, database, table, column, index or row, and for an existing database, column or index.
  • Explicit code: Added type annotations to 15 functions and 2 transaction callbacks, put from google.cloud import spanner inside each region, replaced format(*row) with named row unpacking, and passed request= and timeout= as keywords.
  • Explicit failures in write_with_dml_transaction: The transfer now raises ValueError when an album row is missing, MarketingBudget is NULL, or the second album has less than the transfer amount. Before, it raised IndexError, raised TypeError, or returned without doing anything. read_write_transaction in the same file already raises ValueError for a low budget. Output on success is unchanged.
  • Docstring fixes: Corrected "an storing index" and a reference to add_scoring_index (the function is add_storing_index).

Verification

  • Runtime: Python 3.13 with google-cloud-spanner==3.71.0 (the version pinned in samples/samples/requirements.txt)
  • black==23.7.0, ruff format and flake8 with the flags from samples/samples/noxfile.py: PASS
  • Printed output and database state of all 19 snippets on the Cloud Spanner Emulator, before and after the change, are identical: PASS
  • Existing tests for these snippets in quickstart_test.py, snippets_test.py and pg_snippets_test.py (22 tests) on the Cloud Spanner Emulator: PASS
  • Every exception class named in a new docstring or comment was triggered against the edited snippet on the Cloud Spanner Emulator and matches. The 240-second operation timeout and a transaction abort were simulated, because the emulator cannot produce them: PASS

Applies the code-for-llm guidelines to the 19 regions that the Spanner
getting-started page includes from snippets.py, pg_snippets.py and
quickstart.py.

- State arguments, raised exceptions and recovery steps in docstrings,
  or in comments where the region starts inside a function body.
- Add type annotations, region-local imports, named row unpacking, and
  the request= and timeout= keywords.
- Raise ValueError in write_with_dml_transaction for a missing album
  row, a NULL budget, or a budget below the transfer amount.
  read_write_transaction already raises ValueError for a low budget.

Function names, parameters, region tags, SQL and printed output are
unchanged.
Removes the 'from google.cloud import spanner' lines added inside 18 getting-started regions. snippets.py and pg_snippets.py already import spanner at module level. quickstart.py keeps its import, which was there before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant