Repository navigation
fix: decode FIT invalid sentinels as None - #90
Merged
Merged
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
The FIT protocol defines one invalid value per base type (
enum/uint80xFF,sint80x7F,uint160xFFFF,uint*z0,date_time0xFFFFFFFF, …) and says a decoder assigns it when data is unknown or absent. The library was returning those sentinels as measurements:record.cadence→255record.speed→65.535lap.time_in_power_zone→[4294967.295, …]record.left_power_phase→[358.59, 358.59]A single Garmin SDK
Activity.fithas 26,052 such elements. Callers had to know every base type's sentinel,record.cadence is Nonewas never true, and a required field written as its sentinel passed FILE_TYPE validation.Behaviour change — callers will notice
These now return
Noneinstead of a number: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.time_in_*_zoneentries, invalidleft_power_phaseentries, etc.Field.get_values()for a byte field whose every byte is0xFF.to_rows()cells for invalid values: empty instead of the sentinel number.What a caller must do: check for
None(or useField.has_value()) before arithmetic or comparisons on decoded values.fit_tool/examples/modify_activity_example.pyshows 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. Usehas_value()for "has a real value".0xFFinside a developerapplication_idis data. Only a byte field where every byte is0xFFreads asNone(FIT Protocol Table 7: "Field is invalid if all bytes are invalid").Noneso positions survive (e.g. power-zone slots), rather than collapsing the whole field toNoneas Garmin's Python/JS SDKs do.speed = 65.535) reads back asNone; 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 toNone;get_value/get_values/to_rowfollow. Raw encoded values stay on the field, so PRESERVE and CANONICAL re-encoding are byte-identical.has_value(), so a required field written as its sentinel is reported missing.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..fitfixtures decode and re-encode byte-for-byte; a dirty-record edit keeps the invalid cadence byte0xFF.Activity.fitcadence 255 →None, speed 65535 →None,left_pco127 →None,left_power_phase→[None, None];activity_poolswim_with_hr.fitlap power zones →[None] × 8; every base-type sentinel; byte arrays; required-field detection; the dirty-record round-trip.Reviewer: look at these four first
fit_tool/field.py—has_value()/_has_no_value()and the sentinel check indecode_value.is_valid()deliberately stays size-based.fit_tool/validation/file_types/common.py— required fields now checkhas_value()before the property.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.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.