Skip to content

fix: enum coercion at call sites in 3 mypy-excluded parsers/validators - #210

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

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

Conversation

@Sinity

@Sinity Sinity commented Apr 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Three excluded files each passing a bare `str` where the declared type is a Literal/Enum. Gate grows from 398 → 401 under `mypy --strict`.

Problem

  • `polylogue/sources/parsers/claude_common.py:98` and `polylogue/sources/parsers/claude_code_parser.py:71` — `ParsedContentBlock(type="text", ...)`. The field is declared `type: ContentBlockType`. Pydantic has a `coerce_type` validator that accepts str at runtime, but mypy sees the annotated field type.
  • `polylogue/schemas/validator.py:81` — `extract_payload_samples` takes `sample_granularity: Literal["document", "record"]`. The caller only checked `isinstance(granularity, str)`; a schema with an arbitrary string value would have propagated downstream.

Solution

  • Parsers: pass `ContentBlockType.TEXT` directly instead of `"text"`. Add `ContentBlockType` to the existing `polylogue.types` import in `claude_code_parser.py`; `claude_common.py` didn't import from `polylogue.types` previously — add that import too.
  • validator: constrain to the two known Literal values. Accept `raw_granularity == "record"` or `"document"`; anything else (including non-str) falls through to the provider-based default. Small behaviour tightening: a schema carrying a typo like `"records"` now takes the provider-based default instead of passing through to `extract_payload_samples` where it would have been accepted as a str and broken downstream.

