[pyflakes] Detect assignments that shadow definitions (F811) (#11961)

## Summary
This PR updates `F811` rule to include assignment as possible shadowed
binding. This will fix issue: #11828 .

## Test Plan

Add a test file, F811_30.py, which includes a redefinition after an
assignment and a verified snapshot file.
This commit is contained in:
ukyen 2024-06-23 18:29:32 +01:00 committed by GitHub
parent c3f61a012e
commit 068b75cc8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 2 deletions

View file

@ -177,16 +177,31 @@ impl<'a> Binding<'a> {
| BindingKind::Builtin => {
return false;
}
// Assignment-assignment bindings are not considered redefinitions, as in:
// ```python
// x = 1
// x = 2
// ```
BindingKind::Assignment | BindingKind::NamedExprAssignment => {
if matches!(
existing.kind,
BindingKind::Assignment | BindingKind::NamedExprAssignment
) {
return false;
}
}
_ => {}
}
// Otherwise, the shadowed binding must be a class definition, function definition, or
// import to be considered a redefinition.
// Otherwise, the shadowed binding must be a class definition, function definition,
// import, or assignment to be considered a redefinition.
matches!(
existing.kind,
BindingKind::ClassDefinition(_)
| BindingKind::FunctionDefinition(_)
| BindingKind::Import(_)
| BindingKind::FromImport(_)
| BindingKind::Assignment
| BindingKind::NamedExprAssignment
)
}