Skip to content

Incorrect NoQA placement for continuation with multi-line string #7530

@dhruvmanila

Description

@dhruvmanila

Take the following code snippet:

assert foo, \
    """triple-quoted
    string"""

Running the following command repeatedly:

$ ruff check --no-cache --isolated --select=F821 fstring.py --add-noqa
Added 1 noqa directive.

$ ruff check --no-cache --isolated --select=F821 fstring.py --add-noqa
Added 1 noqa directive.

...

It keeps on adding the noqa directive inside the first line of the string:

assert foo, \
    """triple-quoted  # noqa: F821  # noqa: F821
    string"""

I think the problem is in the way the range is being pushed onto the NoQA mapping in the following code:

pub(crate) fn push_mapping(&mut self, range: TextRange) {
if let Some(last_range) = self.ranges.last_mut() {
// Strictly sorted insertion
if last_range.end() <= range.start() {
// OK
}
// Try merging with the last inserted range
else if let Some(intersected) = last_range.intersect(range) {
*last_range = intersected;
return;
} else {
panic!("Ranges must be inserted in sorted order")
}
}

  1. The <= doesn't make it strict
  2. We should use the union range instead of intersected range

So,

    pub(crate) fn push_mapping(&mut self, range: TextRange) {
        if let Some(last_range) = self.ranges.last_mut() {
            // Strictly sorted insertion
            if last_range.end() < range.start() {
                // OK
            } else if range.end() < last_range.start() {
                panic!("Ranges must be inserted in sorted order")
            } else {
                *last_range = last_range.cover(range);
                return;
            }
        }

        self.ranges.push(range);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsuppressionRelated 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