MartinThoma / flake8-simplify

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

[SIM904][False-positive] Accessing previous dict values #100

Closed Skylion007 closed 2 years ago

Skylion007 commented 2 years ago

Desired change

Explanation

Example

This is an example where the mentioned rule(s) would currently be suboptimal:

        perf = {"total_time": end_time - start_time}
        perf["frame_time"] = perf["total_time"] / total_frames
        perf["fps"] = 1.0 / perf["frame_time"]
        perf["time_per_step"] = time_per_step
        perf["avg_sim_step_time"] = total_sim_step_time / total_frames

or more simply:

        perf = {"a": 1}
        perf["b"] = perf["a"] / 10

Yes, in this case, it could be written by creating lots of additional variables to replace the references to previous values of "total_time" and "frame_time", but in more complicated dictionaries it seems like this rule could become more troublesome,

MartinThoma commented 2 years ago

Good catch, I'll fix that tomorrow.

MartinThoma commented 2 years ago

Just in case you missed it : in order to still add new rules quickly and don't disturb users of flake8-simplify, I'll use SIM9 rules. If there are no more false positives for 6 months, I'll change the rule to something not beginning with "9". This way people can ignore the SIM9 rules in their CI and just try them once in a while.

MartinThoma commented 2 years ago

This looks very similar to #99 . I'll address both in the same PR/commit