Skip to content

multi-value-repeated-key-variable (F602)

Derived from the Pyflakes linter.

Fix is sometimes available.

What it does

Checks for dictionary keys that are repeated with different values.

Why is this bad?

Dictionary keys should be unique. If a key is repeated with a different value, the first values will be overwritten and the key will correspond to the last value. This is likely a mistake.

Example

foo = {
    bar: 1,
    baz: 2,
    baz: 3,
}
foo[baz]  # 3

Use instead:

foo = {
    bar: 1,
    baz: 2,
}
foo[baz]  # 2

Fix safety

This rule's fix is marked as unsafe because removing a repeated dictionary key may delete comments that are attached to the removed key-value pair. This can also change the program's behavior if the value expressions have side effects.

References