Deduplicate stats queries and return proper HTTP errors - #187
Merged
Conversation
Backend consistency cleanup (Tier 2), no change to successful responses:
- Collapse the near-identical `fetch_dataset_most_used_smiles_for_inputs`
and `..._for_products` into one private helper, with the input/product
table names passed as safely-composed `psycopg.sql.Identifier`s rather
than duplicated query text.
- Replace bare `ValueError`s in API handlers with `HTTPException`s so
clients get meaningful status codes instead of 500s:
- `/molfile` with an unparseable SMILES -> 400
- `/reaction_summary` for an unknown reaction_id -> 404 (and 500 only
for the genuinely-impossible "multiple matches" case)
- Add regression tests for both error paths.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #187 +/- ##
==========================================
+ Coverage 82.88% 83.08% +0.20%
==========================================
Files 16 16
Lines 1075 1082 +7
==========================================
+ Hits 891 899 +8
+ Misses 184 183 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bdeadman
approved these changes
Jun 16, 2026
This was referenced Jun 21, 2026
skearnes
added a commit
that referenced
this pull request
Jun 21, 2026
#187 introduced two HTTPException calls in view.py (and one test line in view_test.py) wider than 88 chars after #188 lowered the line length. Because it merged after #188 without rebasing, the `ruff format --check` step in the check_python CI job has been failing on main ever since. Pure `ruff format` reflow; no behavior change. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backend API consistency cleanup (Tier 2). No change to successful responses.
Changes
fetch_dataset_most_used_smiles_for_inputsand..._for_productswere identical apart from three table/column names. They now delegate to one private helper, with the differing identifiers composed viapsycopg.sql.Identifier(schema-qualified, injection-safe) instead of duplicated query text. Public function signatures are unchanged.ValueErrors (which surface as opaque 500s):/molfilewith an unparseable SMILES →400/reaction_summaryfor an unknownreaction_id→404(and500reserved for the genuinely-impossible "multiple matches" case)Deliberately deferred
The frontend fetch-error consolidation (also in the Tier 2 plan) touches
ChartView.tsxandMainDatasetView.tsx, which are actively being edited on another branch — folding it in now would create merge conflicts, so it'll come separately.Testing
ruff check/ruff format --check: cleanty check ord_interface: cleanpytest ord_interface/api(with conda postgres): 54 passing, including the 2 new error-case tests and the existing stats tests that cover the refactored helper🤖 Generated with Claude Code
Greptile Summary
This PR cleans up backend API consistency by deduplicating two identical stats SQL queries into a shared private helper and replacing bare
ValueErrorraises with proper FastAPIHTTPExceptionresponses._fetch_dataset_most_used_smilesinqueries.pycomposes schema-qualified table and column identifiers viapsycopg.sql.Identifier, correctly mirroring both original queries; the{compound}.{foreign_key}three-part reference is valid PostgreSQL./molfilewith an unparseable SMILES now returns HTTP 400;/reaction_summarywith an unknown ID returns 404, and the previously-combined guard for the impossible multi-match case is split into its own 500.Confidence Score: 5/5
Safe to merge — all changes are narrowly scoped, public function signatures are unchanged, and the refactored SQL has been validated against the two original queries it replaces.
The SQL refactoring correctly reconstructs both original queries using psycopg.sql.Identifier for all dynamic identifiers, the three-part schema.table.column reference is valid PostgreSQL, and the error-handling changes are straightforward substitutions of ValueError with typed HTTPExceptions. Regression tests confirm the new error paths.
No files require special attention.
Important Files Changed
Reviews (1): Last reviewed commit: "Deduplicate stats queries and return pro..." | Re-trigger Greptile