Repository navigation
fix: narrow object-valued Row/sentinel access in 3 storage files - #212
Conversation
Three more single-error excluded files. Each was passing an `object` or sentinel-widened value to int()/float() which mypy rightly rejects. Gate grows from 404 → 407 files. - polylogue/storage/search_query_support.py: `sort_key_to_iso(sort_key: object)` called `float(sort_key)` directly. The try/except catches TypeError at runtime but mypy insists on a pre-check. Add `isinstance(sort_key, (int, float, str))` guard before float(). - polylogue/storage/session_product_status.py: `_to_int(row[0] or 0)` where `row[0]` is `object | Any` (sqlite3 column value). Explicitly guard on `isinstance(value, (int, float, str))` and fall back to 0 otherwise. Same runtime behavior, explicit at the type level. - polylogue/storage/backends/queries/raw_state.py: `int(state.validation_drift_count)` after `is not UNSET` still carried `_RawStateUnset` in the union because `is not UNSET` does not narrow across custom sentinels for mypy. Narrow explicitly with `isinstance(drift_count, _RawStateUnset)` and coerce via `or 0`. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughThese changes add defensive type checking to three storage module functions and remove mypy type-checking exclusions for those modules, enabling stricter static type analysis on the updated code. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@polylogue/storage/session_product_status.py`:
- Around line 257-259: _in the _to_int function the code assumes any str
converts cleanly to int which will raise ValueError for non-numeric strings like
"N/A"; wrap the conversion of value (int(value)) in a try/except catching
ValueError and TypeError and return 0 on failure so the intended fallback works;
keep the existing check for isinstance(value, (int, float, str)) but perform the
safe conversion inside the try/except block and return int(value) on success
else 0.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: aa77da6d-f059-4270-8476-ff37e4d80286
📒 Files selected for processing (4)
polylogue/storage/backends/queries/raw_state.pypolylogue/storage/search_query_support.pypolylogue/storage/session_product_status.pypyproject.toml
💤 Files with no reviewable changes (1)
- pyproject.toml
| if isinstance(value, (int, float, str)): | ||
| return int(value) | ||
| return 0 |
There was a problem hiding this comment.
Handle non-numeric strings without raising in _to_int.
Line 257 permits str, but Line 258 can still throw ValueError (e.g., "N/A"), which breaks the intended fallback-to-0 behavior.
Suggested fix
- if isinstance(value, (int, float, str)):
- return int(value)
+ if isinstance(value, (int, float)):
+ return int(value)
+ if isinstance(value, str):
+ try:
+ return int(value)
+ except ValueError:
+ return 0
return 0🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@polylogue/storage/session_product_status.py` around lines 257 - 259, _in the
_to_int function the code assumes any str converts cleanly to int which will
raise ValueError for non-numeric strings like "N/A"; wrap the conversion of
value (int(value)) in a try/except catching ValueError and TypeError and return
0 on failure so the intended fallback works; keep the existing check for
isinstance(value, (int, float, str)) but perform the safe conversion inside the
try/except block and return int(value) on success else 0.
## Summary Three more single-error excluded files in the storage layer. Each was passing an `object` or custom-sentinel value to `int()` / `float()`; mypy rejects, the try/except at runtime absorbed it silently. Gate grows from 404 → 407 under `mypy --strict`. ## Problem - `polylogue/storage/search_query_support.py:18` — `float(sort_key)` where `sort_key: object`. The try/except catches `TypeError` at runtime but mypy requires a pre-narrow. - `polylogue/storage/session_product_status.py:254` — `int(row[0] or 0)` where `row[0]` is `object | Any` from a sqlite3 Row. - `polylogue/storage/backends/queries/raw_state.py:68` — `int(state.validation_drift_count)` inside `if state.validation_drift_count is not UNSET`. `UNSET` is an instance of `_RawStateUnset`, not a Sentinel/Literal, so `is not UNSET` doesn't narrow the `_RawStateUnset` arm out of the union. ## Solution - **search_query_support**: `isinstance(sort_key, (int, float, str))` guard before `float()`; same fallback path as the try/except, but explicit at the type level. - **session_product_status**: bind `value = row[0]`, guard `isinstance(value, (int, float, str))` before `int()`, return 0 otherwise. - **raw_state**: explicit `isinstance(drift_count, _RawStateUnset)` narrowing and `or 0` for None; import `_RawStateUnset` alongside `UNSET`. ## Verification - `nix develop -c mypy --config-file pyproject.toml polylogue/storage/search_query_support.py polylogue/storage/session_product_status.py polylogue/storage/backends/queries/raw_state.py` — `Success: no issues found in 3 source files` - `nix develop -c devtools verify --quick` — format, lint, mypy, render-all --check all passed - Pre-push hook (`devtools verify --quick`) passed Full `devtools verify` (with pytest) deferred to CI.
Three storage-layer files each passing an `object` or custom- sentinel value to `int()` / `float()`. Mypy rejected; the try/except at runtime absorbed it silently. `storage/search_query_support.py`: `float(sort_key)` where `sort_key: object`. `isinstance(sort_key, (int, float, str))` guard before `float()` — same fallback path as the try/except, but explicit at the type level. `storage/session_product_status.py`: `int(row[0] or 0)` where `row[0]` is `object | Any` from a sqlite3 Row. Bind `value = row[0]`, guard `isinstance(value, (int, float, str))` before `int()`, return 0 otherwise. `storage/backends/queries/raw_state.py`: `int(state.validation_drift_count)` inside `if state.validation_drift_count is not UNSET`. `UNSET` is an instance of `_RawStateUnset`, not a Sentinel/Literal, so the identity check doesn't narrow the `_RawStateUnset` arm out of the union. Explicit `isinstance(..., _RawStateUnset)` narrowing plus `or 0` for None; import `_RawStateUnset` alongside `UNSET`. Gate grows from 404 → 407 files. Verification: mypy clean on the three touched files; devtools verify --quick green.
Three storage-layer files each passing an `object` or custom- sentinel value to `int()` / `float()`. Mypy rejected; the try/except at runtime absorbed it silently. `storage/search_query_support.py`: `float(sort_key)` where `sort_key: object`. `isinstance(sort_key, (int, float, str))` guard before `float()` — same fallback path as the try/except, but explicit at the type level. `storage/session_product_status.py`: `int(row[0] or 0)` where `row[0]` is `object | Any` from a sqlite3 Row. Bind `value = row[0]`, guard `isinstance(value, (int, float, str))` before `int()`, return 0 otherwise. `storage/backends/queries/raw_state.py`: `int(state.validation_drift_count)` inside `if state.validation_drift_count is not UNSET`. `UNSET` is an instance of `_RawStateUnset`, not a Sentinel/Literal, so the identity check doesn't narrow the `_RawStateUnset` arm out of the union. Explicit `isinstance(..., _RawStateUnset)` narrowing plus `or 0` for None; import `_RawStateUnset` alongside `UNSET`. Gate grows from 404 → 407 files. Verification: mypy clean on the three touched files; devtools verify --quick green.
Three storage-layer files each passing an `object` or custom- sentinel value to `int()` / `float()`. Mypy rejected; the try/except at runtime absorbed it silently. `storage/search_query_support.py`: `float(sort_key)` where `sort_key: object`. `isinstance(sort_key, (int, float, str))` guard before `float()` — same fallback path as the try/except, but explicit at the type level. `storage/session_product_status.py`: `int(row[0] or 0)` where `row[0]` is `object | Any` from a sqlite3 Row. Bind `value = row[0]`, guard `isinstance(value, (int, float, str))` before `int()`, return 0 otherwise. `storage/backends/queries/raw_state.py`: `int(state.validation_drift_count)` inside `if state.validation_drift_count is not UNSET`. `UNSET` is an instance of `_RawStateUnset`, not a Sentinel/Literal, so the identity check doesn't narrow the `_RawStateUnset` arm out of the union. Explicit `isinstance(..., _RawStateUnset)` narrowing plus `or 0` for None; import `_RawStateUnset` alongside `UNSET`. Gate grows from 404 → 407 files. Verification: mypy clean on the three touched files; devtools verify --quick green.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
Summary
Three more single-error excluded files in the storage layer. Each was passing an
objector custom-sentinel value toint()/float(); mypy rejects, the try/except at runtime absorbed it silently. Gate grows from 404 → 407 undermypy --strict.Problem
polylogue/storage/search_query_support.py:18—float(sort_key)wheresort_key: object. The try/except catchesTypeErrorat runtime but mypy requires a pre-narrow.polylogue/storage/session_product_status.py:254—int(row[0] or 0)whererow[0]isobject | Anyfrom a sqlite3 Row.polylogue/storage/backends/queries/raw_state.py:68—int(state.validation_drift_count)insideif state.validation_drift_count is not UNSET.UNSETis an instance of_RawStateUnset, not a Sentinel/Literal, sois not UNSETdoesn't narrow the_RawStateUnsetarm out of the union.Solution
isinstance(sort_key, (int, float, str))guard beforefloat(); same fallback path as the try/except, but explicit at the type level.value = row[0], guardisinstance(value, (int, float, str))beforeint(), return 0 otherwise.isinstance(drift_count, _RawStateUnset)narrowing andor 0for None; import_RawStateUnsetalongsideUNSET.Verification
nix develop -c mypy --config-file pyproject.toml polylogue/storage/search_query_support.py polylogue/storage/session_product_status.py polylogue/storage/backends/queries/raw_state.py—Success: no issues found in 3 source filesnix develop -c devtools verify --quick— format, lint, mypy, render-all --check all passeddevtools verify --quick) passedFull
devtools verify(with pytest) deferred to CI.