Skip to content

Commit aaab17c

Browse files
Return default on KeyError at any level in get_metadata (#7109)
1 parent 61353b7 commit aaab17c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

distributed/scheduler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6939,10 +6939,10 @@ def set_metadata(self, keys: list[str], value: object = None) -> None:
69396939

69406940
def get_metadata(self, keys: list[str], default=no_default):
69416941
metadata = self.task_metadata
6942-
for key in keys[:-1]:
6943-
metadata = metadata[key]
69446942
try:
6945-
return metadata[keys[-1]]
6943+
for key in keys:
6944+
metadata = metadata[key]
6945+
return metadata
69466946
except KeyError:
69476947
if default != no_default:
69486948
return default

distributed/tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5583,10 +5583,29 @@ def fib(x):
55835583

55845584
@gen_cluster(client=True)
55855585
async def test_task_metadata(c, s, a, b):
5586+
with pytest.raises(KeyError):
5587+
await c.get_metadata("x")
5588+
with pytest.raises(KeyError):
5589+
await c.get_metadata(["x"])
5590+
result = await c.get_metadata("x", None)
5591+
assert result is None
5592+
result = await c.get_metadata(["x"], None)
5593+
assert result is None
5594+
5595+
with pytest.raises(KeyError):
5596+
await c.get_metadata(["x", "y"])
5597+
result = await c.get_metadata(["x", "y"], None)
5598+
assert result is None
5599+
55865600
await c.set_metadata("x", 1)
55875601
result = await c.get_metadata("x")
55885602
assert result == 1
55895603

5604+
with pytest.raises(TypeError):
5605+
await c.get_metadata(["x", "y"])
5606+
with pytest.raises(TypeError):
5607+
await c.get_metadata(["x", "y"], None)
5608+
55905609
future = c.submit(inc, 1)
55915610
key = future.key
55925611
await wait(future)

0 commit comments

Comments
 (0)