fix TypeError when passing bytearray to from_bytes#703
Merged
Ousret merged 1 commit intojawah:masterfrom Mar 6, 2026
Merged
Conversation
The `from_bytes` function in `api.py` accepts both `bytes` and `bytearray` as valid input. However, when `preemptive_behaviour` is enabled (the default), the input is passed to `any_specified_encoding` in `utils.py`, which strictly checked for `isinstance(sequence, bytes)`. This caused an immediate `TypeError` when a `bytearray` was provided. This commit updates the type check in `any_specified_encoding` to `isinstance(sequence, (bytes, bytearray))` to properly support `bytearray` inputs without breaking backward compatibility.
Merged
Ousret
added a commit
that referenced
this pull request
Mar 6, 2026
## [3.4.5](3.4.4...3.4.5) (2026-03-06) ### Changed - Update `setuptools` constraint to `setuptools>=68,<=82`. - Raised upper bound of mypyc for the optional pre-built extension to v1.19.1 ### Fixed - Add explicit link to lib math in our optimized build. (#692) - Logger level not restored correctly for empty byte sequences. (#701) - TypeError when passing bytearray to from_bytes. (#703) ### Misc - Applied safe micro-optimizations in both our noise detector and language detector. - Rewrote the `query_yes_no` function (inside CLI) to avoid using ambiguous licensed code. - Added `cd.py` submodule into mypyc optional compilation to reduce further the performance impact.
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.
This PR fixes a bug where passing a valid
bytearraytofrom_bytescrashes the application with aTypeError. Theapi.from_bytestype hints explicitly supportbytearray, but the underlying helper functionutils.any_specified_encodingdid not.What actually happens:
When
from_bytesis called with abytearrayandpreemptive_behaviour=True,utils.any_specified_encodingraises aTypeErrorbecause it strictly checksif not isinstance(sequence, bytes):.What I expected to happen:
The function should successfully process the
bytearraysequence and return aCharsetMatchesobject, just as it does for standardbytesobjects.How to reproduce the issue:
Changes Made:
utils.pyaround line 260 to check forisinstance(sequence, (bytes, bytearray))instead of strictlybytes.Checklist:
CONTRIBUTING.mddocument.nox -s testlocally.nox -s lintlocally.nox -s coveragelocally.