Skip to content

fix: narrow object-valued Row/sentinel access in 3 storage files - #212

Merged
Sinity merged 1 commit into
masterfrom
feature/chore/mypy-tranche-7
Apr 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/chore/mypy-tranche-7

Conversation

@Sinity

@Sinity Sinity commented Apr 17, 2026 •

Copy link
Copy Markdown
Owner

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 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]>
@coderabbitai

coderabbitai Bot commented Apr 17, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

These 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

Cohort / File(s) Summary
Defensive Type Checking
polylogue/storage/backends/queries/raw_state.py, polylogue/storage/search_query_support.py, polylogue/storage/session_product_status.py
Type checks added before conversions: apply_raw_state_update handles _RawStateUnset instances; sort_key_to_iso validates type before float() conversion; _to_int validates first value type before integer conversion.
Mypy Configuration
pyproject.toml
Removed mypy exclusions for the three updated storage modules, enabling stricter type checking on those files.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Type-safe checks now guard the way,
No more silent failures in the fray!
Mypy watches with a stricter eye,
These modules strengthened—errors won't fly! 📋✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: narrowing object-valued access patterns in three storage files to satisfy mypy strict-mode checking.
Description check ✅ Passed The description comprehensively covers all required sections: Summary explains what changed and why (mypy strict-mode gate increase), Problem details the three specific type-narrowing issues, Solution describes each fix applied, and Verification provides exact commands run and their results.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/chore/mypy-tranche-7

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7ce64da and 1cb23fb.

📒 Files selected for processing (4)
  • polylogue/storage/backends/queries/raw_state.py
  • polylogue/storage/search_query_support.py
  • polylogue/storage/session_product_status.py
  • pyproject.toml
💤 Files with no reviewable changes (1)
  • pyproject.toml

Comment on lines +257 to +259
if isinstance(value, (int, float, str)):
return int(value)
return 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

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.

@Sinity
Sinity merged commit 2331044 into master Apr 17, 2026
14 checks passed
@Sinity
Sinity deleted the feature/chore/mypy-tranche-7 branch April 17, 2026 16:12
Sinity added a commit that referenced this pull request Apr 22, 2026
## 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.
Sinity added a commit that referenced this pull request Apr 22, 2026
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.
Sinity added a commit that referenced this pull request Apr 22, 2026
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.
Sinity added a commit that referenced this pull request Apr 22, 2026
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.
@Sinity

Sinity commented May 1, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@Sinity
Sinity removed the request for review from Copilot May 1, 2026 01:05
@Sinity

Sinity commented May 1, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@Sinity
Sinity requested a review from Copilot May 1, 2026 01:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants