Skip to content

Make all setter functions consistently return bytearray#585

Open
gijzelaerr wants to merge 1 commit intomasterfrom
fix/553-setter-return-consistency
Open

Make all setter functions consistently return bytearray#585
gijzelaerr wants to merge 1 commit intomasterfrom
fix/553-setter-return-consistency

Conversation

@gijzelaerr
Copy link
Owner

Summary

Fixes #553

Five setter functions (set_fstring, set_string, set_dword, set_dint, set_udint) returned None while all other setters (set_bool, set_byte, set_word, set_int, set_uint, set_real, set_usint, set_sint, set_lreal, set_char, set_date, set_time) returned the modified bytearray. This inconsistency prevented idiomatic chaining like:

data = (
    set_uint(bytearray(2), 0, current_dateTime.year)
    + set_usint(bytearray(1), 0, current_dateTime.month)
    + set_udint(bytearray(4), 0, current_dateTime.microsecond * 1000)  # <-- returned None!
)

Root cause analysis

The setters were written at different times by different contributors. The earlier setters followed the pattern of returning the modified buffer, but the later additions (set_dword, set_dint, set_udint) and the string setters (set_fstring, set_string) simply forgot the return statement. Since Python functions implicitly return None, these silently broke concatenation-based patterns.

Fix

Added return bytearray_ to all five functions and updated their type annotations from -> None to -> bytearray. This is a backwards-compatible change — existing code that ignores the return value continues to work identically, since the functions still modify the bytearray in-place.

Test plan

  • All 326 existing tests pass
  • Verify that set_udint(bytearray(4), 0, 123) + set_uint(bytearray(2), 0, 456) now works

🤖 Generated with Claude Code

set_fstring, set_string, set_dword, set_dint, and set_udint now return
the modified bytearray, matching the behavior of all other setters.

Fixes #553

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Problem with some setters

1 participant