Skip to content

fix: tighten typing on Card and Token dataclasses#188

Merged
piitaya merged 2 commits into
masterfrom
dataclasses_card_token
May 27, 2026
Merged

fix: tighten typing on Card and Token dataclasses#188
piitaya merged 2 commits into
masterfrom
dataclasses_card_token

Conversation

@piitaya
Copy link
Copy Markdown
Collaborator

@piitaya piitaya commented May 27, 2026

Summary

The Card, Chapter, Track, and Token dataclasses had broken annotations like field: str = None (assigning None to a non-Optional type) and dict[Track] = None (missing key type). mypy strict and pyright both flagged them, and downstream code (Home Assistant yoto integration) had to work around it with card.chapters or {} everywhere.

Changes

  • Fix Optional annotations: str = Nonestr | None = None, same for int.
  • Fix dict typing: dict[Track]dict[str, Track], dict[Chapter]dict[str, Chapter].
  • Make identifier fields required (no default): Card.id, Chapter.key, Track.key. They're always set at construction by the parser, and a card/chapter/track without an identifier doesn't exist in the Yoto model.
  • Default chapters and tracks to field(default_factory=dict) instead of None. A card always has chapters and a chapter always has tracks in the Yoto data model; the None was only an artifact of incremental parsing. Empty dict represents the "not yet loaded" state.
  • Remove now-dead is None guards in client.py and or {} in the e2e test.

Impact

No runtime behavior change. Pure annotation tightening plus removal of dead guards.

Downstream consumers (HA core integration) can drop their card.chapters or {} defensive code and trust the type.

@piitaya piitaya merged commit 9f52a51 into master May 27, 2026
8 checks passed
@piitaya piitaya deleted the dataclasses_card_token branch May 27, 2026 08:47
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