-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into codegen-multiline-s…
…tring
- Loading branch information
Showing
37 changed files
with
363 additions
and
297 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
6 changes: 6 additions & 0 deletions
6
crates/ruff_linter/resources/test/fixtures/pyflakes/F811_28.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,6 @@ | ||
"""Regression test for: https://github.com/astral-sh/ruff/issues/10384""" | ||
|
||
import datetime | ||
from datetime import datetime | ||
|
||
datetime(1, 2, 3) |
37 changes: 37 additions & 0 deletions
37
crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bool.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,37 @@ | ||
# These testcases should raise errors | ||
|
||
class Float: | ||
def __bool__(self): | ||
return 3.05 # [invalid-bool-return] | ||
|
||
class Int: | ||
def __bool__(self): | ||
return 0 # [invalid-bool-return] | ||
|
||
|
||
class Str: | ||
def __bool__(self): | ||
x = "ruff" | ||
return x # [invalid-bool-return] | ||
|
||
# TODO: Once Ruff has better type checking | ||
def return_int(): | ||
return 3 | ||
|
||
class ComplexReturn: | ||
def __bool__(self): | ||
return return_int() # [invalid-bool-return] | ||
|
||
|
||
|
||
# These testcases should NOT raise errors | ||
|
||
class Bool: | ||
def __bool__(self): | ||
return True | ||
|
||
|
||
class Bool2: | ||
def __bool__(self): | ||
x = True | ||
return x |
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
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
8 changes: 1 addition & 7 deletions
8
...ter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_1.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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F811_1.py:1:25: F811 [*] Redefinition of unused `FU` from line 1 | ||
F811_1.py:1:25: F811 Redefinition of unused `FU` from line 1 | ||
| | ||
1 | import fu as FU, bar as FU | ||
| ^^ F811 | ||
| | ||
= help: Remove definition: `FU` | ||
|
||
ℹ Safe fix | ||
1 |-import fu as FU, bar as FU | ||
1 |+import fu as FU | ||
|
||
|
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
8 changes: 1 addition & 7 deletions
8
...ter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_2.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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F811_2.py:1:34: F811 [*] Redefinition of unused `FU` from line 1 | ||
F811_2.py:1:34: F811 Redefinition of unused `FU` from line 1 | ||
| | ||
1 | from moo import fu as FU, bar as FU | ||
| ^^ F811 | ||
| | ||
= help: Remove definition: `FU` | ||
|
||
ℹ Safe fix | ||
1 |-from moo import fu as FU, bar as FU | ||
1 |+from moo import fu as FU | ||
|
||
|
10 changes: 1 addition & 9 deletions
10
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_23.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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F811_23.py:4:15: F811 [*] Redefinition of unused `foo` from line 3 | ||
F811_23.py:4:15: F811 Redefinition of unused `foo` from line 3 | ||
| | ||
3 | import foo as foo | ||
4 | import bar as foo | ||
| ^^^ F811 | ||
| | ||
= help: Remove definition: `foo` | ||
|
||
ℹ Safe fix | ||
1 1 | """Test that shadowing an explicit re-export produces a warning.""" | ||
2 2 | | ||
3 3 | import foo as foo | ||
4 |-import bar as foo | ||
|
||
|
12 changes: 12 additions & 0 deletions
12
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_28.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,12 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F811_28.py:4:22: F811 Redefinition of unused `datetime` from line 3 | ||
| | ||
3 | import datetime | ||
4 | from datetime import datetime | ||
| ^^^^^^^^ F811 | ||
5 | | ||
6 | datetime(1, 2, 3) | ||
| | ||
= help: Remove definition: `datetime` |
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
78 changes: 78 additions & 0 deletions
78
crates/ruff_linter/src/rules/pylint/rules/invalid_bool_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,78 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::ReturnStatementVisitor; | ||
use ruff_python_ast::visitor::Visitor; | ||
use ruff_python_ast::Stmt; | ||
use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `__bool__` implementations that return a type other than `bool`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `__bool__` method should return a `bool` object. Returning a different | ||
/// type may cause unexpected behavior. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __bool__(self): | ||
/// return 2 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// def __bool__(self): | ||
/// return True | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The `__bool__` method](https://docs.python.org/3/reference/datamodel.html#object.__bool__) | ||
#[violation] | ||
pub struct InvalidBoolReturnType; | ||
|
||
impl Violation for InvalidBoolReturnType { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`__bool__` does not return `bool`") | ||
} | ||
} | ||
|
||
/// E0307 | ||
pub(crate) fn invalid_bool_return(checker: &mut Checker, name: &str, body: &[Stmt]) { | ||
if name != "__bool__" { | ||
return; | ||
} | ||
|
||
if !checker.semantic().current_scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
let returns = { | ||
let mut visitor = ReturnStatementVisitor::default(); | ||
visitor.visit_body(body); | ||
visitor.returns | ||
}; | ||
|
||
for stmt in returns { | ||
if let Some(value) = stmt.value.as_deref() { | ||
if !matches!( | ||
ResolvedPythonType::from(value), | ||
ResolvedPythonType::Unknown | ||
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Bool)) | ||
) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBoolReturnType, value.range())); | ||
} | ||
} else { | ||
// Disallow implicit `None`. | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBoolReturnType, 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
Oops, something went wrong.