A dict comprehension uses the same literal key for every element

tx0/python:comprehension-key-never-variesdefaultwarningbug

What it finds

{"name": item for item in items}: the key is the same for every element, so each one replaces the last and the dict ends with one entry. The key was meant to depend on the element, as in {item.name: item for item in items}.

From the rule's source, comprehension_key_never_varies.rs.

Example from the rule's tests
def index(items):    a = {'name': item for item in items}tx0 reports: every element gets the key 'name', so the dict ends with one entry    b = {item.name: item for item in items}    c = {1: item for item in items}tx0 reports: every element gets the key 1    d = {(None): item for item in items}tx0 reports: every element gets the key None    e = {f"{item}": item for item in items}    return a, b, c, d, e
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:comprehension-key-never-varies" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:comprehension-key-never-varies] reason="..."

tx0 explain tx0/python:comprehension-key-never-varies shows the same in the terminal. Found a false positive? Report it.