adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.83k stars 269 forks source link

style: Use f-strings wherever possible #600

Closed DimitriPapadopoulos closed 11 months ago

DimitriPapadopoulos commented 11 months ago

They're faster.

coveralls commented 11 months ago

Coverage Status

coverage: 99.391% (-0.002%) from 99.393% when pulling cb7e19a9cd9ce15fe08e5e66f719bd3a95c7e4cc on DimitriPapadopoulos:f-strings into 816d575228a075a4599fdc3027797c483e96cdfa on adrienverge:master.

DimitriPapadopoulos commented 11 months ago

Have you changed them one by one and checked they all make sense? Or did you use a script to find and replace them? (in some rare cases like %02d, %.2f or %-10s, f-string cannot be used as is)

There are not such cases as far as I can see. Yet, it possible to write f{variable!02d}f{variable:02d} or f{variable!.2f}f{variable:.2f}, isn't it?

adrienverge commented 11 months ago

There are not such cases as far as I can see. Yet, it possible to write f{variable!02d} or f{variable!.2f}, isn't it?

Sure it is! I just wanted to make sure you (or your custom script?) looked for them, if any :)

I still see a few %s and %d in some files (namely yamllint/linter.py, yamllint/rules/line_length.py, yamllint/rules/indentation.py and yamllint/rules/octal_values.py). Could you check them?

DimitriPapadopoulos commented 11 months ago

As for the tool I use, it's pyupgrade. But I often end up applying changes manually, because:

  1. You cannot limit pyupgrade to a single class of changes. I prefer an individual commit per class of changes.
  2. It's not perfect: it often leaves behind some cases of % string interpolation.
DimitriPapadopoulos commented 11 months ago

I have left an instance of % string interpolation in yamllint/rules/line_length.py because I find the f-string interpolation is less readable – unless of course I use additional variables to store the result of complex expressions.