Avoid F821 false positive on annotated global (#1196)

This commit is contained in:
Charlie Marsh
2022-12-11 10:04:06 -05:00
committed by GitHub
parent 247dcc9f9c
commit 360b033e04
4 changed files with 39 additions and 2 deletions

View File

@@ -1155,7 +1155,13 @@ where
// If any global bindings don't already exist in the global scope, add it.
let globals = operations::extract_globals(body);
for (name, stmt) in operations::extract_globals(body) {
if !self.scopes[GLOBAL_SCOPE_INDEX].values.contains_key(name) {
if self.scopes[GLOBAL_SCOPE_INDEX]
.values
.get(name)
.map_or(true, |index| {
matches!(self.bindings[*index].kind, BindingKind::Annotation)
})
{
let index = self.bindings.len();
self.bindings.push(Binding {
kind: BindingKind::Assignment,
@@ -1207,7 +1213,13 @@ where
// If any global bindings don't already exist in the global scope, add it.
let globals = operations::extract_globals(body);
for (name, stmt) in &globals {
if !self.scopes[GLOBAL_SCOPE_INDEX].values.contains_key(name) {
if self.scopes[GLOBAL_SCOPE_INDEX]
.values
.get(name)
.map_or(true, |index| {
matches!(self.bindings[*index].kind, BindingKind::Annotation)
})
{
let index = self.bindings.len();
self.bindings.push(Binding {
kind: BindingKind::Assignment,
@@ -2717,6 +2729,7 @@ impl<'a> Checker<'a> {
let mut first_iter = true;
let mut in_generator = false;
let mut import_starred = false;
for scope_index in self.scope_stack.iter().rev() {
let scope = &self.scopes[*scope_index];

View File

@@ -93,6 +93,7 @@ mod tests {
#[test_case(CheckCode::F821, Path::new("F821_3.py"); "F821_3")]
#[test_case(CheckCode::F821, Path::new("F821_4.py"); "F821_4")]
#[test_case(CheckCode::F821, Path::new("F821_5.py"); "F821_5")]
#[test_case(CheckCode::F821, Path::new("F821_6.py"); "F821_6")]
#[test_case(CheckCode::F822, Path::new("F822.py"); "F822")]
#[test_case(CheckCode::F823, Path::new("F823.py"); "F823")]
#[test_case(CheckCode::F831, Path::new("F831.py"); "F831")]

View File

@@ -0,0 +1,6 @@
---
source: src/pyflakes/mod.rs
expression: checks
---
[]