Verification

  • `nix develop -c mypy --config-file pyproject.toml polylogue/sources/parsers/claude_code_parser.py polylogue/sources/parsers/claude_common.py polylogue/schemas/validator.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.

🤖 Generated with Claude Code

Three more single-error excluded files. Each was assigning a bare `str`
where a Literal/Enum was required. Gate grows from 398 → 401 files.

- polylogue/sources/parsers/claude_common.py: ParsedContentBlock.type is
  declared `ContentBlockType` (not str). Pydantic's `coerce_type`
  validator accepts str at runtime, but the type system sees the
  annotated field type. Pass `ContentBlockType.TEXT` directly instead of
  the raw string "text".

- polylogue/sources/parsers/claude_code_parser.py: same fix as above;
  add ContentBlockType to the existing `polylogue.types` import.

- polylogue/schemas/validator.py: `extract_payload_samples` expects
  `sample_granularity: Literal["document", "record"]`. The old code
  fell through `isinstance(granularity, str)` without constraining
  the string. Widen the fallback: accept the literal strings when
  present in the schema, otherwise derive from provider membership in
  `_RECORD_VALIDATION_PROVIDERS`. A schema with an unexpected string
  value now falls through to the provider-based default instead of
  being silently propagated downstream.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: c7d154d3-3e89-41bb-8aad-bb865ade6fde

📥 Commits

Reviewing files that changed from the base of the PR and between a7e43cd and d3cba09.

📒 Files selected for processing (4)
  • polylogue/schemas/validator.py
  • polylogue/sources/parsers/claude_code_parser.py
  • polylogue/sources/parsers/claude_common.py
  • pyproject.toml
💤 Files with no reviewable changes (1)
  • pyproject.toml

📝 Walkthrough

Walkthrough

These changes enhance type safety by normalizing granularity configuration to explicit enum types in the validator, replacing string literals with typed enum values in Claude parsers, and enabling stricter mypy analysis on previously excluded modules.

Changes

Cohort / File(s) Summary
Granularity Type Normalization
polylogue/schemas/validator.py
Added validation logic to read x-polylogue-sample-granularity header and normalize it to Literal["document", "record"] with provider-based fallback defaults, replacing unvalidated string acceptance.
Enum Type Usage in Parsers
polylogue/sources/parsers/claude_code_parser.py, polylogue/sources/parsers/claude_common.py
Replaced literal string "text" with ContentBlockType.TEXT enum value when constructing fallback ParsedContentBlock objects.
Type Checking Configuration
pyproject.toml
Removed three file patterns from tool.mypy.exclude, enabling strict mypy type checking on previously excluded modules.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 With enums in place and types made clear,
No strings left loose to interfere—
Type safety hops from tree to tree,
Mypy checks now run wild and free!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 accurately summarizes the main changes: fixing enum coercion issues in three mypy-excluded files by using proper enum values instead of string literals.
Description check ✅ Passed The description fully covers all required template sections: Summary explains what changed, Problem details the issues, Solution describes the fixes, and Verification shows test results. Risks section appropriately omitted as stated.

✏️ 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-5

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

@Sinity
Sinity merged commit 929fa0d into master Apr 17, 2026
14 checks passed
@Sinity
Sinity deleted the feature/chore/mypy-tranche-5 branch April 17, 2026 13:49
Sinity added a commit that referenced this pull request Apr 22, 2026
#210)

## Summary

Three excluded files each passing a bare \`str\` where the declared type is a Literal/Enum. Gate grows from 398 → 401 under \`mypy --strict\`.

## Problem

- **\`polylogue/sources/parsers/claude_common.py:98\`** and **\`polylogue/sources/parsers/claude_code_parser.py:71\`** — \`ParsedContentBlock(type="text", ...)\`. The field is declared \`type: ContentBlockType\`. Pydantic has a \`coerce_type\` validator that accepts str at runtime, but mypy sees the annotated field type.
- **\`polylogue/schemas/validator.py:81\`** — \`extract_payload_samples\` takes \`sample_granularity: Literal["document", "record"]\`. The caller only checked \`isinstance(granularity, str)\`; a schema with an arbitrary string value would have propagated downstream.

## Solution

- **Parsers**: pass \`ContentBlockType.TEXT\` directly instead of \`"text"\`. Add \`ContentBlockType\` to the existing \`polylogue.types\` import in \`claude_code_parser.py\`; \`claude_common.py\` didn't import from \`polylogue.types\` previously — add that import too.
- **validator**: constrain to the two known Literal values. Accept \`raw_granularity == "record"\` or \`"document"\`; anything else (including non-str) falls through to the provider-based default. Small behaviour tightening: a schema carrying a typo like \`"records"\` now takes the provider-based default instead of passing through to \`extract_payload_samples\` where it would have been accepted as a str and broken downstream.

## Verification

- \`nix develop -c mypy --config-file pyproject.toml polylogue/sources/parsers/claude_code_parser.py polylogue/sources/parsers/claude_common.py polylogue/schemas/validator.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.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@Sinity Sinity added tool:claude Claude type:fix Bug fix labels Apr 22, 2026
Sinity added a commit that referenced this pull request Apr 22, 2026
#210)

Three excluded files each passing a bare `str` where the declared
type is a Literal / Enum.

`sources/parsers/claude_common.py` and
`sources/parsers/claude_code_parser.py`:
`ParsedContentBlock(type="text", ...)` where the field is declared
`type: ContentBlockType`. Pydantic's `coerce_type` validator
accepts str at runtime, but mypy sees the annotated field type.
Pass `ContentBlockType.TEXT` directly; add the `polylogue.types`
import where missing.

`schemas/validator.py`: `extract_payload_samples` declares
`sample_granularity: Literal["document", "record"]`, but the caller
only checked `isinstance(granularity, str)` — a schema with an
arbitrary string value would have propagated downstream. Constrain
to the two Literal values; anything else falls through to the
provider-based default. Behavior tightening: a schema with a typo
like `"records"` now takes the provider default instead of passing
through as str and breaking downstream.

Gate grows from 398 → 401 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
#210)

Three excluded files each passing a bare `str` where the declared
type is a Literal / Enum.

`sources/parsers/claude_common.py` and
`sources/parsers/claude_code_parser.py`:
`ParsedContentBlock(type="text", ...)` where the field is declared
`type: ContentBlockType`. Pydantic's `coerce_type` validator
accepts str at runtime, but mypy sees the annotated field type.
Pass `ContentBlockType.TEXT` directly; add the `polylogue.types`
import where missing.

`schemas/validator.py`: `extract_payload_samples` declares
`sample_granularity: Literal["document", "record"]`, but the caller
only checked `isinstance(granularity, str)` — a schema with an
arbitrary string value would have propagated downstream. Constrain
to the two Literal values; anything else falls through to the
provider-based default. Behavior tightening: a schema with a typo
like `"records"` now takes the provider default instead of passing
through as str and breaking downstream.

Gate grows from 398 → 401 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
#210)

Three excluded files each passing a bare `str` where the declared
type is a Literal / Enum.

`sources/parsers/claude_common.py` and
`sources/parsers/claude_code_parser.py`:
`ParsedContentBlock(type="text", ...)` where the field is declared
`type: ContentBlockType`. Pydantic's `coerce_type` validator
accepts str at runtime, but mypy sees the annotated field type.
Pass `ContentBlockType.TEXT` directly; add the `polylogue.types`
import where missing.

`schemas/validator.py`: `extract_payload_samples` declares
`sample_granularity: Literal["document", "record"]`, but the caller
only checked `isinstance(granularity, str)` — a schema with an
arbitrary string value would have propagated downstream. Constrain
to the two Literal values; anything else falls through to the
provider-based default. Behavior tightening: a schema with a typo
like `"records"` now takes the provider default instead of passing
through as str and breaking downstream.

Gate grows from 398 → 401 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. Swish!

ℹ️ 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 requested a review from Copilot May 1, 2026 01:51
@Sinity
Sinity removed the request for review from Copilot May 1, 2026 02:15
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.

1 participant