[New-bugs-announce] [issue37068] Emit SyntaxWarning for f-strings without expressions ?

Matthias Bussonnier report at bugs.python.org
Mon May 27 15:11:14 EDT 2019


New submission from Matthias Bussonnier <bussonniermatthias at gmail.com>:

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.

----------
components: Interpreter Core
messages: 343672
nosy: mbussonn
priority: normal
severity: normal
status: open
title: Emit SyntaxWarning for f-strings without expressions ?
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37068>
_______________________________________


More information about the New-bugs-announce mailing list