Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from collections.abc import Set as AbstractSet # Ok | ||
|
||
|
||
from collections.abc import Set # Ok | ||
|
||
|
||
from collections.abc import ( | ||
Container, | ||
Sized, | ||
Set, # Ok | ||
ValuesView | ||
) | ||
|
||
from collections.abc import ( | ||
Container, | ||
Sized, | ||
Set as AbstractSet, # Ok | ||
ValuesView | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from collections.abc import Set as AbstractSet # Ok | ||
|
||
|
||
from collections.abc import Set # PYI025 | ||
|
||
|
||
from collections.abc import ( | ||
Container, | ||
Sized, | ||
Set, # PYI025 | ||
ValuesView | ||
) | ||
|
||
from collections.abc import ( | ||
Container, | ||
Sized, | ||
Set as AbstractSet, | ||
ValuesView # Ok | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
crates/ruff/src/rules/flake8_pyi/rules/unaliased_collections_abc_set_import.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use rustpython_parser::ast::StmtImportFrom; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `from collections.abc import Set` imports that do not alias | ||
/// `Set` to `AbstractSet`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `Set` type in `collections.abc` is an abstract base class for set-like types. | ||
/// It is easily confused with, and not equivalent to, the `set` builtin. | ||
/// | ||
/// To avoid confusion, `Set` should be aliased to `AbstractSet` when imported. This | ||
/// makes it clear that the imported type is an abstract base class, and not the | ||
/// `set` builtin. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from collections.abc import Set | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from collections.abc import Set as AbstractSet | ||
/// ``` | ||
#[violation] | ||
pub struct UnaliasedCollectionsAbcSetImport; | ||
|
||
impl Violation for UnaliasedCollectionsAbcSetImport { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!( | ||
"Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin" | ||
) | ||
} | ||
|
||
fn autofix_title(&self) -> Option<String> { | ||
Some(format!("Alias `Set` to `AbstractSet`")) | ||
} | ||
} | ||
|
||
/// PYI025 | ||
pub(crate) fn unaliased_collections_abc_set_import(checker: &mut Checker, stmt: &StmtImportFrom) { | ||
let Some(module_id) = &stmt.module else { | ||
return; | ||
}; | ||
if module_id.as_str() != "collections.abc" { | ||
return; | ||
} | ||
|
||
for name in &stmt.names { | ||
if name.name.as_str() == "Set" && name.asname.is_none() { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnaliasedCollectionsAbcSetImport, | ||
name.range, | ||
)); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI025_PYI025.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
22 changes: 22 additions & 0 deletions
22
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI025_PYI025.pyi.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI025.pyi:4:29: PYI025 Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin | ||
| | ||
4 | from collections.abc import Set # PYI025 | ||
| ^^^ PYI025 | ||
| | ||
= help: Alias `Set` to `AbstractSet` | ||
|
||
PYI025.pyi:10:5: PYI025 Use `from collections.abc import Set as AbstractSet` to avoid confusion with the `set` builtin | ||
| | ||
10 | Container, | ||
11 | Sized, | ||
12 | Set, # PYI025 | ||
| ^^^ PYI025 | ||
13 | ValuesView | ||
14 | ) | ||
| | ||
= help: Alias `Set` to `AbstractSet` | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.