Skip to content

Accept memoryview in setter/getter type annotations#647

Merged
gijzelaerr merged 1 commit into
masterfrom
fix/memoryview-setter-compat
Mar 20, 2026
Merged

Accept memoryview in setter/getter type annotations#647
gijzelaerr merged 1 commit into
masterfrom
fix/memoryview-setter-compat

Conversation

@gijzelaerr
Copy link
Copy Markdown
Owner

Summary

  • Add Buffer = Union[bytearray, memoryview] type alias to setters and getters
  • Update all function signatures to accept both bytearray and memoryview
  • Fix .decode() calls in get_wchar/get_wstring to use bytes() conversion for memoryview compatibility
  • Add 17 memoryview compatibility tests covering all setter functions

The setter functions already worked at runtime with memoryview (they use direct struct.pack() slice assignment), but the type annotations only accepted bytearray, causing mypy errors when passing memoryview objects from ctypes buffers. This is a common use case with the Server class.

Credit to LuTiFlekSSer for identifying the memoryview compatibility issue in their fork.

Test plan

  • All 17 new memoryview tests pass
  • Full test suite passes (464 passed, 48 skipped)
  • mypy clean (33 source files)
  • ruff check/format clean

🤖 Generated with Claude Code

The setter functions already worked with memoryview at runtime (using
direct struct.pack() slice assignment), but the type annotations only
accepted bytearray. This caused mypy errors when passing memoryview
objects from ctypes buffers.

- Add Buffer type alias (Union[bytearray, memoryview]) to setters and getters
- Update all function signatures to accept Buffer
- Fix .decode() calls in getters to use bytes() for memoryview compat
- Add 17 memoryview compatibility tests

Credit: LuTiFlekSSer for identifying the memoryview compatibility issue.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gijzelaerr gijzelaerr merged commit 60047da into master Mar 20, 2026
37 checks passed
@gijzelaerr gijzelaerr added this to the 3.1 milestone Mar 24, 2026
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