flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

[Question] Automatically fix yoda-conditions

Open conradogarciaberrotaran opened this issue 2 years ago • 3 comments

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!

conradogarciaberrotaran avatar Apr 04 '22 13:04 conradogarciaberrotaran

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.

MartinThoma avatar Apr 05 '22 15:04 MartinThoma

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

conradogarciaberrotaran avatar Apr 05 '22 16:04 conradogarciaberrotaran

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!

MartinThoma avatar Apr 05 '22 17:04 MartinThoma