This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Emit SyntaxWarning for f-strings without expressions ?
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, mbussonn, rhettinger, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2019-05-27 19:11 by mbussonn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg343672 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-05-27 19:11
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.
msg343673 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-05-27 19:15
I think this would be better suited for a linter.
msg343676 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-27 19:24
I do not think that all uses of f-strings without expression are errors. Therefore it would be better to left this on linters. Linters are optional and usually are flexible in configuring what warnings and in what part of the code should be emitted.
msg344123 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-05-31 21:14
An f-string is equivalent to a format string and call. The exact details are not important.

An field with no value is an error either way.  (I prefer the compile-time exception.)
>>> f'{}a'
SyntaxError: f-string: empty expression not allowed
>>> '{}a'.format()
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    '{}a'.format()
IndexError: tuple index out of range

>>> 'abc'.format()
'abc'
>>> f'abc'
'abc'

I agree that neither needs a warning.  I actually think the useless call is worse than the unneeded prefix.

SyntaxWarnings are rare.  Invalid escapes are now errors.
>>> '\o'
SyntaxError: invalid escape sequence \o
msg344755 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-06-05 17:58
Adding my agreement that this is a feature, not a bug.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81249
2019-06-05 17:58:03rhettingersetnosy: + rhettinger
messages: + msg344755
2019-05-31 21:14:40terry.reedysetstatus: open -> closed

type: enhancement

nosy: + terry.reedy
messages: + msg344123
resolution: rejected
stage: resolved
2019-05-27 19:24:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg343676
2019-05-27 19:15:47eric.smithsetnosy: + eric.smith
messages: + msg343673
2019-05-27 19:11:14mbussonncreate