-
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 pylint rule invalid-bytes-returned (PLE0308) See #970 for rules Test Plan: `cargo test`
- Loading branch information
1 parent
06b3e37
commit 1480d72
Showing
10 changed files
with
239 additions
and
29 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bytes.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,65 @@ | ||
# These testcases should raise errors | ||
|
||
|
||
class Float: | ||
def __bytes__(self): | ||
return 3.05 # [invalid-bytes-return] | ||
|
||
|
||
class Int: | ||
def __bytes__(self): | ||
return 0 # [invalid-bytes-return] | ||
|
||
|
||
class Str: | ||
def __bytes__(self): | ||
return "some bytes" # [invalid-bytes-return] | ||
|
||
|
||
class BytesNoReturn: | ||
def __bytes__(self): | ||
print("ruff") # [invalid-bytes-return] | ||
|
||
|
||
class BytesWrongRaise: | ||
def __bytes__(self): | ||
print("raise some error") | ||
raise NotImplementedError # [invalid-bytes-return] | ||
|
||
|
||
# TODO: Once Ruff has better type checking | ||
def return_bytes(): | ||
return "some string" | ||
|
||
|
||
class ComplexReturn: | ||
def __bytes__(self): | ||
return return_bytes() # [invalid-bytes-return] | ||
|
||
|
||
# These testcases should NOT raise errors | ||
|
||
|
||
class Bytes: | ||
def __bytes__(self): | ||
return b"some bytes" | ||
|
||
|
||
class Bytes2: | ||
def __bytes__(self): | ||
x = b"some bytes" | ||
return x | ||
|
||
|
||
class Bytes3: | ||
def __bytes__(self): ... | ||
|
||
|
||
class Bytes4: | ||
def __bytes__(self): | ||
pass | ||
|
||
|
||
class Bytes5: | ||
def __bytes__(self): | ||
raise NotImplementedError |
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
90 changes: 90 additions & 0 deletions
90
crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.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,90 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::ReturnStatementVisitor; | ||
use ruff_python_ast::identifier::Identifier; | ||
use ruff_python_ast::visitor::Visitor; | ||
use ruff_python_ast::{self as ast}; | ||
use ruff_python_semantic::analyze::function_type::is_stub; | ||
use ruff_python_semantic::analyze::type_inference::{PythonType, ResolvedPythonType}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `__bytes__` implementations that return a type other than `bytes`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `__bytes__` method should return a `bytes` object. Returning a different | ||
/// type may cause unexpected behavior. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __bytes__(self): | ||
/// return 2 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// def __bytes__(self): | ||
/// return b"2" | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The `__bytes__` method](https://docs.python.org/3/reference/datamodel.html#object.__bytes__) | ||
#[violation] | ||
pub struct InvalidBytesReturnType; | ||
|
||
impl Violation for InvalidBytesReturnType { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`__bytes__` does not return `bytes`") | ||
} | ||
} | ||
|
||
/// E0308 | ||
pub(crate) fn invalid_bytes_return(checker: &mut Checker, function_def: &ast::StmtFunctionDef) { | ||
if function_def.name.as_str() != "__bytes__" { | ||
return; | ||
} | ||
|
||
if !checker.semantic().current_scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
if is_stub(function_def, checker.semantic()) { | ||
return; | ||
} | ||
|
||
let returns = { | ||
let mut visitor = ReturnStatementVisitor::default(); | ||
visitor.visit_body(&function_def.body); | ||
visitor.returns | ||
}; | ||
|
||
if returns.is_empty() { | ||
checker.diagnostics.push(Diagnostic::new( | ||
InvalidBytesReturnType, | ||
function_def.identifier(), | ||
)); | ||
} | ||
|
||
for stmt in returns { | ||
if let Some(value) = stmt.value.as_deref() { | ||
if !matches!( | ||
ResolvedPythonType::from(value), | ||
ResolvedPythonType::Unknown | ResolvedPythonType::Atom(PythonType::Bytes) | ||
) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBytesReturnType, value.range())); | ||
} | ||
} else { | ||
// Disallow implicit `None`. | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBytesReturnType, stmt.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
43 changes: 43 additions & 0 deletions
43
...nt/snapshots/ruff_linter__rules__pylint__tests__PLE0308_invalid_return_type_bytes.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,43 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
invalid_return_type_bytes.py:6:16: PLE0308 `__bytes__` does not return `bytes` | ||
| | ||
4 | class Float: | ||
5 | def __bytes__(self): | ||
6 | return 3.05 # [invalid-bytes-return] | ||
| ^^^^ PLE0308 | ||
| | ||
|
||
invalid_return_type_bytes.py:11:16: PLE0308 `__bytes__` does not return `bytes` | ||
| | ||
9 | class Int: | ||
10 | def __bytes__(self): | ||
11 | return 0 # [invalid-bytes-return] | ||
| ^ PLE0308 | ||
| | ||
|
||
invalid_return_type_bytes.py:16:16: PLE0308 `__bytes__` does not return `bytes` | ||
| | ||
14 | class Str: | ||
15 | def __bytes__(self): | ||
16 | return "some bytes" # [invalid-bytes-return] | ||
| ^^^^^^^^^^^^ PLE0308 | ||
| | ||
|
||
invalid_return_type_bytes.py:20:9: PLE0308 `__bytes__` does not return `bytes` | ||
| | ||
19 | class BytesNoReturn: | ||
20 | def __bytes__(self): | ||
| ^^^^^^^^^ PLE0308 | ||
21 | print("ruff") # [invalid-bytes-return] | ||
| | ||
|
||
invalid_return_type_bytes.py:25:9: PLE0308 `__bytes__` does not return `bytes` | ||
| | ||
24 | class BytesWrongRaise: | ||
25 | def __bytes__(self): | ||
| ^^^^^^^^^ PLE0308 | ||
26 | print("raise some error") | ||
27 | raise NotImplementedError # [invalid-bytes-return] | ||
| |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.