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

Sort edits prior to deduplicating in quotation fix #11452

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ class C:

def func() -> DataFrame[[DataFrame[_P, _R]], DataFrame[_P, _R]]:
...


def f():
from pandas import DataFrame, Series

def func(self) -> DataFrame | list[Series]:
pass
11 changes: 9 additions & 2 deletions crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use std::cmp::Reverse;

use ruff_diagnostics::Edit;
use ruff_python_ast::helpers::{map_callable, map_subscript};
Expand Down Expand Up @@ -286,11 +287,17 @@ pub(crate) fn quote_annotation(

/// Filter out any [`Edit`]s that are completely contained by any other [`Edit`].
pub(crate) fn filter_contained(edits: Vec<Edit>) -> Vec<Edit> {
let mut edits = edits;

// Sort such that the largest edits are prioritized.
edits.sort_unstable_by_key(|edit| (edit.start(), Reverse(edit.end())));

// Remove any edits that are completely contained by another edit.
let mut filtered: Vec<Edit> = Vec::with_capacity(edits.len());
for edit in edits {
if filtered
if !filtered
.iter()
.all(|filtered_edit| !filtered_edit.range().contains_range(edit.range()))
.any(|filtered_edit| filtered_edit.range().contains_range(edit.range()))
{
filtered.push(edit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ quote.py:78:24: TCH002 [*] Move third-party import `pandas.DataFrame` into a typ
91 |- def func() -> DataFrame[[DataFrame[_P, _R]], DataFrame[_P, _R]]:
91 |+ def func() -> "DataFrame[[DataFrame[_P, _R]], DataFrame[_P, _R]]":
92 92 | ...
93 93 |
94 94 |

quote.py:78:35: TCH002 [*] Move third-party import `pandas.Series` into a type-checking block
|
Expand Down Expand Up @@ -337,5 +339,61 @@ quote.py:78:35: TCH002 [*] Move third-party import `pandas.Series` into a type-c
91 |- def func() -> DataFrame[[DataFrame[_P, _R]], DataFrame[_P, _R]]:
91 |+ def func() -> "DataFrame[[DataFrame[_P, _R]], DataFrame[_P, _R]]":
92 92 | ...
93 93 |
94 94 |

quote.py:96:24: TCH002 [*] Move third-party import `pandas.DataFrame` into a type-checking block
|
95 | def f():
96 | from pandas import DataFrame, Series
| ^^^^^^^^^ TCH002
97 |
98 | def func(self) -> DataFrame | list[Series]:
|
= help: Move into type-checking block

ℹ Unsafe fix
1 |+from typing import TYPE_CHECKING
2 |+
3 |+if TYPE_CHECKING:
4 |+ from pandas import DataFrame, Series
1 5 | def f():
2 6 | from pandas import DataFrame
3 7 |
--------------------------------------------------------------------------------
93 97 |
94 98 |
95 99 | def f():
96 |- from pandas import DataFrame, Series
97 100 |
98 |- def func(self) -> DataFrame | list[Series]:
101 |+ def func(self) -> "DataFrame | list[Series]":
99 102 | pass

quote.py:96:35: TCH002 [*] Move third-party import `pandas.Series` into a type-checking block
|
95 | def f():
96 | from pandas import DataFrame, Series
| ^^^^^^ TCH002
97 |
98 | def func(self) -> DataFrame | list[Series]:
|
= help: Move into type-checking block

ℹ Unsafe fix
1 |+from typing import TYPE_CHECKING
2 |+
3 |+if TYPE_CHECKING:
4 |+ from pandas import DataFrame, Series
1 5 | def f():
2 6 | from pandas import DataFrame
3 7 |
--------------------------------------------------------------------------------
93 97 |
94 98 |
95 99 | def f():
96 |- from pandas import DataFrame, Series
97 100 |
98 |- def func(self) -> DataFrame | list[Series]:
101 |+ def func(self) -> "DataFrame | list[Series]":
99 102 | pass
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec<Message> {
}

thread_local! {
static MAX_ITERATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(8) };
static MAX_ITERATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(10) };
}

pub fn set_max_iterations(max: usize) {
Expand Down
Loading