Skip to content

fix: guard parse_token_int against IEEE 754 special floats (#875)#999

Merged
microsasa merged 1 commit intomainfrom
fix/875-sanitize-ieee-special-floats-cbae788755389aaf
Apr 19, 2026
Merged

fix: guard parse_token_int against IEEE 754 special floats (#875)#999
microsasa merged 1 commit intomainfrom
fix/875-sanitize-ieee-special-floats-cbae788755389aaf

Conversation

@microsasa
Copy link
Copy Markdown
Owner

Closes #875

Problem

parse_token_int in models.py only rejected float('inf') and float('nan') incidentally — float.is_integer() happens to return False for both, but this was undocumented and fragile. The issue requested an explicit guard so the contract is clear and future refactors don't silently regress.

Changes

Validator fix (src/copilot_usage/models.py)

  • Added import math and an explicit math.isfinite(raw) guard in parse_token_int before the is_integer() check
  • Updated the docstring to document the IEEE 754 special float → None rule

Tests (tests/copilot_usage/test_parser.py)

  • Added parametrized test_returns_none_for_ieee_special_floats to TestExtractOutputTokens covering inf, nan, -inf
  • Extended _EQUIVALENCE_CASES with float('inf'), float('-inf'), and float('nan') so the cross-check between _extract_output_tokens and AssistantMessageData covers these values

Pre-existing tests

  • test_models.py::TestSanitizeNonNumericTokens::test_special_float_maps_to_zero already existed and continues to pass

Verification

make check V=1 passes: lint ✅, pyright ✅, bandit ✅, unit tests (99% coverage) ✅, e2e tests ✅

Generated by Issue Implementer · ● 10.5M ·

Add explicit math.isfinite() check in parse_token_int so float('inf'),
float('-inf'), and float('nan') are rejected before reaching Pydantic
int coercion. Previously these values were only handled incidentally via
float.is_integer() returning False.

- Add math.isfinite guard in parse_token_int (models.py)
- Update parse_token_int docstring to document IEEE special float rule
- Add parametrized test for _extract_output_tokens with special floats
- Extend _EQUIVALENCE_CASES cross-check with inf, -inf, and nan

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Apr 19, 2026
Copilot AI review requested due to automatic review settings April 19, 2026 07:42
@microsasa microsasa enabled auto-merge April 19, 2026 07:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR clarifies and hardens token-count parsing by explicitly rejecting IEEE 754 special float values (inf, -inf, nan) in parse_token_int, ensuring the contract is documented and guarded against future refactors.

Changes:

  • Added an explicit math.isfinite() guard in parse_token_int before the float.is_integer() check.
  • Documented the special-float rejection rule in parse_token_int’s docstring.
  • Added regression tests covering inf, -inf, and nan, and extended equivalence-case coverage to include them.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/copilot_usage/models.py Adds explicit finite-float validation and documents special-float handling in parse_token_int.
tests/copilot_usage/test_parser.py Adds targeted tests for special floats and extends fast-path vs model-path equivalence cases.

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Apr 19, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Quality Gate — Auto-approved

Low-impact defensive fix with good test coverage. The math.isfinite() guard makes parse_token_int's rejection of IEEE 754 special floats (inf, -inf, nan) explicit rather than relying on the incidental behavior of float.is_integer(). No behavioral change for real-world inputs.

Evaluated:

  • import math placed correctly in stdlib imports
  • Guard positioned correctly before is_integer() inside the isinstance(raw, float) block
  • Docstring accurately updated
  • Parametrized tests cover all three IEEE 754 specials
  • Equivalence cross-check cases extended
  • All 8 CI checks green
  • Complies with CODING_GUIDELINES.md (typing, testing, formatting)

@microsasa microsasa merged commit 2a16060 into main Apr 19, 2026
8 checks passed
@microsasa microsasa deleted the fix/875-sanitize-ieee-special-floats-cbae788755389aaf branch April 19, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aw Created by agentic workflow aw-quality-gate-approved Quality gate approved the PR

Projects

None yet

2 participants