Skip to content

False-positive for consider-iterating-dictionary #5478

Closed
@WolfgangFellger

Description

@WolfgangFellger

Bug description

In the following example, line 4 is reported as consider-iterating-dictionary. However, without the .keys() the expression raises a TypeError. Please note that line 6 is not reported, this seems to only be triggered with an in.

a_dict = {"a": 1, "b": 2, "c": 3}
a_set = {"c", "d"}

if "a" in a_dict.keys() | a_set:  # reports consider-iterating-dictionary
	pass
print(a_dict.keys() | a_set)      # not reported
print(a_dict | a_set)             # TypeError```

Command used

pylint test.py

Pylint output

************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:4:10: C0201: Consider iterating the dictionary directly instead of calling .keys() (consider-iterating-dictionary)

Expected behavior

if "a" in a_dict.keys() | a_set should not be reported, as the proposed fix is a TypeError.

Pylint version

pylint 2.12.2
astroid 2.9.0
Python 3.9.9 (main, Nov 17 2021, 16:30:15) 
[GCC 10.2.1 20210110]

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions