You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At work we stumbled upon a bug in an old code, that used % formatting, but the fmt string was wrong, because it didn't contain any replacement fields. It would be nice if pylint could warn about format strings without any replacement fields.
Current behavior
defdemo(param):
print("1asfasfdsf dasfdadfsdf"%param) # no warning (code like this line was used in our code-base)print("2asfasfdsf dasfdadfsdf"% ()) # no warningprint("3asfasfdsf dasfdadfsdf"% []) # pylint outputs too-many-format-args# this line raises when you run the script using cpythonprint("4asfasfdsf dasfdadfsdf"%None) # pylint outputs too-many-format-argsdemo(())
demo.py:4:10: E1305: Too many arguments for format string (too-many-format-args)
demo.py:7:10: E1305: Too many arguments for format string (too-many-format-args)
When using .format the behavior is a bit different (both in cpython as well as in pylint)
Contrary to the first example, this one doesn't raise an exception when you run it using cpython. However, pylint considers all print function calls as invalid, which is wrong, because cpython doesn't raise.
I don't understand why python supports format strings without any replacement fields. Maybe because if the fmt string and the args passed to e.g. .format are determined dynamically.
Anyway, I think pylint should output an error (e.g. bad-format-string) if the format string is a string without any replacement fields (as in the examples above).
Things I want to mention but I don't care about them:
There is an inconsistency between lists and tuples if used as format string args:
This adds a checker that checks strings with '%' or format() applied to them.
If no variables to be replaced are found the warning is emitted.
Closespylint-dev#4042
* Add ``format-string-without-interpolation`` checker
This adds a checker that checks strings with '%' or format() applied to them.
If no variables to be replaced are found the warning is emitted.
Closes#4042
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
At work we stumbled upon a bug in an old code, that used
%
formatting, but the fmt string was wrong, because it didn't contain any replacement fields. It would be nice if pylint could warn about format strings without any replacement fields.Current behavior
When using
.format
the behavior is a bit different (both in cpython as well as in pylint)Contrary to the first example, this one doesn't raise an exception when you run it using cpython. However, pylint considers all print function calls as invalid, which is wrong, because cpython doesn't raise.
Expected behavior
I don't understand why python supports format strings without any replacement fields. Maybe because if the fmt string and the args passed to e.g.
.format
are determined dynamically.Anyway, I think pylint should output an error (e.g.
bad-format-string
) if the format string is a string without any replacement fields (as in the examples above).Things I want to mention but I don't care about them:
There is an inconsistency between lists and tuples if used as format string args:
There are differences between
%
formating and.format
formatting in pylint.pylint --version output
The text was updated successfully, but these errors were encountered: