Allow F811 noqa declarations on containing import lines (#2553)

This commit is contained in:
Charlie Marsh 2023-02-03 14:51:06 -05:00 committed by GitHub
parent 64c79bde83
commit 9751951d10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 5 deletions

View file

@ -0,0 +1,53 @@
"""Test: noqa directives."""
from typing_extensions import List, Sequence
# This should ignore both errors.
from typing import ( # noqa: F811
List,
Sequence,
)
# This should ignore both errors.
from typing import ( # noqa
List,
Sequence,
)
# This should ignore both errors.
from typing import (
List, # noqa: F811
Sequence, # noqa: F811
)
# This should ignore both errors.
from typing import (
List, # noqa
Sequence, # noqa
)
# This should ignore the first error.
from typing import (
List, # noqa: F811
Sequence,
)
# This should ignore both errors.
from typing import ( # noqa
List,
Sequence,
)
# This should ignore both errors.
from typing import List, Sequence # noqa: F811
# This should ignore both errors.
from typing import List, Sequence # noqa
def f():
# This should ignore both errors.
from typing import ( # noqa: F811
List,
Sequence,
)

View file

@ -3858,13 +3858,21 @@ impl<'a> Checker<'a> {
))
{
if self.settings.rules.enabled(&Rule::RedefinedWhileUnused) {
self.diagnostics.push(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
pyflakes::rules::RedefinedWhileUnused {
name: name.to_string(),
line: existing.range.location.row(),
},
binding_range(&binding, self.locator),
));
);
if let Some(parent) = binding.source.as_ref() {
if matches!(parent.node, StmtKind::ImportFrom { .. })
&& parent.location.row() != binding.range.location.row()
{
diagnostic.parent(parent.location);
}
}
self.diagnostics.push(diagnostic);
}
}
} else if existing_is_import && binding.redefines(existing) {
@ -4614,13 +4622,22 @@ impl<'a> Checker<'a> {
if let Some(indices) = self.redefinitions.get(index) {
for index in indices {
diagnostics.push(Diagnostic::new(
let rebound = &self.bindings[*index];
let mut diagnostic = Diagnostic::new(
pyflakes::rules::RedefinedWhileUnused {
name: (*name).to_string(),
line: binding.range.location.row(),
},
binding_range(&self.bindings[*index], self.locator),
));
binding_range(rebound, self.locator),
);
if let Some(parent) = &rebound.source {
if matches!(parent.node, StmtKind::ImportFrom { .. })
&& parent.location.row() != rebound.range.location.row()
{
diagnostic.parent(parent.location);
}
};
diagnostics.push(diagnostic);
}
}
}

View file

@ -89,6 +89,7 @@ mod tests {
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_18.py"); "F811_18")]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_19.py"); "F811_19")]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_20.py"); "F811_20")]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_21.py"); "F811_21")]
#[test_case(Rule::UndefinedName, Path::new("F821_0.py"); "F821_0")]
#[test_case(Rule::UndefinedName, Path::new("F821_1.py"); "F821_1")]
#[test_case(Rule::UndefinedName, Path::new("F821_2.py"); "F821_2")]

View file

@ -0,0 +1,19 @@
---
source: src/rules/pyflakes/mod.rs
expression: diagnostics
---
- kind:
RedefinedWhileUnused:
name: Sequence
line: 26
location:
row: 32
column: 4
end_location:
row: 32
column: 12
fix: ~
parent:
row: 30
column: 0