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

Add type annotations to various functions within distributed.worker #5290

Merged
merged 6 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ dask-worker-space/
.ycm_extra_conf.py
tags
.ipynb_checkpoints
.venv/
33 changes: 21 additions & 12 deletions distributed/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
typename,
)

import distributed

orf marked this conversation as resolved.
Show resolved Hide resolved
from . import comm, preloading, profile, system, utils
from .batched import BatchedSend
from .comm import connect, get_address_host
Expand Down Expand Up @@ -2813,8 +2815,14 @@ async def plugin_remove(self, comm=None, name=None):
return {"status": "OK"}

async def actor_execute(
self, comm=None, actor=None, function=None, args=(), kwargs={}
self,
comm=None,
actor=None,
function=None,
args=(),
kwargs: Optional[dict] = None,
):
kwargs = kwargs or {}
separate_thread = kwargs.pop("separate_thread", True)
key = actor
actor = self.actors[key]
Expand Down Expand Up @@ -2849,7 +2857,7 @@ def actor_attribute(self, comm=None, actor=None, attribute=None):
except Exception as ex:
return {"status": "error", "exception": to_serialize(ex)}

def meets_resource_constraints(self, key):
def meets_resource_constraints(self, key: str) -> bool:
ts = self.tasks[key]
if not ts.resource_restrictions:
return True
Expand Down Expand Up @@ -3259,8 +3267,7 @@ async def get_profile(
return prof

async def get_profile_metadata(self, comm=None, start=0, stop=None):
if stop is None:
add_recent = True
add_recent = stop is None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you instead directly change to if stop is None: on line 3284?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The line below this overwrites stop: stop = stop or now. I'm not clear on what these arguments do, but this would always make the condition true.

now = time() + self.scheduler_delay
stop = stop or now
start = start or 0
Expand Down Expand Up @@ -3442,14 +3449,14 @@ def validate_state(self):
#######################################

@property
def client(self):
def client(self) -> Client:
with self._lock:
if self._client:
return self._client
else:
return self._get_client()

def _get_client(self, timeout=None):
def _get_client(self, timeout=None) -> Client:
"""Get local client attached to this worker

If no such client exists, create one
Expand Down Expand Up @@ -3531,7 +3538,7 @@ def get_current_task(self):
return self.active_threads[threading.get_ident()]


def get_worker():
def get_worker() -> Worker:
"""Get the worker currently running this task

Examples
Expand All @@ -3558,7 +3565,9 @@ def get_worker():
raise ValueError("No workers found")


def get_client(address=None, timeout=None, resolve_address=True):
def get_client(
address=None, timeout=None, resolve_address=True
) -> Client:
"""Get a client while within a task.

This client connects to the same scheduler to which the worker is connected
Expand Down Expand Up @@ -3673,7 +3682,7 @@ class Reschedule(Exception):
"""


def parse_memory_limit(memory_limit, nthreads, total_cores=CPU_COUNT):
def parse_memory_limit(memory_limit, nthreads, total_cores=CPU_COUNT) -> Optional[int]:
if memory_limit is None:
return None

Expand Down Expand Up @@ -3802,7 +3811,7 @@ def execute_task(task):
_cache_lock = threading.Lock()


def dumps_function(func):
def dumps_function(func) -> bytes:
"""Dump a function to bytes, cache functions"""
try:
with _cache_lock:
Expand Down Expand Up @@ -4023,7 +4032,7 @@ def __repr__(self):
return msg


def convert_args_to_str(args, max_len=None):
def convert_args_to_str(args, max_len: Optional[int] = None) -> str:
"""Convert args to a string, allowing for some arguments to raise
exceptions during conversion and ignoring them.
"""
Expand All @@ -4042,7 +4051,7 @@ def convert_args_to_str(args, max_len=None):
return "({})".format(", ".join(strs))


def convert_kwargs_to_str(kwargs, max_len=None):
def convert_kwargs_to_str(kwargs: dict, max_len: Optional[int] = None) -> str:
"""Convert kwargs to a string, allowing for some arguments to raise
exceptions during conversion and ignoring them.
"""
Expand Down