Skip to content

F601: Repeated int keys not detected with booleans #12772

@addoolit

Description

@addoolit

The multi-value-repeated-key-literal (F601) rule doesn't detect when a key has been duplicated for True and False boolean when they duplicate a matching int literal. This is also true for the reverse where an int literal duplicates an existing bool key. This was found with ruff 0.5.7 and Python 3.12.2.

mydict = {
    1: "abc",
    1: "def",
    True: "ghi",
    0: "foo",
    0: "bar",
    False: "baz",
}

Ruff detects the int literal repetition, but misses the bool repetition.

> ruff.exe check ruff_repro.py --isolated
ruff_repro.py:3:5: F601 Dictionary key literal `1` repeated
  |
1 | mydict = {
2 |     1: "abc",
3 |     1: "def",
  |     ^ F601
4 |     True: "ghi",
5 |     0: "foo",
  |
  = help: Remove repeated key literal `1`

ruff_repro.py:6:5: F601 Dictionary key literal `0` repeated
  |
4 |     True: "ghi",
5 |     0: "foo",
6 |     0: "bar",
  |     ^ F601
7 |     False: "baz",
8 | }
  |
  = help: Remove repeated key literal `0`

Found 2 errors.

This state of this dict is ultimately {1: 'ghi', 0: 'baz'}. I haven't found a stated guarantee that True is always 1, and False is always 0, but it appears that they are. The only thing I've seen related is this note in Boolean type - bool:

In many numeric contexts, False and True behave like the integers 0 and 1, respectively. However, relying on this is discouraged; explicitly convert using int() instead.

Metadata

Metadata

Assignees

Labels

acceptedReady for implementationhelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions