Skip to content

Clear all 27 mypy errors in language_lens.py (#431 session 4) - #442

Merged
squid-protocol merged 1 commit into
mainfrom
fix/431-language-lens-typing
Jul 25, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
fix/431-language-lens-typing

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Part of #429/#431, one file per session. This session: `language_lens.py` (plus `LENS_CONFIG` in `language_standards.py`, its root cause).

Root cause: `LENS_CONFIG` mixes `Set[str]`/`Dict[str,str]`/`List[Dict]`/`Dict[str,float]` values in one dict literal, widened by mypy to `Collection[object]` -- the same class of bug as #436's `RECORDING_SCHEMAS` fix, producing 16 direct errors in `language_lens.py` plus 4 more in `prism.py`. Added a `LensConfig` `TypedDict`.

Six smaller follow-on fixes, all in `language_lens.py`:

  • `target_id`: two genuinely independent uses of the same name in different branches (one always-str, one legitimately `Optional[str]`). Renamed the always-str one to `prose_target_id` instead of fighting narrowing across unrelated paths.
  • `lock_tier`: sub-tiers like 1.5/1.7 are intentional (matches `DetectorResult`'s own `lock_tier: Union[int, float]`). Annotated `float` at declaration; `_forge_result`'s `tier: int` param widened to match.
  • `local_tally`: missing annotation, `Dict[str, int]`.
  • Two `max(d, key=d.get)` calls: `dict.get` is overloaded, which mypy can't resolve when passed bare as a key function. Switched to `key=lambda k: d[k]`.
  • `regex_hits`: multiplied by a 0.7 language handicap later on. Annotated `float` at declaration.
  • `_find_balanced_end`'s `limit`: computed from `Dict[str, float]`-typed thresholds but used as a `range()` bound. Explicit `int()` cast -- a character-position limit was always meant to be whole.

157 → 126 unique baseline entries.

Test plan

  • `language_lens.py` reports zero mypy errors after this change
  • Baseline regenerated in the minimal env `mypy-audit.yml` actually uses
  • `flake8`, `dead_key_audit.py --ci`, full `pytest tests/` (866 passed, 1 deselected, 1 pre-existing xfail) all clean

Root cause + six smaller follow-on fixes:

- LENS_CONFIG (language_standards.py) mixes Set[str]/Dict[str,str]/
  List[Dict]/Dict[str,float] values in one dict literal, widened by mypy
  to Collection[object] -- the same class of bug as #436's RECORDING_SCHEMAS,
  producing 16 direct "Collection[object] has no attribute get/items"
  errors in language_lens.py plus 4 more in prism.py. Added a LensConfig
  TypedDict. All consumers already use literal string keys.

- `target_id`: two genuinely independent uses of the same name in
  different branches of one function -- one always a plain str (a
  self-contained prose-extension shortcut that returns immediately),
  the other legitimately Optional[str] (an anchor-map lookup). Renamed
  the first to prose_target_id instead of fighting mypy's narrowing
  across two unrelated code paths sharing one name.

- `lock_tier`: first assigned as int (4), later assigned 1.5/1.7 --
  sub-tiers are intentional (see "Tier 1.5"/"Tier 1.7" in the code and
  DetectorResult's own lock_tier: Union[int, float]). Annotated float
  at declaration; _forge_result's tier: int parameter widened to match.

- `local_tally`: missing annotation on an empty dict accumulator.
  Dict[str, int].

- Two `max(d, key=d.get)` calls: dict.get is overloaded (1-arg vs
  2-arg-with-default), which mypy can't resolve when passed bare as a
  key function. Switched both to `key=lambda k: d[k]` -- safe since key
  is only ever invoked on d's own members.

- `regex_hits`: assigned int(0), later multiplied by 0.7 (a language
  handicap). Annotated float at declaration -- its only consumer is a
  final `/loc` density calculation that was always going to produce a
  float anyway.

- `_find_balanced_end`'s `limit`: self.thresholds is Dict[str, float]
  (mixing float confidence values with integer limits like
  HANDSHAKE_LOOKAHEAD_LIMIT), so the range()-bound computed from it
  needs an explicit int() -- a character-position limit was always
  meant to be a whole number. Fixed the return-value mismatch too, same
  root cause.

157 -> 126 unique baseline entries.

Verified: language_lens.py reports zero mypy errors after this change.
flake8, dead_key_audit.py --ci, full pytest suite (866 passed,
1 deselected, 1 pre-existing xfail) all clean.
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 0168b0a into main Jul 25, 2026
26 checks passed
@squid-protocol
squid-protocol deleted the fix/431-language-lens-typing branch July 25, 2026 17:43
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