Skip to content

Make sure any BaseNode always has a valid Node ID#657

Merged
acolomb merged 2 commits intocanopen-python:masterfrom
acolomb:node-id-always-valid
May 7, 2026
Merged

Make sure any BaseNode always has a valid Node ID#657
acolomb merged 2 commits intocanopen-python:masterfrom
acolomb:node-id-always-valid

Conversation

@acolomb
Copy link
Copy Markdown
Member

@acolomb acolomb commented May 6, 2026

The BaseNode.id attribute can be initialized either from the given node_id parameter, or from the Object Dictionary if zero was passed. The None value as mentioned in the docstring actually violates the type hint, so drop that suggestion. Make sure the inferred type of the self.id attribute does not allow None, but is always an integer.

Furthermore, check the resulting Node ID against the allowed value range and raise a ValueError instead of constructing a node object with an invalid Node ID, which will cause problems down the road. This is technically an API change, but it just causes things to fail early on instead of raising exceptions later on.

Copy the docstring for LocalNode as well, to document the expected node_id "invalid" value. And because there was none at all before, include a bit of functional description to set the expectations straight.

The BaseNode.id attribute can be initialized either from the given
node_id parameter, or from the Object Dictionary if zero was passed.
The None value as mentioned in the docstring actually violates the
type hint, so drop that suggestion.  Make sure the inferred type of
the self.id attribute does not allow None, but is always an integer.

Furthermore, check the resulting Node ID against the allowed value
range and raise a ValueError instead of constructing a node object
with an invalid Node ID, which will cause problems down the road.
This is technically an API change, but it just causes things to fail
early on instead of raising exceptions later on.

Copy the docstring for LocalNode as well, to document the expected
node_id "invalid" value.  And because there was none at all before,
include a bit of functional description to set the expectations
straight.
@acolomb
Copy link
Copy Markdown
Member Author

acolomb commented May 6, 2026

Replaces #650, as far as Node ID assumptions are concerned.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Collaborator

@friederschueler friederschueler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues found in canopen/node/base.py:

  1. Bug: The ValueError message uses %r as a format specifier but never substitutes self.id, so the actual invalid value is lost in the error message.
  2. Minor: The condition not self.id or not 1 <= self.id <= 127 has redundancy — not self.id is already covered by the range check. Can be simplified to not (1 <= self.id <= 127).

Comment thread canopen/node/base.py Outdated
Comment thread canopen/node/base.py Outdated
@acolomb acolomb merged commit 9dbb9fd into canopen-python:master May 7, 2026
4 of 5 checks passed
@acolomb acolomb deleted the node-id-always-valid branch May 7, 2026 10:23
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.

2 participants