Skip to content

Detect noqa directives inside f-strings #7291

@dhruvmanila

Description

@dhruvmanila

noqa comments for strings goes at the end of it. Similarly, for multi-line strings (triple-quoted), the noqa directive is expected on the last line of the string.

Now, with the new f-string specification, multi-line expressions are allowed in a single-quoted f-string. For example, the following code is valid in Python 3.12 but invalid before 3.12:

f"foo {
    a
        *
            b
}"

We need to make sure to add the noqa directives at the end of f-string regardless if it's single or triple-quoted. For the above example, the outcome would be:

f"foo {
    a
        *
            b
}"  # noqa: F821

The extract_noqa_line_for function needs to be updated to fix this. Currently, it only checks for tripled-quoted strings using the String token. Now, we will need to use the FStringStart token to (1) check if it's a tripled-quoted f-string, if it is then (2) get the end range of the corresponding FStringEnd token. This could be done by either using a stack or using the Indexer to get the f-string range.

Metadata

Metadata

Assignees

Labels

python312Related to Python 3.12suppressionRelated to supression of violations e.g. noqa

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions