Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ensure_bytes from dask.utils #6295

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion distributed/comm/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tornado.websocket import WebSocketClosedError, WebSocketHandler, websocket_connect

import dask
from dask.utils import ensure_bytes

from distributed.comm.addressing import parse_host_port, unparse_host_port
from distributed.comm.core import (
Expand All @@ -36,7 +37,7 @@
get_tcp_server_address,
to_frames,
)
from distributed.utils import ensure_bytes, nbytes
from distributed.utils import nbytes

logger = logging.getLogger(__name__)

Expand Down
3 changes: 1 addition & 2 deletions distributed/protocol/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from tlz import identity

import dask

from distributed.utils import ensure_bytes
from dask.utils import ensure_bytes

compressions: dict[
str | None | Literal[False],
Expand Down
4 changes: 2 additions & 2 deletions distributed/protocol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import dask
from dask.base import normalize_token
from dask.utils import typename
from dask.utils import ensure_bytes, typename

from distributed.protocol import pickle
from distributed.protocol.compression import decompress, maybe_compress
Expand All @@ -22,7 +22,7 @@
pack_frames_prelude,
unpack_frames,
)
from distributed.utils import ensure_bytes, has_keyword
from distributed.utils import has_keyword

dask_serialize = dask.utils.Dispatch("dask_serialize")
dask_deserialize = dask.utils.Dispatch("dask_deserialize")
Expand Down
4 changes: 2 additions & 2 deletions distributed/protocol/tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

np = pytest.importorskip("numpy")

from dask.utils import tmpfile
from dask.utils import ensure_bytes, tmpfile

from distributed.protocol import (
decompress,
Expand All @@ -20,7 +20,7 @@
from distributed.protocol.pickle import HIGHEST_PROTOCOL
from distributed.protocol.utils import BIG_BYTES_SHARD_SIZE
from distributed.system import MEMORY_LIMIT
from distributed.utils import ensure_bytes, nbytes
from distributed.utils import nbytes
from distributed.utils_test import gen_cluster


Expand Down
2 changes: 1 addition & 1 deletion distributed/protocol/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
np = pytest.importorskip("numpy")

from dask.dataframe.utils import assert_eq
from dask.utils import ensure_bytes

from distributed.protocol import (
decompress,
Expand All @@ -13,7 +14,6 @@
serialize,
to_serialize,
)
from distributed.utils import ensure_bytes

dfs = [
pd.DataFrame({}),
Expand Down
23 changes: 0 additions & 23 deletions distributed/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import array
import asyncio
import contextvars
import functools
Expand Down Expand Up @@ -26,7 +25,6 @@
LoopRunner,
TimeoutError,
_maybe_complex,
ensure_bytes,
ensure_ip,
format_dashboard_link,
get_ip_interface,
Expand Down Expand Up @@ -248,27 +246,6 @@ def test_seek_delimiter_endline():
assert f.tell() == 7


def test_ensure_bytes():
data = [b"1", "1", memoryview(b"1"), bytearray(b"1"), array.array("b", [49])]
for d in data:
result = ensure_bytes(d)
assert isinstance(result, bytes)
assert result == b"1"


def test_ensure_bytes_ndarray():
np = pytest.importorskip("numpy")
result = ensure_bytes(np.arange(12))
assert isinstance(result, bytes)


def test_ensure_bytes_pyarrow_buffer():
pa = pytest.importorskip("pyarrow")
buf = pa.py_buffer(b"123")
result = ensure_bytes(buf)
assert isinstance(result, bytes)


def test_nbytes():
np = pytest.importorskip("numpy")

Expand Down
20 changes: 9 additions & 11 deletions distributed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import dask
from dask import istask
from dask.utils import ensure_bytes as _ensure_bytes
from dask.utils import parse_timedelta as _parse_timedelta
from dask.widgets import get_template

Expand Down Expand Up @@ -1000,17 +1001,14 @@ def ensure_bytes(s):
>>> ensure_bytes(b'123')
b'123'
"""
if isinstance(s, bytes):
return s
elif hasattr(s, "encode"):
return s.encode()
else:
try:
return bytes(s)
except Exception as e:
raise TypeError(
"Object %s is neither a bytes object nor has an encode method" % s
) from e
warnings.warn(
"`distributed.utils.ensure_bytes` is deprecated. "
"Please switch to `dask.utils.ensure_bytes`. "
"This will be removed in `2022.6.0`.",
DeprecationWarning,
stacklevel=2,
)
Comment on lines +1004 to +1010
Copy link
Member Author

Choose a reason for hiding this comment

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

Please let me know if this looks alright.

Also picked 2022.6.0 as the point when we would remove this. Though that is somewhat arbitrary. Happy to change if needed.

return _ensure_bytes(s)


def open_port(host=""):
Expand Down