-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[flake8-pyi] Implement PYI057 #11486
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
33d3c5f
[flake8-pyi] Implement PYI057
tomasr8 25894cf
Move rule from stable to preview
tomasr8 7bc036c
Use 'resolve_qualified_name' to simplify code
tomasr8 e5b8584
Make clippy happy
tomasr8 7234065
Only use the name range in imports
tomasr8 2f01144
Improve documentation
tomasr8 b0d1fc6
Do not implement 'fix_title'
tomasr8 916f31c
Fix tests
tomasr8 69b2f08
Use an enum to encode ByteString origin
tomasr8 7a7ae26
Minor nitpicks
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI057.py
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,10 @@ | ||
import typing | ||
import collections.abc | ||
import foo | ||
from typing import ByteString | ||
from collections.abc import ByteString | ||
from foo import ByteString | ||
|
||
a: typing.ByteString | ||
b: collections.abc.ByteString | ||
c: foo.ByteString |
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI057.pyi
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,10 @@ | ||
import typing | ||
import collections.abc | ||
import foo | ||
from typing import ByteString | ||
from collections.abc import ByteString | ||
from foo import ByteString | ||
|
||
a: typing.ByteString | ||
b: collections.abc.ByteString | ||
c: foo.ByteString |
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
105 changes: 105 additions & 0 deletions
105
crates/ruff_linter/src/rules/flake8_pyi/rules/bytestring_usage.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,105 @@ | ||
use ruff_diagnostics::{Diagnostic, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, Expr}; | ||
use ruff_python_semantic::Modules; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `typing.ByteString` or `collections.abc.ByteString`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `ByteString` has been deprecated since Python 3.9 and will be removed in | ||
/// Python 3.14. The Python documentation recommends using either | ||
/// `collections.abc.Buffer` (or the `typing_extensions` backport | ||
/// on Python <3.12) or a union like `bytes | bytearray | memoryview` instead. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from typing import ByteString | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from collections.abc import Buffer | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The `ByteString` type](https://docs.python.org/3/library/typing.html#typing.ByteString) | ||
#[violation] | ||
pub struct ByteStringUsage { | ||
origin: ByteStringOrigin, | ||
} | ||
|
||
impl Violation for ByteStringUsage { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let ByteStringUsage { origin } = self; | ||
format!("Do not use `{origin}.ByteString`, which has unclear semantics and is deprecated") | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
enum ByteStringOrigin { | ||
Typing, | ||
CollectionsAbc, | ||
} | ||
|
||
impl std::fmt::Display for ByteStringOrigin { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str(match self { | ||
Self::Typing => "typing", | ||
Self::CollectionsAbc => "collections.abc", | ||
}) | ||
} | ||
} | ||
|
||
/// PYI057 | ||
pub(crate) fn bytestring_attribute(checker: &mut Checker, attribute: &Expr) { | ||
let semantic = checker.semantic(); | ||
if !semantic | ||
.seen | ||
.intersects(Modules::TYPING | Modules::COLLECTIONS) | ||
{ | ||
return; | ||
} | ||
let Some(qualified_name) = semantic.resolve_qualified_name(attribute) else { | ||
return; | ||
}; | ||
let origin = match qualified_name.segments() { | ||
["typing", "ByteString"] => ByteStringOrigin::Typing, | ||
["collections", "abc", "ByteString"] => ByteStringOrigin::CollectionsAbc, | ||
_ => return, | ||
}; | ||
checker.diagnostics.push(Diagnostic::new( | ||
ByteStringUsage { origin }, | ||
attribute.range(), | ||
)); | ||
} | ||
|
||
/// PYI057 | ||
pub(crate) fn bytestring_import(checker: &mut Checker, import_from: &ast::StmtImportFrom) { | ||
let ast::StmtImportFrom { names, module, .. } = import_from; | ||
|
||
let module_id = match module { | ||
Some(module) => module.id.as_str(), | ||
None => return, | ||
}; | ||
|
||
let origin = match module_id { | ||
"typing" => ByteStringOrigin::Typing, | ||
"collections.abc" => ByteStringOrigin::CollectionsAbc, | ||
_ => return, | ||
}; | ||
|
||
for name in names { | ||
if name.name.as_str() == "ByteString" { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ByteStringUsage { origin }, name.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
39 changes: 39 additions & 0 deletions
39
...c/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI057_PYI057.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,39 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI057.py:4:20: PYI057 Do not use `typing.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
2 | import collections.abc | ||
3 | import foo | ||
4 | from typing import ByteString | ||
| ^^^^^^^^^^ PYI057 | ||
5 | from collections.abc import ByteString | ||
6 | from foo import ByteString | ||
| | ||
|
||
PYI057.py:5:29: PYI057 Do not use `collections.abc.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
3 | import foo | ||
4 | from typing import ByteString | ||
5 | from collections.abc import ByteString | ||
| ^^^^^^^^^^ PYI057 | ||
6 | from foo import ByteString | ||
| | ||
|
||
PYI057.py:8:4: PYI057 Do not use `typing.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
6 | from foo import ByteString | ||
7 | | ||
8 | a: typing.ByteString | ||
| ^^^^^^^^^^^^^^^^^ PYI057 | ||
9 | b: collections.abc.ByteString | ||
10 | c: foo.ByteString | ||
| | ||
|
||
PYI057.py:9:4: PYI057 Do not use `collections.abc.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
8 | a: typing.ByteString | ||
9 | b: collections.abc.ByteString | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI057 | ||
10 | c: foo.ByteString | ||
| |
39 changes: 39 additions & 0 deletions
39
.../rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI057_PYI057.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,39 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI057.pyi:4:20: PYI057 Do not use `typing.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
2 | import collections.abc | ||
3 | import foo | ||
4 | from typing import ByteString | ||
| ^^^^^^^^^^ PYI057 | ||
5 | from collections.abc import ByteString | ||
6 | from foo import ByteString | ||
| | ||
|
||
PYI057.pyi:5:29: PYI057 Do not use `collections.abc.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
3 | import foo | ||
4 | from typing import ByteString | ||
5 | from collections.abc import ByteString | ||
| ^^^^^^^^^^ PYI057 | ||
6 | from foo import ByteString | ||
| | ||
|
||
PYI057.pyi:8:4: PYI057 Do not use `typing.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
6 | from foo import ByteString | ||
7 | | ||
8 | a: typing.ByteString | ||
| ^^^^^^^^^^^^^^^^^ PYI057 | ||
9 | b: collections.abc.ByteString | ||
10 | c: foo.ByteString | ||
| | ||
|
||
PYI057.pyi:9:4: PYI057 Do not use `collections.abc.ByteString`, which has unclear semantics and is deprecated | ||
| | ||
8 | a: typing.ByteString | ||
9 | b: collections.abc.ByteString | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI057 | ||
10 | c: foo.ByteString | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomasr8: I added this in my latest push. It's an optimisation we do in a lot of rules. It's a very cheap check to see whether any of the relevant modules we care about have been imported at all. If not, we can just short-circuit the rest of the rule :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah cool! I'll keep that in mind for future PRs :)