Skip to content

Improve object handling & testing of ensure_unicode#9059

Merged
jcrist merged 8 commits intodask:mainfrom
jakirkham:ens_uni_pb
May 11, 2022
Merged

Improve object handling & testing of ensure_unicode#9059
jcrist merged 8 commits intodask:mainfrom
jakirkham:ens_uni_pb

Conversation

@jakirkham
Copy link
Copy Markdown
Member

@jakirkham jakirkham commented May 9, 2022

Make sure that ensure_unicode (like ensure_bytes) can handle any Python Buffer Protocol object (like bytearray and memoryview objects). Also include various tests to ensure different objects can be decoded to str.

xref: #9050

  • Closes #xxxx
  • Tests added / passed
  • Passes pre-commit run --all-files

jakirkham added 3 commits May 9, 2022 16:07
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.
@jakirkham jakirkham changed the title Improve object handling & testing in ensure_unicode Improve object handling & testing of ensure_unicode May 9, 2022
@jakirkham jakirkham added needs review Needs review from a contributor. hygiene Improve code quality and reduce maintenance overhead labels May 9, 2022
@jakirkham
Copy link
Copy Markdown
Member Author

jakirkham commented May 10, 2022

Test failures are due to issue ( #9060 ). So unrelated to these changes

Edit: Restarting CI now that PR ( #8993 ) is merged, which should fix those failures.

Edit 2: Restarting showed the same failures. Trying merging with main. This fixed it.

@jakirkham
Copy link
Copy Markdown
Member Author

cc @jrbourbeau

@jakirkham
Copy link
Copy Markdown
Member Author

CI now passes! 🎉

Please let me know if anything else is needed here 🙂

Copy link
Copy Markdown
Member

@jrbourbeau jrbourbeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jakirkham! I'm curious, did you run into a use case where this was needed?

@jakirkham
Copy link
Copy Markdown
Member Author

I'm curious, did you run into a use case where this was needed?

Was updating ensure_bytes in PR ( #9050 ) and noticed that similar improvements could be made here as were there

@jakirkham jakirkham requested a review from jrbourbeau May 10, 2022 22:40
jakirkham added 3 commits May 10, 2022 20:55
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.
@jcrist jcrist merged commit 5f74a6c into dask:main May 11, 2022
@jakirkham jakirkham deleted the ens_uni_pb branch May 11, 2022 16:42
@jakirkham
Copy link
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hygiene Improve code quality and reduce maintenance overhead needs review Needs review from a contributor.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants