Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Duplicate key in dict literal

ID: py/duplicate-key-dict-literalKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - maintainability   - useless-code   - external/cwe/cwe-561Query suites:   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Dictionary literals are constructed in the order given in the source. This means that if a key is duplicated, the second key-value pair will overwrite the first; as a dictionary can only have one value per key.

Recommendation

Check for typos to ensure that the keys are supposed to be the same. If they are then decide which value is wanted and delete the other one.

Example

The following example will output"c", because the mapping between 2 and"b" is overwritten by the mapping from 2 to"c". The programmer may have meant to map 3 to"c" instead.

dictionary={1:"a",2:"b",2:"c"}# BAD: The `2` key is duplicated.print(dictionary[2])

References


[8]ページ先頭

©2009-2025 Movatter.jp