Improve object handling & testing of ensure_unicode#9059
Merged
Conversation
Any other arbitrary object (like `bytearray` or `memoryview` based objects) can be decoded to `unicode` via `codecs.decode`. This is analogous to what is done in `ensure_bytes`. So handle this case here. If this also fails, then raise as usual.
ensure_unicodeensure_unicode
Member
Author
Member
Author
|
cc @jrbourbeau |
Member
Author
|
CI now passes! 🎉 Please let me know if anything else is needed here 🙂 |
jrbourbeau
reviewed
May 10, 2022
Member
jrbourbeau
left a comment
There was a problem hiding this comment.
Thanks @jakirkham! I'm curious, did you run into a use case where this was needed?
Member
Author
Was updating |
This is more consistent with the other tests, which also use this type. Though `int8` also works.
Appears this already gets interpreted correctly by `array`. Should also make the code easier to read for other maintainers.
Avoids the `array.array` bit which is a tad verbose.
2 tasks
jcrist
approved these changes
May 11, 2022
Member
Author
|
Thanks all! 🙏 |
erayaslan
pushed a commit
to erayaslan/dask
that referenced
this pull request
May 12, 2022
* Use `elif` for `decode` in `ensure_unicode` * Handle Python Buffer Protocol in `ensure_unicode` Any other arbitrary object (like `bytearray` or `memoryview` based objects) can be decoded to `unicode` via `codecs.decode`. This is analogous to what is done in `ensure_bytes`. So handle this case here. If this also fails, then raise as usual. * Include `ensure_unicode` tests for various objects * Clarify error messages * Use `uint8` in `array` tests This is more consistent with the other tests, which also use this type. Though `int8` also works. * Pass `bytes` directly to `array` Appears this already gets interpreted correctly by `array`. Should also make the code easier to read for other maintainers. * Use `from array import array` Avoids the `array.array` bit which is a tad verbose.
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.
Make sure that
ensure_unicode(likeensure_bytes) can handle any Python Buffer Protocol object (likebytearrayandmemoryviewobjects). Also include various tests to ensure different objects can be decoded tostr.xref: #9050
pre-commit run --all-files