Skip to content

Detect noqa directives for multi-line f-strings - #7326

Merged
dhruvmanila merged 4 commits into
dhruv/pep-701from
dhruv/issue-7291
Sep 21, 2023
Merged

Detect noqa directives for multi-line f-strings#7326
dhruvmanila merged 4 commits into
dhruv/pep-701from
dhruv/issue-7291

Conversation

@dhruvmanila

@dhruvmanila dhruvmanila commented Sep 13, 2023

Copy link
Copy Markdown
Member

Summary

This PR updates the NoQA directive detection to consider the new f-string tokens.

The reason being that now there can be multi-line f-strings without triple-quotes:

f"{
x
	*
		y
}"

Here, the noqa directive should go at the end of the last line.

Test Plan

  • Add new test cases for f-strings

  • Tested with --add-noqa using the following command with the above code snippet:

     $ cargo run --bin ruff -- check --select=F821 --no-cache --isolated ~/playground/ruff/fstring.py --add-noqa
     Added 1 noqa directive.

    Output:

     f"{
     x
     	*
         	y
     }"  # noqa: F821

    Running the same command again doesn't add noqa directive and without the --add-noqa flag, the violation isn't reported.

fixes: #7291

@dhruvmanila dhruvmanila linked an issue Sep 13, 2023 that may be closed by this pull request
@dhruvmanila dhruvmanila added python312 Related to Python 3.12 suppression Related to supression of violations e.g. noqa labels Sep 13, 2023
@dhruvmanila
dhruvmanila force-pushed the dhruv/issue-7291 branch 2 times, most recently from fbcedf2 to 9c4d048 Compare September 14, 2023 02:29
@dhruvmanila
dhruvmanila changed the base branch from dhruv/issue-7290 to dhruv/pep-701 September 14, 2023 02:40
@dhruvmanila
dhruvmanila changed the base branch from dhruv/pep-701 to dhruv/issue-7290 September 14, 2023 02:48
Comment thread crates/ruff/src/directives.rs Outdated
Comment on lines +110 to +112
// SAFETY: If we're at the end of a f-string, the indexer must have a
// corresponding f-string range for it.
let f_string_start = indexer.f_string_range(range.start()).unwrap().start();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a slight performance penalty here as not many single quoted f-strings are going to be multi-line but we can't really know that until we get the entire f-string range.

@dhruvmanila
dhruvmanila marked this pull request as ready for review September 14, 2023 03:14
Comment thread crates/ruff/src/directives.rs Outdated
Base automatically changed from dhruv/issue-7290 to dhruv/pep-701 September 19, 2023 06:25
@dhruvmanila
dhruvmanila force-pushed the dhruv/issue-7291 branch 2 times, most recently from 7f349c5 to e01be7c Compare September 20, 2023 03:48
@dhruvmanila
dhruvmanila merged commit 89f1c0c into dhruv/pep-701 Sep 21, 2023
@dhruvmanila
dhruvmanila deleted the dhruv/issue-7291 branch September 21, 2023 02:21
dhruvmanila added a commit that referenced this pull request Sep 21, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 22, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 22, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 22, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 26, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 27, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 28, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 29, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
dhruvmanila added a commit that referenced this pull request Sep 29, 2023
## Summary

This PR updates the NoQA directive detection to consider the new
f-string tokens.

The reason being that now there can be multi-line f-strings without
triple-quotes:

```python
f"{
x
	*
		y
}"
```

Here, the `noqa` directive should go at the end of the last line.

## Test Plan

* Add new test cases for f-strings
* Tested with `--add-noqa` using the following command with the above
code snippet:
	```console
$ cargo run --bin ruff -- check --select=F821 --no-cache --isolated
~/playground/ruff/fstring.py --add-noqa
	Added 1 noqa directive.
	```

	Output:
	```python
	f"{
	x
    	*
        	y
	}"  # noqa: F821
	```
Running the same command again doesn't add `noqa` directive and without
the `--add-noqa` flag, the violation isn't reported.

fixes: #7291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python312 Related to Python 3.12 suppression Related to supression of violations e.g. noqa

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect noqa directives inside f-strings

2 participants