MartinThoma / flake8-simplify

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

[Question] Automatically fix yoda-conditions #137

Open conradogarciaberrotaran opened 2 years ago

conradogarciaberrotaran commented 2 years ago

Hi, first of all, thanks for the great tool. I have several apps with a lot of yoda-conditions and I wanted to see if there is a way to make it automatically fix. Is it possible to implement this?

Thanks!

MartinThoma commented 2 years ago

Thank you!

I was actually wondering about this several times myself. In principle, I would have the necessary information to fix things. However, this is currently not possible with Flake8: https://github.com/PyCQA/flake8/issues/545

In the past, when I had to do a lot of changes to fix such an issue, I did it gradually:

  1. Disable the rule
  2. Fix the rule whenever I see it
  3. Once all are fixed: Enable the rule

Depending on the project size, this can take a while.

conradogarciaberrotaran commented 2 years ago

I'll look more into https://github.com/PyCQA/autoflake, but it seems like there's no plugin interface.

MartinThoma commented 2 years ago

There is actually a simple combination of tools that might do the job :

  1. Black cover formatter
  2. Find the joda conditions with flake8-simplify
  3. Use simple string search+replace

If you find something that works well, please share it here!