Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F811_34.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Regression test for: https://github.com/astral-sh/ruff/issues/23802"""

# F811: both annotated assignments, first unused
bar: int = 1
bar: int = 2 # F811

x: str = "hello"
x: str = "world" # F811

# OK: plain reassignment (no annotation)
y = 1
y = 2

# OK: first is plain, second is annotated
z = 1
z: int = 2

# OK: first is annotated, second is plain
w: int = 1
w = 2

# OK: used between assignments
a: int = 1
print(a)
a: int = 2
7 changes: 7 additions & 0 deletions crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ use crate::settings::{LinterSettings, types::PreviewMode};

// Rule-specific behavior

// https://github.com/astral-sh/ruff/issues/23802
pub(crate) const fn is_annotated_assignment_redefinition_enabled(
settings: &LinterSettings,
) -> bool {
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/pull/21382
pub(crate) const fn is_custom_exception_checking_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,19 @@ mod tests {
Ok(())
}

#[test]
fn f811_annotated_assignment_redefinition() -> Result<()> {
let diagnostics = test_path(
Path::new("pyflakes/F811_34.py"),
&LinterSettings {
preview: PreviewMode::Enabled,
..LinterSettings::for_rule(Rule::RedefinedWhileUnused)
},
)?;
assert_diagnostics!(diagnostics);
Ok(())
}

#[test]
fn extend_generics() -> Result<()> {
let snapshot = "extend_immutable_calls".to_string();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::Stmt;
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{BindingKind, Imported, Scope, ScopeId};
use ruff_python_semantic::{Binding, BindingKind, Imported, Scope, ScopeId, SemanticModel};
use ruff_source_file::SourceRow;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::fix::edits;
use crate::preview::is_annotated_assignment_redefinition_enabled;
use crate::{Fix, FixAvailability, Violation};

use rustc_hash::FxHashMap;
Expand All @@ -31,6 +33,14 @@ use rustc_hash::FxHashMap;
/// import bar
/// ```
///
/// ## Preview
/// When [preview] is enabled, this rule also flags annotated variable
/// redeclarations. For example, `bar: int = 1` followed by `bar: int = 2`
/// will be flagged as a redefinition of an unused variable, whereas plain
/// reassignments like `bar = 1` followed by `bar = 2` remain unflagged.
///
/// [preview]: https://docs.astral.sh/ruff/preview/
///
/// ## Options
///
/// This rule ignores dummy variables, as determined by:
Expand Down Expand Up @@ -79,9 +89,15 @@ pub(crate) fn redefined_while_unused(checker: &Checker, scope_id: ScopeId, scope
}

// If the shadowing binding isn't considered a "redefinition" of the
// shadowed binding, abort.
// shadowed binding, abort — unless both are annotated assignments
// and preview mode is enabled (see #23802).
if !binding.redefines(shadowed) {
continue;
if !(is_annotated_assignment_redefinition_enabled(checker.settings())
&& is_annotated_assignment(binding, checker.semantic())
&& is_annotated_assignment(shadowed, checker.semantic()))
{
continue;
}
}

if shadow.same_scope() {
Expand Down Expand Up @@ -224,3 +240,9 @@ pub(crate) fn redefined_while_unused(checker: &Checker, scope_id: ScopeId, scope
}
}
}

fn is_annotated_assignment(binding: &Binding, semantic: &SemanticModel) -> bool {
binding
.statement(semantic)
.is_some_and(Stmt::is_ann_assign_stmt)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811 Redefinition of unused `bar` from line 4
--> F811_34.py:4:1
|
3 | # F811: both annotated assignments, first unused
4 | bar: int = 1
| --- previous definition of `bar` here
5 | bar: int = 2 # F811
| ^^^ `bar` redefined here
6 |
7 | x: str = "hello"
|
help: Remove definition: `bar`

F811 Redefinition of unused `x` from line 7
--> F811_34.py:7:1
|
5 | bar: int = 2 # F811
6 |
7 | x: str = "hello"
| - previous definition of `x` here
8 | x: str = "world" # F811
| ^ `x` redefined here
9 |
10 | # OK: plain reassignment (no annotation)
|
help: Remove definition: `x`
Loading