Narrow encode_into offset stub type to int - #1163
Merged
Merged
Conversation
The type stubs for `msgspec.json.Encoder.encode_into` and `msgspec.msgpack.Encoder.encode_into` declared `offset: int | None = 0`, but the C implementation passes the argument to `PyLong_AsSsize_t` and raises `TypeError: an integer is required` for `None`. The docstring also documents `offset : int, optional`, with `-1` (not `None`) as the sentinel for writing at the end of the buffer. Narrow the stubs to `int` and add type-level and runtime regression tests.
dylanpulver
had a problem deploying
to
docs-preview
August 21, 2026 21:29 — with
GitHub Actions
Failure
Siyet
approved these changes
Sep 14, 2026
Siyet
left a comment
Contributor
There was a problem hiding this comment.
Thx for the fix and for adding regression tests for both encoders!
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.
Widening the runtime to accept
Nonewas the alternative. I rejected it: the docstring specifiesoffset : int, optionaland reserves-1as the sentinel for writing at the end of the buffer, which leavesNonewith no meaning to carry. Narrowing the stub makes the declared type match both the C code and the documented contract.The
int | Nonetraces back to the originalOptional[int]annotation and survived the 3.10+ syntax migration in #1041. Nothing in the tree passesNone.Residual limitation: this edits the same two stub lines as open PR #1160, which reshapes
bufferand the return type while keepingoffset: int | None. Whichever lands second needs a one-line rebase. The two changes are otherwise independent, since #1160 keeps readingoffsetviaPyLong_AsSsize_t.Measurement, CPython 3.14:
just test-typingclean under mypy 2.3.0, pyright 1.1.411, pyrefly 1.2.0.basic_typing_examples.py, so the type-level test is not vacuous.pytest tests/unit: 6119 passed, 404 skipped, identical to the pre-change baseline. The two runtime assertions are pins on current behavior rather than new coverage.ruff formatcollapses the signature onto one line once| Noneis gone; that reformat is included.No changelog entry, following merged stub PRs #1055, #1057, #1062, #1065, #1074 and #1093, which leave it to the aggregated "type stub improvements" line at release.