refactor(mcuboot): contain TLV info magics in the type system#115
Merged
Conversation
- ImageMagic, ImageTLVInfoMagic, and ImageTLVProtInfoMagic Literal types tie each magic constant to its type; ImageHeader.magic and ImageTLVInfo.magic are no longer plain int - ImageTLVInfo is generic over its magic so protected and unprotected TLV info regions are distinct static types; the expected magic argument to loads/load_from binds the type parameter and supplies the runtime check, replacing the protected: bool flag - ImageTLVInfo.load_tlvs_from reads and parses the TLV entries that follow the header, removing the wire-format arithmetic from ImageInfo.load_file - ImageHeader bad-magic validation moves from __post_init__ (made unreachable by pydantic Literal validation) into loads, preserving MCUBootImageError on the load path BREAKING CHANGE: ImageTLVInfo.REGION_SIZE is removed (use IMAGE_TLV_INFO_STRUCT.size); ImageInfo.protected_tlvs defaults to None instead of []; ImageTLVInfo.loads/load_from take a required magic argument instead of protected: bool Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Collaborator
Author
|
FYI @ChapterSevenSeeds - stronger typing for mcuboot types, claude pass for "100% coverage". |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the MCUBoot image parsing utilities to encode MCUBoot “magic” constants (image header magic and TLV info/protected TLV info magics) directly in the type system using Literal aliases and a generic ImageTLVInfo[T]. It simplifies TLV parsing by moving “read following TLVs” logic onto the TLV-info header type and strengthens error-path behavior with additional tests.
Changes:
- Introduces
Literal-backed magic types (ImageMagic,ImageTLVInfoMagic,ImageTLVProtInfoMagic) and makesImageTLVInfogeneric over its magic. - Updates TLV-info loading APIs to require an explicit expected magic (replacing the previous
protected: boolflag) and addsImageTLVInfo.load_tlvs_from(...)to encapsulate TLV-region parsing. - Adjusts
ImageInfoprotected TLV fields (protected_tlvsnowNoneby default) and expands tests with type-binding assertions and additional error-path coverage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/smpclient/mcuboot.py |
Refactors magic constants into Literal types, generifies TLV-info headers, moves magic validation to load paths, and encapsulates TLV region parsing. |
tests/test_mcuboot_tools.py |
Adds assert_type checks for the new generic types and expands regression/error-path test coverage for the refactor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
ImageMagic,ImageTLVInfoMagic, andImageTLVProtInfoMagicLiteraltype aliases tie each magic constant to its type (Literal[...]cannot reference aFinalname per PEP 586, so the constants are typedFinal[<alias>]to keep the value and type checked against each other).ImageTLVInfois now generic over its magic, so protected and unprotected TLV info regions are distinct static types. The expected magic argument toloads/load_frombinds the type parameter and supplies the runtime check, replacing theprotected: boolflag. The loaders use a method-scoped TypeVar (MagicT) because class-scoped TypeVars only bind viaself/cls— verified against both mypy and pyright.ImageTLVInfo.load_tlvs_from(file)reads and parses the TLV entries that follow the header, removing thetlv_tot - IMAGE_TLV_INFO_STRUCT.sizewire-format arithmetic fromImageInfo.load_file.ImageHeaderbad-magic validation moves from__post_init__(made unreachable by pydanticLiteralvalidation) intoloads, preservingMCUBootImageErroron the load path.assert_typechecks pin the static types at call sites, and new error-path tests bringsmpclient/mcuboot.pyto 100% line and branch coverage.Breaking changes
ImageTLVInfo.loads/load_fromtake a requiredmagicargument instead ofprotected: bool = False.ImageTLVInfo.REGION_SIZEis removed — useIMAGE_TLV_INFO_STRUCT.size.ImageInfo.protected_tlvsdefaults toNoneinstead of[], pairing withprotected_tlv_info.Test plan
task format,task lint,task typecheck(mypy), fulltask test— 296 passedmcuboot.pyandtest_mcuboot_tools.py(loaders inferImageTLVInfo[Literal[0x6907]]/[Literal[0x6908]], noUnknown)smpclient/mcuboot.py🤖 Generated with Claude Code