Skip to content

Fix _arrange_cols: stop mutating the caller's properties list#254

Draft
thodson-usgs wants to merge 4 commits intoDOI-USGS:mainfrom
thodson-usgs:fix/arrange-cols-mutation
Draft

Fix _arrange_cols: stop mutating the caller's properties list#254
thodson-usgs wants to merge 4 commits intoDOI-USGS:mainfrom
thodson-usgs:fix/arrange-cols-mutation

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

@thodson-usgs thodson-usgs commented May 3, 2026

Summary

`_arrange_cols` did `properties.append("geometry")` and `properties[properties.index("id")] = output_id` in place. Calling any getter twice with the same `properties=[...]` therefore caused that list to grow unboundedly and have `id` rewritten across calls — a real action-at-a-distance defect for any user code that re-uses a `properties` list across requests.

This PR takes a local copy of the list and mutates that. The caller's list is untouched.

Test plan

  • New regression test calls the getter twice with the same `properties` list and asserts the list is byte-for-byte unchanged.
  • Two existing-behavior tests confirm the post-fix output: `'id'` in `properties` still resolves to the `output_id` column, and `geometry` is still tacked on when present.
  • Full local test suite passes (229 tests, with `API_USGS_PAT` set).

Related PRs

Other open PRs in this bug-review series that touch dataretrieval/waterdata/utils.py (different functions, no functional conflicts):

thodson-usgs and others added 2 commits May 3, 2026 15:20
`_arrange_cols` did `properties.append("geometry")` and
`properties[properties.index("id")] = output_id` in place. Calling any
getter twice with the same `properties=[...]` therefore caused that
list to grow unboundedly and have `id` rewritten across calls — a
real action-at-a-distance defect for any code that re-uses a
`properties` list across requests.

Take a local copy of the list and mutate that. Caller's list is
untouched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the regression-narrative phrasing for the local-copy comment and
trim the multi-line "id is technically a valid column..." block to a
two-line WHY explaining the rename intent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an action-at-a-distance bug in dataretrieval.waterdata.utils._arrange_cols where the function mutated the caller-provided properties list in-place, causing surprising behavior when the same list instance is reused across multiple requests.

Changes:

  • Stop mutating the caller’s properties list by copying it to a local list before applying id/geometry adjustments.
  • Add regression/behavior tests covering non-mutation, idoutput_id behavior, and retention of geometry when present.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
dataretrieval/waterdata/utils.py Copies properties before mutation to prevent side effects across calls.
tests/waterdata_utils_test.py Adds regression + behavior tests validating the fixed _arrange_cols semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread dataretrieval/waterdata/utils.py Outdated
@@ -687,15 +687,15 @@ def _arrange_cols(
# If properties are provided, filter to only those columns
# plus geometry if skip_geometry is False
thodson-usgs and others added 2 commits May 4, 2026 10:11
Per copilot review on PR DOI-USGS#254. _arrange_cols doesn't take skip_geometry
- it conditionally keeps the geometry column based on whether one is
present in the DataFrame.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…tion

# Conflicts:
#	tests/waterdata_utils_test.py
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.

2 participants