Fix Optional handling for node IDs and PDO variables#650
Open
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
Open
Fix Optional handling for node IDs and PDO variables#650bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
Conversation
- 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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Apr 29, 2026
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.
Fix
Optionalhandling to prevent operations on potentiallyNonevalues.Problem
mypy reports
[operator]and[union-attr]errors whereOptionalvalues are used without None-checks — e.g.0x600 + self.idwhenself.idcould beNone, ordivmod(self.offset, 8)whenself.offsetisNone.Changes
canopen/node/base.pyself.idasOptional[int]to match its runtime value (node_id or self.object_dictionary.node_id).canopen/node/remote.py/canopen/node/local.pyassert self.id is not Nonein__init__and re-annotateself.id: intto narrow the type for all methods.canopen/pdo/base.pyassertguards forself.offsetandself.pdo_parentat the start ofget_data()andset_data().wait_for_reception()return type:Optional[float](wasfloat, but returnsNoneon timeout).Impact
Eliminates 21
[operator]and[union-attr]errors in node and PDO modules.", "draft">false