Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Sep 21, 2023

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Sep 13, 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:

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 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
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
@dhruvmanila dhruvmanila merged commit 89f1c0c into dhruv/pep-701 Sep 21, 2023
12 of 15 checks passed
@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