Skip to content

fix: decode FIT invalid sentinels as None - #90

Merged
shaonianche merged 1 commit into
mainfrom
fix/invalid-values-none
Nov 20, 2026
Merged

shaonianche merged 1 commit into
mainfrom
fix/invalid-values-none

Conversation

@shaonianche

Copy link
Copy Markdown
Owner

What this fixes

The FIT protocol defines one invalid value per base type (enum/uint8 0xFF, sint8 0x7F, uint16 0xFFFF, uint*z 0, date_time 0xFFFFFFFF, …) and says a decoder assigns it when data is unknown or absent. The library was returning those sentinels as measurements:

  • record.cadence → 255
  • record.speed → 65.535
  • unset lap.time_in_power_zone → [4294967.295, …]
  • record.left_power_phase → [358.59, 358.59]

A single Garmin SDK Activity.fit has 26,052 such elements. Callers had to know every base type's sentinel, record.cadence is None was never true, and a required field written as its sentinel passed FILE_TYPE validation.

Behaviour change — callers will notice

These now return None instead of a number:

  • Scalar integer, enum, and date_time fields whose wire value is the base type's invalid sentinel: record.cadence (255), record.speed (65.535), record.left_pco (127), device_info.battery_voltage (255.996), file_id.serial_number (uint32z 0), record.activity_type, and every other numeric/enum scalar.
  • Elements of numeric/enum arrays that hold the sentinel: unset time_in_*_zone entries, invalid left_power_phase entries, etc.
  • Field.get_values() for a byte field whose every byte is 0xFF.
  • CSV / to_rows() cells for invalid values: empty instead of the sentinel number.

What a caller must do: check for None (or use Field.has_value()) before arithmetic or comparisons on decoded values. fit_tool/examples/modify_activity_example.py shows the pattern (if power_field.has_value(): power = power_field.get_value()).

Deliberately unchanged:

  • Field.is_valid() still means "the field is on the wire" (size-based), so definitions and encoding are unaffected. Use has_value() for "has a real value".
  • Byte fields stay opaque: 0xFF inside a developer application_id is data. Only a byte field where every byte is 0xFF reads as None (FIT Protocol Table 7: "Field is invalid if all bytes are invalid").
  • An all-invalid numeric array stays a list of None so positions survive (e.g. power-zone slots), rather than collapsing the whole field to None as Garmin's Python/JS SDKs do.
  • Authoring a value that encodes to the sentinel (e.g. speed = 65.535) reads back as None; that is inherent to FIT.

What changed

  • BaseType.is_invalid_value(): per-type sentinel predicate.
  • Field.has_value() / _has_no_value(); decode_value() maps sentinels to None; get_value / get_values / to_row follow. Raw encoded values stay on the field, so PRESERVE and CANONICAL re-encoding are byte-identical.
  • FILE_TYPE required-field checks use has_value(), so a required field written as its sentinel is reported missing.
  • Component expansion skips all-invalid packed sources and may fill a present-but-invalid destination (matches Garmin's JS/Python SDKs).
  • docs/CAPABILITY_BOUNDARY.md, the modify example, and a Breaking Changes news fragment.

Tests

  • uv run pytest -q: 480 passed, 1 skipped, 30 subtests. The skip is the live Garmin JS interop test, which needs a Node SDK checkout.
  • uv run ruff check fit_tool: clean.
  • uv run mypy: clean.
  • All 23 committed .fit fixtures decode and re-encode byte-for-byte; a dirty-record edit keeps the invalid cadence byte 0xFF.
  • New tests pin Activity.fit cadence 255 → None, speed 65535 → None, left_pco 127 → None, left_power_phase → [None, None]; activity_poolswim_with_hr.fit lap power zones → [None] × 8; every base-type sentinel; byte arrays; required-field detection; the dirty-record round-trip.

Reviewer: look at these four first

  1. fit_tool/field.py — has_value() / _has_no_value() and the sentinel check in decode_value. is_valid() deliberately stays size-based.
  2. fit_tool/validation/file_types/common.py — required fields now check has_value() before the property.
  3. fit_tool/components.py — all-invalid packed sources no longer expand; an invalid destination can be filled. Two accumulator tests moved from 65535 (the uint16 sentinel) to 65534, with a new test pinning the skip.
  4. fit_tool/field.py::to_row — CSV output for invalid cells is now empty.

No issue number: this is a behaviour fix found during the profile-coverage review.

Map each base type's invalid value to None on read, for scalars and array
elements, instead of returning the sentinel as a measurement (255 bpm,
65.535 m/s, 4294967.295 s, ...). Raw encoded values stay on the field so
PRESERVE and CANONICAL re-encoding remain byte-identical.

- BaseType.is_invalid_value() defines the per-type sentinel; byte fields stay
  opaque except when every byte is invalid (FIT protocol Table 7).
- Field.has_value() distinguishes "on the wire" from "has a value"; is_valid()
  stays size-based so definitions and encode are unchanged.
- FILE_TYPE required-field checks use has_value(), so a required field written
  as its sentinel is reported missing.
- Component expansion skips all-invalid packed sources and may fill a
  present-but-invalid destination, matching Garmin's SDKs.
- All-invalid numeric arrays stay lists of None so positions survive.

Behaviour change: callers doing arithmetic or comparisons on decoded values
must check for None first (or use Field.has_value()).

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f68a-2665-7000-a200-a75073933440
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.21%. Comparing base (d470d21) to head (556d27f).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
fit_tool/field.py 92.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #90      +/-   ##
==========================================
+ Coverage   93.03%   93.21%   +0.17%     
==========================================
  Files          46       46              
  Lines        2728     2755      +27     
  Branches      425      436      +11     
==========================================
+ Hits         2538     2568      +30     
+ Misses        109      107       -2     
+ Partials       81       80       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shaonianche
shaonianche merged commit af9e1d3 into main Nov 20, 2026
10 checks passed
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