-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PYI024 for
flake8-pyi
plugin (#4756)
- Loading branch information
Showing
11 changed files
with
142 additions
and
0 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,9 @@ | ||
import collections | ||
|
||
person: collections.namedtuple # OK | ||
|
||
from collections import namedtuple | ||
|
||
person: namedtuple # OK | ||
|
||
person = namedtuple("Person", ["name", "age"]) # 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import collections | ||
|
||
person: collections.namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
|
||
from collections import namedtuple | ||
|
||
person: namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
|
||
person = namedtuple( | ||
"Person", ["name", "age"] | ||
) # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" |
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
64 changes: 64 additions & 0 deletions
64
crates/ruff/src/rules/flake8_pyi/rules/collections_named_tuple.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,64 @@ | ||
use rustpython_parser::ast::Expr; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::prelude::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `collections.namedtuple` in stub files. | ||
/// | ||
/// ## Why is this bad? | ||
/// `typing.NamedTuple` is the "typed version" of `collections.namedtuple`. | ||
/// | ||
/// The class generated by subclassing `typing.NamedTuple` is equivalent to | ||
/// `collections.namedtuple`, with the exception that `typing.NamedTuple` | ||
/// includes an `__annotations__` attribute, which allows type checkers to | ||
/// infer the types of the fields. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from collections import namedtuple | ||
/// | ||
/// | ||
/// person = namedtuple("Person", ["name", "age"]) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from typing import NamedTuple | ||
/// | ||
/// | ||
/// class Person(NamedTuple): | ||
/// name: str | ||
/// age: int | ||
/// ``` | ||
#[violation] | ||
pub struct CollectionsNamedTuple; | ||
|
||
impl Violation for CollectionsNamedTuple { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `typing.NamedTuple` instead of `collections.namedtuple`") | ||
} | ||
|
||
fn autofix_title(&self) -> Option<String> { | ||
Some(format!("Replace with `typing.NamedTuple`")) | ||
} | ||
} | ||
|
||
/// PYI024 | ||
pub(crate) fn collections_named_tuple(checker: &mut Checker, expr: &Expr) { | ||
if checker | ||
.semantic_model() | ||
.resolve_call_path(expr) | ||
.map_or(false, |call_path| { | ||
matches!(call_path.as_slice(), ["collections", "namedtuple"]) | ||
}) | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(CollectionsNamedTuple, expr.range())); | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI024_PYI024.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 | ||
--- | ||
|
37 changes: 37 additions & 0 deletions
37
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI024_PYI024.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,37 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI024.pyi:3:9: PYI024 Use `typing.NamedTuple` instead of `collections.namedtuple` | ||
| | ||
3 | import collections | ||
4 | | ||
5 | person: collections.namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
| ^^^^^^^^^^^^^^^^^^^^^^ PYI024 | ||
6 | | ||
7 | from collections import namedtuple | ||
| | ||
= help: Replace with `typing.NamedTuple` | ||
|
||
PYI024.pyi:7:9: PYI024 Use `typing.NamedTuple` instead of `collections.namedtuple` | ||
| | ||
7 | from collections import namedtuple | ||
8 | | ||
9 | person: namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
| ^^^^^^^^^^ PYI024 | ||
10 | | ||
11 | person = namedtuple( | ||
| | ||
= help: Replace with `typing.NamedTuple` | ||
|
||
PYI024.pyi:9:10: PYI024 Use `typing.NamedTuple` instead of `collections.namedtuple` | ||
| | ||
9 | person: namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
10 | | ||
11 | person = namedtuple( | ||
| ^^^^^^^^^^ PYI024 | ||
12 | "Person", ["name", "age"] | ||
13 | ) # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
| | ||
= help: Replace with `typing.NamedTuple` | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.