Skip to content

Nested f-string quotes raise C4001 in Python 3.12 #32

@fmigneault

Description

@fmigneault

Defining a string as follows:

x = True
y = f"{'x' if x else 'y'}/z"

Using Python 3.12 flags this as invalid quote style with the message C4001: Invalid string quote ', should be " (invalid-string-quote).

Running the same code with previous Python version does not create any warning.

While nested f"{"x"}" is supported in Python 3.12 (https://peps.python.org/pep-0701/), making the warning valid in that case, code that needs to be backward compatible with older versions of Python as well have no choice to use the different single quote to avoid syntax errors.

# Python 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0] on linux
# Type "help", "copyright", "credits" or "license" for more information.
>>> x=f"{"x"}"
  File "<stdin>", line 1
    x=f"{"x"}"
          ^
SyntaxError: f-string: expecting '}'

There should be a separate warning (not C4001) for that particular case, such that code that needs to purposely allow them can provide an appropriate ignore=C400X to pylint. Currently, the only options are to stay with Python 3.11 and below, or ignore C4001 completely, which mostly invalidates using this plugin altogether.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions