Or more precisely I suggest to emit a SyntaxWarning for JoinedStr where no f-string members has an expression.
There seem to be a couple of case where f-strings without expressions may indicate
that the programmer may have made a mistake; though there are also a number of
case where f-string w/o expressions are legitimate; I believe we should be able
to emit a SyntaxWarning in case where `f` are really unnecessary or might be a
mistakes.
First case where f-string w/o expressions are legitimate: multiline joined string,
to make sure each line is aligned. Example from CPython source:
# not a SyntaxWarning.
msg = (
f'error while attempting to bind on '
f'address {laddr!r}: '
f'{exc.strerror.lower()}'
)
The first line has obviously no expression, but the f is useful for visual
consistency, and we likely do not want a warning.
Though in the above case we can imagine the following typo :
msg = (
f'error while attempting to bind on ', #SyntaxWarning here
f'address {laddr!r}: ',
f'{exc.strerror.lower()}'
)
Easy to make and in this case, the expression-less f-string is likely an error.
In this case a syntax warning would help to distinguish that there are trailing
commas.
Another case from the cpython is the following:
fullName = f'%s.%s.%s' % ( #SyntaxWarning here
testCaseClass.__module__, testCaseClass.__qualname__, attrname
)
Looking at the history; I believe the author was meaning to change to an
f-string, but got interrupted half-way and only added the prefix.
Pep 498 does not seem to say anything about f-string w/o expressions; but
test-suite appear to test that f-string without expression do not raise an
error. I do not believe that making it an error is in anyway desirable;
I believe a SyntaxWarning would align with the current warning on invalid
escape sequence, and help – mostly during refactoring, if an f-string loses some of its parameters, and the f"" if non-intentionally kept.
|