MartinThoma / flake8-simplify

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

SIM401 false positive #83

Closed MetRonnie closed 2 years ago

MetRonnie commented 2 years ago
if 'foo' in some_dict['a']:
    some_dict['b'] = some_dict['a']['foo']
else:
    some_dict['a']['foo'] = some_dict['b']

flake8-simplify incorrectly tells us:

Use 'some_dict['b'] = some_dict['a'].get(foo, "some_dict['b']")' instead of an if-block

but that is not equivalent in this case

MartinThoma commented 2 years ago

You are correct: The recommendation of flake8-simplify==0.14.5 is wrong. Thank you for reporting it!

I will work on this as soon as possible. This is a free-time project for me, so I'm uncertain when this will be the case.

As an intermediate fix, you can either ignore this line via:

if 'foo' in some_dict['a']:  # noqa: SIM401
    some_dict['b'] = some_dict['a']['foo']
else:
    some_dict['a']['foo'] = some_dict['b']

Or you can deactivate that rule completely (e.g. via .flake8 / tox.ini / setup.cfg):

[flake8]
ignore = SIM401
MartinThoma commented 2 years ago

This bug appears in Python 3.9.1 but not in Python 3.7.7. I guess there might be two bugs: The one that causes the false-positive in Python 3.9 and one causing a false-negative in Python 3.7

MartinThoma commented 2 years ago

Test was added here: https://github.com/MartinThoma/flake8-simplify/pull/85

MartinThoma commented 2 years ago

Thank you very much for your help. The issue was fixed in flake8-simplify==0.14.6