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

Reintroduce weight function #4723

Merged
merged 4 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions distributed/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
LoopRunner,
TimeoutError,
_maybe_complex,
deprecated,
ensure_bytes,
ensure_ip,
format_dashboard_link,
Expand Down Expand Up @@ -617,3 +618,20 @@ def test_lru():
async def test_offload():
assert (await offload(inc, 1)) == 2
assert (await offload(lambda x, y: x + y, 1, y=2)) == 3


def test_deprecated():
@deprecated()
def foo():
return "bar"

with pytest.warns(DeprecationWarning, match="foo is deprecated"):
assert foo() == "bar"

# Explicit version specified
@deprecated(version_removed="1.2.3")
def foo():
return "bar"

with pytest.warns(DeprecationWarning, match="removed in version 1.2.3"):
assert foo() == "bar"
7 changes: 6 additions & 1 deletion distributed/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
s,
slowinc,
)
from distributed.worker import Worker, error_message, logger, parse_memory_limit
from distributed.worker import Worker, error_message, logger, parse_memory_limit, weight


@pytest.mark.asyncio
Expand Down Expand Up @@ -1787,3 +1787,8 @@ async def test_story(c, s, w):
ts = w.tasks[future.key]
assert ts.state in str(w.story(ts))
assert w.story(ts) == w.story(ts.key)


def test_weight_deprecated():
with pytest.warns(DeprecationWarning):
weight("foo", "bar")
28 changes: 28 additions & 0 deletions distributed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,34 @@ def time_warn(duration, text):
print("TIME WARNING", text, end - start)


def deprecated(*, version_removed: str = None):
"""Decorator to mark a function as deprecated

Parameters
----------
version_removed : str, optional
If specified, include the version in which the deprecated function
will be removed. Defaults to "a future release".
"""

def decorator(func):
nonlocal version_removed
msg = f"{funcname(func)} is deprecated and will be removed in"
if version_removed is not None:
msg += f" version {version_removed}"
else:
msg += " a future release"

@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)

return wrapper

return decorator


def json_load_robust(fn, load=json.load):
""" Reads a JSON file from disk that may be being written as we read """
while not os.path.exists(fn):
Expand Down
6 changes: 6 additions & 0 deletions distributed/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
LRU,
TimeoutError,
_maybe_complex,
deprecated,
get_ip,
has_arg,
import_file,
Expand Down Expand Up @@ -3718,6 +3719,11 @@ def convert_kwargs_to_str(kwargs, max_len=None):
return "{{{}}}".format(", ".join(strs))


@deprecated(version_removed="2021.06.0")
def weight(k, v):
return sizeof(v)
fjetter marked this conversation as resolved.
Show resolved Hide resolved


async def run(server, comm, function, args=(), kwargs=None, is_coro=None, wait=True):
kwargs = kwargs or {}
function = pickle.loads(function)
Expand Down