Repository: dask/distributed

Issue:
Add `flake8-bugbear` as additional `flake8` plugin
`flake8-bugbear` ([https://github.com/PyCQA/flake8-bugbear](https://github.com/PyCQA/flake8-bugbear)) is a plugin for `flake8` that contains additional rules aimed at finding likely bugs and design problems in the code. Notable rules include:
* B006: Do not use mutable data structures for argument defaults.
* B018: Found useless expression.

At the time of this writing, adding `flake8-bugbear` spits out 166 problems, most of which are in tests. If we decide to go with `flake8-bugbear`, we could ignore it for tests and clean those up over time before flipping the switch.


Discussion:

Resolving patch:
diff --git a/distributed/worker.py b/distributed/worker.py
--- a/distributed/worker.py
+++ b/distributed/worker.py
@@ -1542,7 +1542,7 @@ async def close(  # type: ignore
             if executor is utils._offload_executor:
                 continue  # Never shutdown the offload executor
 
-            def _close(wait):
+            def _close(executor, wait):
                 if isinstance(executor, ThreadPoolExecutor):
                     executor._work_queue.queue.clear()
                     executor.shutdown(wait=wait, timeout=timeout)
@@ -1556,17 +1556,19 @@ def _close(wait):
             if is_python_shutting_down():
                 # If we're shutting down there is no need to wait for daemon
                 # threads to finish
-                _close(wait=False)
+                _close(executor=executor, wait=False)
             else:
                 try:
-                    await to_thread(_close, wait=executor_wait)
+                    await to_thread(_close, executor=executor, wait=executor_wait)
                 except RuntimeError:  # Are we shutting down the process?
                     logger.error(
                         "Could not close executor %r by dispatching to thread. Trying synchronously.",
                         executor,
                         exc_info=True,
                     )
-                    _close(wait=executor_wait)  # Just run it directly
+                    _close(
+                        executor=executor, wait=executor_wait
+                    )  # Just run it directly
 
         self.stop()
         await self.rpc.close()


Test patch:
diff --git a/distributed/tests/test_client.py b/distributed/tests/test_client.py
--- a/distributed/tests/test_client.py
+++ b/distributed/tests/test_client.py
@@ -3896,10 +3896,10 @@ def test_threaded_get_within_distributed(c):
 
     for get in [dask.local.get_sync, dask.multiprocessing.get, dask.threaded.get]:
 
-        def f():
+        def f(get):
             return get({"x": (lambda: 1,)}, "x")
 
-        future = c.submit(f)
+        future = c.submit(f, get)
         assert future.result() == 1
 
 
@@ -4017,8 +4017,8 @@ async def test_serialize_future(s, a, b):
     result = await future
 
     for ci in (c1, c2):
-        for ctxman in ci.as_current, lambda: temp_default_client(ci):
-            with ctxman():
+        for ctxman in lambda ci: ci.as_current(), lambda ci: temp_default_client(ci):
+            with ctxman(ci):
                 future2 = pickle.loads(pickle.dumps(future))
                 assert future2.client is ci
                 assert stringify(future2.key) in ci.futures