Skip to content

Fix Optional handling for node IDs and PDO variables#650

Open
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:fix/mypy-optional-handling
Open

Fix Optional handling for node IDs and PDO variables#650
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:fix/mypy-optional-handling

Conversation

@bizfsc
Copy link
Copy Markdown
Contributor

@bizfsc bizfsc commented Apr 29, 2026

Fix Optional handling to prevent operations on potentially None values.

Problem

mypy reports [operator] and [union-attr] errors where Optional values are used without None-checks — e.g. 0x600 + self.id when self.id could be None, or divmod(self.offset, 8) when self.offset is None.

Changes

canopen/node/base.py

  • Annotate self.id as Optional[int] to match its runtime value (node_id or self.object_dictionary.node_id).

canopen/node/remote.py / canopen/node/local.py

  • Add assert self.id is not None in __init__ and re-annotate self.id: int to narrow the type for all methods.

canopen/pdo/base.py

  • Add assert guards for self.offset and self.pdo_parent at the start of get_data() and set_data().
  • Fix wait_for_reception() return type: Optional[float] (was float, but returns None on timeout).

Impact

Eliminates 21 [operator] and [union-attr] errors in node and PDO modules.", "draft">false

- Annotate BaseNode.id as Optional[int] to match its runtime value
- Add assert + re-annotation in RemoteNode/LocalNode to narrow to int
- Add asserts for PdoVariable.offset and pdo_parent before use
- Fix wait_for_reception return type to Optional[float]
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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