-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Detect when f-string-syntax is used in a string, but not marked as an f-string #2507
Comments
Hey @glasnt ! This makes sense, but will be tricky due to the string potentially being used as a formatting string later on down the line. But we can definitely try to look for this pattern as it might become more prevalent now with the adoption of f-strings. Here's what we can do. Let me know if something of the following doesn't make sense.
That would be all that's needed to get this going. Let me know if there's anything else I can do to help. |
I would also like this check. Missing those dastardly little 'f's is a common occurrence in my experience, and while the edge cases noted in this thread are hypothetically valid, as best I can tell, not a single one of them apply to the code bases I worked in day to day. So I'd be more than happy to have at least a way to opt-in to this check (or, of course, having this be the default). |
I think this would be a very useful addition to have - +1 Though it's worth noting that there are edge cases where I still think some analysis on these would be an improvement |
The analysis would need to check if the contents are
|
Example of relevance: Many examples on https://thedailywtf.com/series/errord One of the most common errors people run into is interpolation code not being interpolated, resulting in things like
or frustrating things like
Such examples have been posted from many sources, including some of the largest IT companies, so missing interpolation seems to be a widespread and very user-facing bug type. |
This checks if text in strings in between `{}`'s are variables. The var the string is assigned to is also checked for a format() call. If this does not happen, the string should probably be a f-string and a message is emitted. This closes pylint-dev#2507
Blocked by pylint-dev/astroid#1156 or by the end of life of python 3.7, see #4787 (comment). |
A related issue is when using multi-line strings like so:
Without thinking too hard, my initial assumption was that the second string wouldn't need an |
Additionally, it would be nice to detect old-style unused formatting, e.g., |
Problem statement
Consider the following codeblock
While the block is correct syntax-wise, the content is almost certainly logically invalid, as seen below:
stmt2
is a valid string, but declares string interpolation formatting that is compatible with f-strings, but the string does not have thef
prefix to invoke the interpolation.Describe the solution you'd like
PyLint should detect strings that use f-string-like syntax, but have not been declared as f-strings, in Python 3.6+
This could either be by:
{}
, and if valid Python, return a warningEdge Case One: Having
{
and}
characters are perfectly valid, but on the edge case that the content within them is a reference to an in-scope variable, or some other expression, it is likely the string was meant to be an f-string. This would be desirable to catch as a linting error.Edge Case Two:
f'...'
is effectively shorthand for'...'.format(locals())
; so this:is entirely legal, and isn't an error. There is a possibly design decision about whether this linting check should be
Additional context
I understand PyLint already warns against f-strings in logging, but I can't locate the base functionality to detect f-strings in the first place. I'd be willing to help implement this feature, but would require some mentoring.
The text was updated successfully, but these errors were encountered: