Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Correct type handling for bitwise variable access#663

Merged
acolomb merged 7 commits into
canopen-python:masterfrom
acolomb:bits-correct-types
May 22, 2026
Merged

Correct type handling for bitwise variable access#663
acolomb merged 7 commits into
canopen-python:masterfrom
acolomb:bits-correct-types

Conversation

@acolomb
Copy link
Copy Markdown
Member

@acolomb acolomb commented May 12, 2026

Contrary to the existing type hints, the decode_bits() / encode_bits() methods can handle strings for lookup as well as an arbitrary collection of integer bit offsets. Correct the hints to reflect that and check at runtime to only lookup from strings.

Correct return type for encode_bits(), as it works only for integers.

Document both functions to explain the intention and caveats to consider, especially the lookup KeyError exception.

Extend test to cover an iterable and a failed lookup.

acolomb added 4 commits May 12, 2026 23:50
Contrary to the existing type hints, the methods can handle strings
for lookup as well as an arbitrary collection of integer bit offsets.
Correct the hints to reflect that and check at runtime to only lookup
from strings.

Correct return type for encode_bits(), as it works only for integers.

Document both functions to explain the intention and caveats to
consider, especially the lookup KeyError exception.

Extend test to cover an iterable and a failed lookup.
Test encoding and decoding with non-contiguous bit offsets.
Any bit for which the replacement pattern contained a 1 bit in one of
the "holes", would be set despite not being part of the desired mask.
This mechanism works only for integers, so add appropriate assertions.
Document all expected types for indexing the Bits.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
canopen/variable.py 92.30% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@acolomb
Copy link
Copy Markdown
Member Author

acolomb commented May 12, 2026

This should properly fix some issues that #651 only silences.

@acolomb
Copy link
Copy Markdown
Member Author

acolomb commented May 12, 2026

Note that there is a logic error in the existing encoder function, caught by the test in commit a0ffffb and fixed in commit dcf7bf4.

@acolomb acolomb marked this pull request as ready for review May 12, 2026 22:52
Comment thread canopen/variable.py
class Bits(Mapping):

def __init__(self, variable: Variable):
assert variable.od.data_type in objectdictionary.datatypes.INTEGER_TYPES
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As I learned like five minutes ago, it seems that -O or -OO flags (mean running python in optimized mode) will disable assertions and thus it is always better to use if / raise. Personally I like the inline assertions a lot and use them quite often. Also libs like pydantic use them in business logic. AI complains you should only use assertions in tests and otherwise raise an appropriate exception (probably TypeError).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the pointer, I didn't know either how easy it is to skip them.

The real question here is what API guarantees we want to give. Bitwise access for a non-integer object is a programming error. Catching that early and reporting in a meaningful way is important during development (of the application I mean, not the library). But once the application straightens out its use of objects and the corresponding types, I see nothing wrong with skipping this check for performance, if optimization is requested by the user. There will be an exception at some point, but disabling assertions on unverified code is basically asking for less useful error diagnostics.

Comment thread canopen/variable.py Outdated
@acolomb acolomb merged commit 9aab2d4 into canopen-python:master May 22, 2026
4 of 5 checks passed
@acolomb acolomb deleted the bits-correct-types branch May 22, 2026 12:27
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