python/unpacked-comprehension
| Rule | unpacked-comprehension |
|---|---|
| Pack | Python |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
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 outWhat the rule reports
Section titled “What the rule reports”The rule reports a dict literal that unpacks a dict comprehension with **.
How to fix it
Section titled “How to fix it”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.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
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.Browse the other rules of the Python pack.