-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
False positive consider-using-f-string
for localization string
#5039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I would like the opinion of @Pierre-Sassoulas and @cdce8p on this. Personally, since f-strings give a performance improvement over |
As @DanielNoord mentioned, I've disabled the check for me personally. I use I can acknowledge that for some this check might be quite helpful. Maybe moving it to an optional checker would resolve that situation. That would fit together nicely with #4787 |
First, let's be very clear that opinions about what is more readable are not the main reason why we added the checker as default. It's mainly because of provable major relative speed disparities. You're incurring a 3* time of processing when using format. We have had a similar warning for logger lazy formatting for a long time and for a smaller performance gain, so this warning is not out of character for pylint. If you're localizing or have a lot of template in your project, it's possible to disable this in pylintrc. Also, it's possible to reasonably deal with long string using other tricks that do not impact performance, for example using triple quotes: my_long_str = f"""
Some big long string format with a date:
{local_date} in {local_time}
""" Or multiple line: my_long_str = (
"Some big long string format with a date:\n"
f"{local_date} in {local_time}"
) Now, I agree there is a false positive for format template but the idea behind still shipping it was that templating is not used so often that it's unreasonable to disable this warning on template (we did it for pylint we had maybe 5 false positives on 20ksloc). We deal with a similar issue with We have a hard time distinguishing legit template strings that are formatted to be used later with format elsewhere ie |
consider-using-f-string
for string template and localization string
As I'm the original author of this checker I am happy to take this one, but I need some more info about the false positive. What is raising a message that shouldn't raise a message? |
For me, false positive for |
Yes, but we don't emit a = "string {}".format(1) # consider-using-f-string
b = "template string {}"
b.format(1)
************* Module test
test.py:1:4: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
------------------------------------------------------------------
Your code has been rated at 6.67/10 (previous run: 0.00/10, +6.67) So defining template strings and using |
consider-using-f-string
for string template and localization stringconsider-using-f-string
for localization string
My bad, I should have remembered that. So in my opinion the only false positive is for localization with n = len(os.listdir('.'))
cat = GNUTranslations(somefile)
message = cat.ngettext(
'There is %(num)d file in this directory',
'There are %(num)d files in this directory',
n) % {'num': n} |
❯ cat test.py
import os
from gettext import GNUTranslations
n = len(os.listdir("."))
cat = GNUTranslations(somefile)
message = cat.ngettext(
"There is %(num)d file in this directory", "There are %(num)d files in this directory", n
) % {"num": n}
❯ pylint '***/pylint/test.py'
************* Module test
test.py:5:22: E0602: Undefined variable 'somefile' (undefined-variable)
------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00) Doesn't seem to issue a message 😄 |
Well, I think pylint should be a bit more smart
_ctx.logger.info(f'Processes to be spawned: {jobs}')
_ctx.logger.info(f'Processes to be spawned: %s' % jobs) It looks like some kind of trolling. |
This is indeed a false positive. Sorry about that! Going to work on a fix now! Edit: just saw that this is not based on |
Am I missing something, or aren't both examples provided by @sergeyklay entirely valid pylint messages? |
I think |
The right way if you want to use lazy formatting so the string formatting is done only if necessary is: _ctx.logger.info('Processes to be spawned: %s', jobs) If you want to use f-string in logging you may disable |
@Pierre-Sassoulas do you want me to make a PR where |
Maybe we need a better documentation about lazy formatting in logging, but there is nothing to change in the code itself as the following correct lazy formatted logging: import logging
logger = logging.getLogger(__file__)
logger.info('Processes to be spawned: %s', "jobs") Does not raise any false positives right now. |
Oh, you're right. My bad :/ As an excuse, I can only say that I probably wanted to get rid of it too quickly. In any case, a bit more detailed explanation in the documentation would not hurt |
@Pierre-Sassoulas I think we can close this issue then, right? Besides the debate on whether this checker should be enabled by default I don't think there are any false positives left in this issue? |
When using f strings in a logger `pylint` complains. Updated based on `pylint` developer recommending to turn this off as f strings are very-efficient. Comment below pylint-dev/pylint#5039 (comment)
When using f strings in a logger `pylint` complains. Updated based on `pylint` developer recommending to turn this off as f strings are very-efficient. Comment below pylint-dev/pylint#5039 (comment)
Current problem
just got a bunch of "consider-using-f-string".
Desired solution
f-strings basically defeats localization. the idea that f-strings are the "right thing to do all the time" is wrong. doesn't belong in a linter.
Additional context
yeah, i'm sure some people argued about this in a pep... but this is the first time the rest of the world bumped into it. f-strings are only "more readable" when there are many parameters, and even then .... not so much:
some_big_long_string.format(date=local_date, time=local_time, username=current_user)
raise UserFacingException(localize("An error in %{program_name} occured"))
why pylint these?
The text was updated successfully, but these errors were encountered: