Skip to content

refactor(ogc): clarify module boundaries#346

Draft
thodson-usgs wants to merge 7 commits into
DOI-USGS:mainfrom
thodson-usgs:refactor/ogc-module-boundaries
Draft

refactor(ogc): clarify module boundaries#346
thodson-usgs wants to merge 7 commits into
DOI-USGS:mainfrom
thodson-usgs:refactor/ogc-module-boundaries

Conversation

@thodson-usgs

Copy link
Copy Markdown
Collaborator

Summary

Structural refactoring of the OGC chunking module to clarify responsibilities and reduce coupling. No behavioral changes — all existing tests pass (one test updated to reflect the live-view simplification in item 3).

Includes the perf commit from #345 as the base.

Changes

1. Extract combining.py from planning.py (item 2)

Moves response/frame recombination helpers (_combine_chunk_frames, _combine_chunk_responses, _merge_response, _lowest_remaining, _safe_elapsed, _QUOTA_HEADER) into a dedicated combining.py. planning.py re-exports them for backwards compatibility.

Rationale: planning.py was conflating "what to split" with "how to reassemble" — two concerns with different reasons to change.

2. Move _Fetch/_Finalize/_passthrough_result into chunking.py (item 3)

These type aliases define ChunkedCall's contract, not anything about interruptions. They lived in interruptions.py only to break a circular import. Now they're in chunking.py where they belong.

3. Simplify ChunkInterrupted by dropping snapshot copies (item 5)

ChunkInterrupted.__init__ used to .copy() the partial frame at raise time so exc.partial_frame was a frozen snapshot separate from exc.call.partial_frame (the live view). The distinction was subtle and unlikely to be used by callers (the primary pattern is exc.call.resume()), and cost a full-frame copy proportional to completed data.

Now partial_frame and partial_response are properties that delegate to self.call — same as the live view, no memory copy. Pickle still works (snapshots at serialize time).

4. Unify error classification into shared _classify_transient helper (item 6)

_classify_chunk_error (chain-walk) and _retryable (top-level only) shared ~80% of their logic. Created a shared _classify_transient(exc) that classifies a single exception, so adding a new transient type only needs one update.

Deferred to follow-up PRs

  • Extract gather_paginated primitive — unifying ChunkedCall._run and wateruse._fan_out (invasive; needs careful testing of the retry/interruption model)
  • Split engine.py — separating request construction, pagination, and argument normalization (~750-line module with 3 concerns)

thodson-usgs and others added 7 commits July 23, 2026 09:45
- Replace pd.json_normalize with pd.DataFrame in _get_resp_data.
  OGC API properties are flat dicts; json_normalize's recursive
  flattening is unnecessary overhead (~2.4× faster per page:
  3.2 ms → 1.35 ms on 2500-feature pages).

- Skip drop_duplicates in _combine_chunk_frames when the plan has
  no filter axis. List-axis chunks produce non-overlapping
  partitions, so dedup is pure overhead in the common
  parallel_chunks case (~2.7× faster combine: 3.4 ms → 1.25 ms
  on 52k-row results).

- Add ChunkPlan.has_filter_axis property to distinguish plans that
  need dedup (filter-axis overlap possible) from those that don't
  (list-axis only).
Move response/frame recombination helpers (_combine_chunk_frames,
_combine_chunk_responses, _merge_response, _lowest_remaining,
_safe_elapsed, _set_response_url, _QUOTA_HEADER) into a dedicated
combining.py. planning.py re-exports them for backwards compat.

Separates 'what to split' (planning) from 'how to reassemble'
(combining), each with its own reason to change.
…g.py

These type aliases define ChunkedCall's contract, not anything about
interruptions. They lived in interruptions.py only to avoid a circular
import. Move them to chunking.py where they belong; interruptions.py
keeps TYPE_CHECKING re-exports for backwards compat.
…perties

Drop the .copy() snapshot of call.partial_frame and the stored
partial_response from ChunkInterrupted.__init__. Instead, partial_frame
and partial_response are now @Property delegates to self.call when
available, falling back to empty/None when call is None.

This avoids copying potentially large DataFrames on every
ChunkInterrupted raise — the primary usage pattern is exc.call.resume(),
not inspecting the partial data on the exception itself.

Pickling still works: __getstate__ snapshots the current partial_frame
and partial_response into private backing attributes that __setstate__
restores, so the degraded (call=None) unpickled form retains the data
that was available at serialization time.
…helper

Extract the per-exception type matching from _classify_chunk_error into
a new _classify_transient(exc) helper that classifies a SINGLE exception
(no chain walk) into (interrupted_class, retry_after) | None.

_classify_chunk_error now calls _classify_transient on each step of its
__cause__ walk. _retryable continues to inspect only the top-level
exception (preserving the intentional divergence: initial-page raw
transients are cheap to retry; wrapped mid-pagination failures escalate
to a resumable ChunkInterrupted instead).

When a new transient error type needs recognition, it now goes in ONE
place (_classify_transient) and both the retry and escalation paths pick
it up automatically.
…hims

Follow-up cleanup on the module-boundary refactor so the split lands in
the import graph, not just the file layout.

- Import the recombination helpers straight from ogc.combining at every
  call site (chunking, engine, wateruse, tests) and delete planning.py's
  backwards-compat re-export block + __all__. The new module previously
  had zero direct importers; everything routed through planning.

- Drop the dead TYPE_CHECKING re-export of _Fetch/_Finalize/
  _passthrough_result from interruptions.py (no importers) and repoint the
  lone Sphinx xref in shaping.py to ogc.chunking._Finalize, their real home.

- Remove ChunkInterrupted.__setstate__: __getstate__ already writes the
  _pickled_* snapshot keys and the base DataRetrievalError.__setstate__
  restores them via __dict__.update, so the override was redundant (the
  property getattr fallbacks cover the hand-constructed case).

- Correct _classify_transient's docstring and _retryable's comment, which
  claimed a delegation that does not — and deliberately should not — exist
  (the two use intentionally different type sets).

No behavior change; mypy --strict and the affected test suites pass.

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

Revert the live-view change (item 3 of DOI-USGS#346): restore exc.partial_frame /
exc.partial_response as raise-time snapshots rather than properties that
delegate to exc.call.

Snapshot is the better contract for an exception — a record of the failure
moment, not a live handle:

- In a resume loop each interruption stays a faithful record of what it saw.
  Live-view aliased every exception to the one shared call, so after a second
  failure exc1.partial_frame and exc2.partial_frame returned the same (later)
  state.
- After a successful resume, live-view's .partial_frame returned the COMPLETE
  result — a field named "partial" that no longer was.
- Exception payloads shouldn't mutate after you catch them (cf.
  CalledProcessError.output).

Removing the delegation also drops the properties and the _pickled_* /
__getstate__ snapshot machinery it required: partial_frame/partial_response
are plain instance attributes again, pickled by the base __getstate__
(net -21 lines in interruptions.py).

No behavior change versus released main — this PR is unmerged, so live-view
never shipped. DOI-USGS#346 is now a true no-behavioral-change refactor.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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