MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

SIM118 false positive when using awsglue.dynamicframe.DynamicFrameCollection #175

Closed robmcd closed 1 year ago

robmcd commented 1 year ago

With code like:

for dyf_name in dfc.keys():
    print(dyf_name)

Where dfc is an instance of DynamicFrameCollection

We see:

SIM118 [*] Use dyf_name in dfc instead of dyf_name in dfc.keys()

However, the suggested change will produce a KeyError.

Is there some way to fix the recommendation to avoid these false positives (apart from ignoring the rule/line)?

MartinThoma commented 1 year ago

Sadly, this cannot be fixed in flake8-simplify with reasonable effort. At this point, flake8-simplify thinks it's a dictionary because it sees the "keys()" method being used. There is no type information. You can disable the rule globally or on that line.

If you have control over DynamicFrameCollection, you might also want to implement __contains__ in a way that behaves like Python dictionaries.