Skip to content

python/unpacked-comprehension

Pack Severity Fix Versions
Python warning unsafe, with xpd check --fix --unsafe-fixes All versions

A dict literal that unpacks a comprehension with ** is hard to read. Build the dict first and add the computed keys in a loop, or give the comprehension a name.

def row(model, limits):
return {"id": model.id, **{f"limit_{k}": v for k, v in limits.items()}}
out = {"id": model.id}
for name, value in limits.items():
out[f"limit_{name}"] = value
return out

The rule reports a dict literal that unpacks a dict comprehension with **.

xpd has no automatic fix, so make the change by hand.

The literal builds a dict, unpacks it and merges it in one expression. A reader has to work through the comprehension to know which keys the result has.

This dict literal unpacks a comprehension with **. Build the dict first and add the computed keys in a loop, or give the comprehension a name.

See all rules in the Python pack.